Skip to content

Commit

Permalink
refactor: update prerender option to discover and resolve routes
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonroberts committed Jan 15, 2023
1 parent e3a5642 commit 0c042fc
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
12 changes: 6 additions & 6 deletions apps/blog-app/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import { defineConfig } from 'vite';
export default defineConfig(() => {
return {
publicDir: 'src/assets',
ssr: {
noExternal: ['@analogjs/router'],
},
build: {
target: ['es2020'],
},
Expand All @@ -18,6 +15,12 @@ export default defineConfig(() => {
ssr: true,
ssrBuildDir: '../../dist/apps/blog-app/ssr',
entryServer: 'apps/blog-app/src/main.server.ts',
static: true,
prerender: {
routes: async () => {
return ['/', '/about', '/blog/2022-12-27-my-first-post'];
},
},
vite: {
tsconfig: 'apps/blog-app/tsconfig.app.json',
},
Expand All @@ -32,9 +35,6 @@ export default defineConfig(() => {
{ baseName: 'public', dir: `./dist/apps/blog-app/client` },
],
buildDir: '../../dist/apps/blog-app/.nitro',
prerender: {
routes: ['/', '/about', '/blog/2022-12-27-my-first-post'],
},
},
}),
],
Expand Down
7 changes: 6 additions & 1 deletion packages/platform/src/lib/build-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@ export async function buildServer(
});
await prepare(nitro);
await copyPublicAssets(nitro);

if (options?.static) {
console.log(`Prerendering static pages...`);
}

await prerender(nitro);

if (!options?.prerender) {
if (!options?.static) {
await build(nitro);
}

Expand Down
18 changes: 17 additions & 1 deletion packages/platform/src/lib/options.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
import type { PluginOptions } from '@analogjs/vite-plugin-angular';
import { NitroConfig } from 'nitropack';

export interface PrerenderOptions {
/**
* Add additional routes to prerender through crawling page links.
*/
discover?: boolean;

/**
* List of routes to prerender resolved statically or dynamically.
*/
routes?: string[] | (() => Promise<(string | undefined)[]>);
}

export interface Options {
ssr?: boolean;
ssrBuildDir?: string;
prerender?: boolean;
/**
* Prerender the static pages without producing the server output.
*/
static?: boolean;
prerender?: PrerenderOptions;
entryServer?: string;
vite?: PluginOptions;
nitro?: NitroConfig;
Expand Down
11 changes: 10 additions & 1 deletion packages/platform/src/lib/vite-nitro-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,21 @@ export function viteNitroPlugin(

return {
name: 'analogjs-vite-nitro-plugin',
config(_config, { command }) {
async config(_config, { command }) {
isServe = command === 'serve';
isBuild = command === 'build';
ssrBuild = _config.build?.ssr === true;
config = _config;

if (isBuild && options?.prerender) {
nitroConfig.prerender = {};
nitroConfig.prerender.crawlLinks = options?.prerender?.discover;

if (typeof options?.prerender?.routes === 'function') {
nitroConfig.prerender.routes = await options.prerender.routes();
}
}

if (isBuild && ssrBuild) {
nitroConfig = {
...nitroConfig,
Expand Down

0 comments on commit 0c042fc

Please sign in to comment.