+
Skip to content

feat(vite-plugin-nitro): support static server side data fetching during prerendering #1771

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

Merged
merged 1 commit into from
Jun 13, 2025
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: 11 additions & 1 deletion apps/analog-app/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,17 @@ export default defineConfig(({ mode, isSsrBuild }) => {
additionalPagesDirs: ['/libs/shared/feature'],
additionalAPIDirs: ['/libs/shared/feature/src/api'],
prerender: {
routes: ['/', '/cart', '/shipping', '/client', '/404.html'],
routes: [
'/',
'/cart',
'/shipping',
'/client',
'/404.html',
{
route: '/newsletter',
staticData: true,
},
],
sitemap: {
host: base,
},
Expand Down
55 changes: 42 additions & 13 deletions apps/docs-app/docs/features/server/static-site-generation.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ export default defineConfig(({ mode }) => ({

### From Content Directory

You might want to prerender all routes that are the result of a rendered content directory.
For example if you have a blog and all your articles are places as Markdown files in the `contents` directory.
You might want to prerender all routes that are the result of a rendered content directory. For example if you have a blog and all your articles are places as Markdown files in the `contents` directory.

For such scenarios, you can add an object to the `routes` config to render everything within a directory.
Keep in mind, that your directory structure may not be reflected 1:1 in your apps path.
Therefore, you have to pass a `transform` function which maps the file paths to the URLs.
The returning string should be the URL path in your app.
Using `transform` allows you also filter out some routes by returning `false`.
This does not include them in the prerender process, such as files marked as `draft` in the frontmatter.

The directory structure may not be reflected 1:1 in your apps path. Therefore, you have to pass a `transform` function which maps the file paths to the URLs. The returning string should be the URL path in your app.

Using `transform` allows you also filter out some routes by returning `false`. This does not include them in the prerender process, such as files marked as `draft` in the frontmatter.

The `contentDir` value of that object can be a glob pattern or just a specific path.

```ts
Expand Down Expand Up @@ -72,9 +72,9 @@ export default defineConfig(({ mode }) => ({
}));
```

### Only static pages
## Only Prerendering Static Pages

To only prerender the static pages, use the `static: true` flag.
To only prerender the static pages without building the server, use the `static: true` flag.

> The `ssr` flag must still be set to `true` for prerendering static pages.

Expand Down Expand Up @@ -109,7 +109,36 @@ export default defineConfig(({ mode }) => ({

The static pages can be deployed from the `dist/analog/public` directory.

### Sitemap Generation
## Prerendering Server-Side Data

When using [Server-Side Data Fetching](/docs/features/data-fetching/server-side-data-fetching), the data is cached and reused using transfer state _only_ on the first request. To prerender the server-side data fetched along with the route, set the `staticData` flag to `true` in the config object for the prerendered route.

For example, a route defined as `src/app/pages/shipping.page.ts` with an associated `src/app/pages/shipping.server.ts` has the route and server data prerendered to be completely static.

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

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => ({
plugins: [
analog({
static: true,
prerender: {
routes: async () => [
'/',
{
route: '/shipping',
staticData: true,
},
],
},
}),
],
}));
```

## Sitemap Generation

Analog also supports automatic sitemap generation. Analog generates a sitemap in the `dist/analog/public` directory when running a build if a sitemap configuration is provided.

Expand All @@ -124,7 +153,7 @@ export default defineConfig(({ mode }) => ({
prerender: {
routes: async () => ['/', '/blog'],
sitemap: {
host: 'https://analogjs.org/',
host: 'https://analogjs.org',
},
},
}),
Expand All @@ -145,7 +174,7 @@ export default defineConfig(({ mode }) => ({
analog({
prerender: {
sitemap: {
host: 'https://analogjs.org/',
host: 'https://analogjs.org',
},
routes: async () => [
'/',
Expand Down Expand Up @@ -198,7 +227,7 @@ mapping of the pages' `<loc>`, `<lastmod>`, `<changefreq>`, and `<priority>` pro
</urlset...>
```

### Post-rendering Hooks
## Post-rendering Hooks

Analog supports the post-rendering hooks during the prerendering process. The use case for post-rendering hooks can be inlining critical CSS, adding/removing scripts in HTML files, etc.

Expand Down
4 changes: 4 additions & 0 deletions packages/vite-plugin-nitro/src/lib/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,8 @@ export interface PrerenderRouteConfig {
* https://www.sitemaps.org/protocol.html#xmlTagDefinitions
*/
sitemap?: PrerenderSitemapConfig | (() => PrerenderSitemapConfig);
/**
* Prerender static data for the prerendered route
*/
staticData?: boolean;
}
11 changes: 11 additions & 0 deletions packages/vite-plugin-nitro/src/lib/vite-plugin-nitro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,12 @@ export function nitro(options?: Options, nitroOptions?: NitroConfig): Plugin[] {
}

prev.push(current.route);

// Add the server-side data fetching endpoint URL
if ('staticData' in current) {
prev.push(`${apiPrefix}/_analog/pages/${current.route}`);
}

return prev;
}

Expand All @@ -304,6 +310,11 @@ export function nitro(options?: Options, nitroOptions?: NitroConfig): Plugin[] {
}

prev.push(result);

// Add the server-side data fetching endpoint URL
if ('staticData' in current) {
prev.push(`${apiPrefix}/_analog/pages/${result}`);
}
}
});

Expand Down
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载