Skip to content

Commit 5f42bc4

Browse files
fix: allow prerender if no pages specified (#4632)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent dbbcd29 commit 5f42bc4

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

docs/start/framework/react/static-prerendering.md

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,32 @@ Static prerendering is the process of generating static HTML files for your appl
77

88
## Prerendering
99

10-
TanStack Start can prerender your application to static HTML files, which can then be served to users without having to generate them on the fly. To prerender your application, you can add the `server.prerender` option to your `app.config.js` file:
10+
TanStack Start can prerender your application to static HTML files, which can then be served to users without having to generate them on the fly. To prerender your application, you can add the `prerender` option to your tanstackStart configuration in `vite.config.ts` file:
1111

12-
```js
13-
// app.config.js
12+
```ts
13+
// vite.config.ts
1414

15-
import { defineConfig } from '@tanstack/react-start/config'
15+
import { tanstackStart } from '@tanstack/react-start/plugin/vite'
1616

1717
export default defineConfig({
18-
server: {
19-
prerender: {
20-
routes: ['/'],
21-
crawlLinks: true,
22-
},
23-
},
18+
plugins: [
19+
tanstackStart({
20+
prerender: {
21+
// Enable prerendering
22+
enabled: true,
23+
},
24+
// Optional configuration for specific pages—without this, it will still automatically
25+
// prerender all pages
26+
pages: [
27+
{
28+
path: '/my-page',
29+
// By default, html files will be named the the same as their route, so `my-page.tsx` would become
30+
// `/my-page.html`. However, in order to have a URL path like `/my-page`, it should be an index
31+
// inside a directory like so:
32+
prerender: { enabled: true, outputPath: '/my-page/index.html' },
33+
},
34+
],
35+
}),
36+
],
2437
})
2538
```

packages/start-plugin-core/src/nitro-plugin/plugin.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,15 @@ async function buildNitroApp(
132132

133133
// If the user has not set a prerender option, we need to set it to true
134134
// if the pages array is not empty and has sub options requiring for prerendering
135+
// If the user has explicitly set prerender.enabled, this should be respected
135136
if (options.prerender?.enabled !== false) {
136137
options.prerender = {
137138
...options.prerender,
138-
enabled: options.pages.some((d) =>
139-
typeof d === 'string' ? false : !!d.prerender?.enabled,
140-
),
139+
enabled:
140+
options.prerender?.enabled ??
141+
options.pages.some((d) =>
142+
typeof d === 'string' ? false : !!d.prerender?.enabled,
143+
),
141144
}
142145
}
143146

0 commit comments

Comments
 (0)