Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update prerender option to discover and resolve routes #211

Merged
merged 1 commit into from
Jan 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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