Skip to content

Commit

Permalink
feat: enable ssr by default for analog platform plugin (#301)
Browse files Browse the repository at this point in the history
  • Loading branch information
goetzrobin authored Mar 21, 2023
1 parent 2267afc commit 95aa678
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 7 deletions.
1 change: 0 additions & 1 deletion apps/analog-app/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export default defineConfig(({ mode }) => {
},
plugins: [
analog({
ssr: true,
ssrBuildDir: '../../dist/apps/analog-app/ssr',
entryServer: 'apps/analog-app/src/main.server.ts',
vite: {
Expand Down
1 change: 0 additions & 1 deletion apps/blog-app/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export default defineConfig(() => {
},
plugins: [
analog({
ssr: true,
ssrBuildDir: '../../dist/apps/blog-app/ssr',
entryServer: 'apps/blog-app/src/main.server.ts',
static: true,
Expand Down
5 changes: 3 additions & 2 deletions apps/docs-app/docs/features/server/server-side-rendering.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

Analog supports server-side rendering during development and building for production.

Enable SSR in the `vite.config.ts` using the `analog()` plugin:
SSR is enabled by default. You can opt out of it and generate a client-only build by
adding the following option to the `analog()` plugin in your `vite.config.ts`:

```ts
import { defineConfig } from 'vite';
import analog from '@analogjs/platform';

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => ({
plugins: [analog({ ssr: true })],
plugins: [analog({ ssr: false })],
}));
```
1 change: 1 addition & 0 deletions packages/platform/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
}
},
"test": {
"dependsOn": ["^build"],
"executor": "@nrwl/vite:test"
}
},
Expand Down
71 changes: 71 additions & 0 deletions packages/platform/src/lib/platform-plugin.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { describe, expect } from 'vitest';
import { platformPlugin } from './platform-plugin';

vi.mock('./vite-nitro-plugin');
vi.mock('./ssr/ssr-build-plugin');
vi.mock('./ssr/dev-server-plugin');

describe('platformPlugin', () => {
const setup = async () => {
const viteNitroPluginImport = await import('./vite-nitro-plugin');
const viteNitroPluginSpy = vi.fn();
viteNitroPluginImport.viteNitroPlugin = viteNitroPluginSpy;

const ssrBuildPluginImport = await import('./ssr/ssr-build-plugin');
const ssrBuildPluginSpy = vi.fn();
ssrBuildPluginImport.ssrBuildPlugin = ssrBuildPluginSpy;

const devServerPluginImport = await import('./ssr/dev-server-plugin');
const devServerPluginSpy = vi.fn();
devServerPluginImport.devServerPlugin = devServerPluginSpy;

return {
viteNitroPluginSpy,
ssrBuildPluginSpy,
devServerPluginSpy,
platformPlugin,
};
};

it('should default ssr to true and pass that value to other plugins if no ssr value provided in options', async () => {
const {
viteNitroPluginSpy,
ssrBuildPluginSpy,
devServerPluginSpy,
platformPlugin,
} = await setup();
platformPlugin();

expect(viteNitroPluginSpy).toHaveBeenCalledWith({ ssr: true }, undefined);
expect(ssrBuildPluginSpy).toHaveBeenCalled();
expect(devServerPluginSpy).toHaveBeenCalled();
});

it('should pass ssr value as true if options.ssr is set to true', async () => {
const {
viteNitroPluginSpy,
ssrBuildPluginSpy,
devServerPluginSpy,
platformPlugin,
} = await setup();
platformPlugin({ ssr: true });

expect(viteNitroPluginSpy).toHaveBeenCalledWith({ ssr: true }, undefined);
expect(ssrBuildPluginSpy).toHaveBeenCalled();
expect(devServerPluginSpy).toHaveBeenCalled();
});

it('should pass ssr value as false if options.ssr is set to false', async () => {
const {
viteNitroPluginSpy,
ssrBuildPluginSpy,
devServerPluginSpy,
platformPlugin,
} = await setup();
platformPlugin({ ssr: false });

expect(viteNitroPluginSpy).toHaveBeenCalledWith({ ssr: false }, undefined);
expect(ssrBuildPluginSpy).not.toHaveBeenCalled();
expect(devServerPluginSpy).not.toHaveBeenCalled();
});
});
14 changes: 11 additions & 3 deletions packages/platform/src/lib/platform-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,20 @@ import { ssrBuildPlugin } from './ssr/ssr-build-plugin';
import { contentPlugin } from './content-plugin';

export function platformPlugin(opts: Options = {}): Plugin[] {
const defaultOptions: Options = {
ssr: true,
};
const mergedOptions = {
...defaultOptions,
...opts,
};

return [
viteNitroPlugin(opts, opts?.nitro),
(opts.ssr ? ssrBuildPlugin() : false) as Plugin,
viteNitroPlugin(mergedOptions, mergedOptions?.nitro),
(mergedOptions.ssr ? ssrBuildPlugin() : false) as Plugin,
...routerPlugin(),
...contentPlugin(),
(opts.ssr
(mergedOptions.ssr
? devServerPlugin({ entryServer: opts.entryServer })
: false) as Plugin,
...angular(opts?.vite),
Expand Down

0 comments on commit 95aa678

Please sign in to comment.