Skip to content

Commit fcb442a

Browse files
author
Rich Harris
committed
add docs
1 parent 067b309 commit fcb442a

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

documentation/docs/20-core-concepts/40-page-options.md

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export const prerender = 'auto';
3333

3434
> If your entire app is suitable for prerendering, you can use [`adapter-static`](https://github.com/sveltejs/kit/tree/master/packages/adapter-static), which will output files suitable for use with any static webserver.
3535
36-
The prerenderer will start at the root of your app and generate files for any prerenderable pages or `+server.js` routes it finds. Each page is scanned for `<a>` elements that point to other pages that are candidates for prerendering — because of this, you generally don't need to specify which pages should be accessed. If you _do_ need to specify which pages should be accessed by the prerenderer, you can do so with the `entries` option in the [prerender configuration](configuration#prerender), or by exporting an entry generator from your dynamic route.
36+
The prerenderer will start at the root of your app and generate files for any prerenderable pages or `+server.js` routes it finds. Each page is scanned for `<a>` elements that point to other pages that are candidates for prerendering — because of this, you generally don't need to specify which pages should be accessed. If you _do_ need to specify which pages should be accessed by the prerenderer, you can do so with [`config.kit.prerender.entries`](configuration#prerender), or by exporting an [`entries`](#entries) function from your dynamic route.
3737

3838
While prerendering, the value of `building` imported from [`$app/environment`](modules#$app-environment) will be `true`.
3939

@@ -84,9 +84,40 @@ If you encounter an error like 'The following routes were marked as prerenderabl
8484

8585
Since these routes cannot be dynamically server-rendered, this will cause errors when people try to access the route in question. There are two ways to fix it:
8686

87-
* Ensure that SvelteKit can find the route by following links from [`config.kit.prerender.entries`](configuration#prerender). Add links to dynamic routes (i.e. pages with `[parameters]` ) to this option if they are not found through crawling the other entry points, else they are not prerendered because SvelteKit doesn't know what value the parameters should have. Pages not marked as prerenderable will be ignored and their links to other pages will not be crawled, even if some of them would be prerenderable.
87+
* Ensure that SvelteKit can find the route by following links from [`config.kit.prerender.entries`](configuration#prerender) or the [`entries`](#entries) page option. Add links to dynamic routes (i.e. pages with `[parameters]` ) to this option if they are not found through crawling the other entry points, else they are not prerendered because SvelteKit doesn't know what value the parameters should have. Pages not marked as prerenderable will be ignored and their links to other pages will not be crawled, even if some of them would be prerenderable.
8888
* Change `export const prerender = true` to `export const prerender = 'auto'`. Routes with `'auto'` can be dynamically server rendered
8989

90+
## entries
91+
92+
SvelteKit will discover pages to prerender automatically, by starting at _entry points_ and crawling them. By default, all your non-dynamic routes are considered entry points — for example, if you have these routes...
93+
94+
```bash
95+
/ # non-dynamic
96+
/blog # non-dynamic
97+
/blog/[slug] # dynamic, because of `[slug]`
98+
```
99+
100+
...SvelteKit will prerender `/` and `/blog`, and in the process discover links like `<a href="/blog/hello-world">` which give it new pages to prerender.
101+
102+
Most of the time, that's enough. In some situations, links to pages like `/blog/hello-world` might not exist (or might not exist on prerendered pages), in which case we need to tell SvelteKit about their existence.
103+
104+
This can be done with [`config.kit.prerender.entries`](configuration#prerender), or by exporting an `entries` function from a `+page.js` or `+page.server.js` belonging to a dynamic route:
105+
106+
```js
107+
/// file: src/routes/blog/[slug]/+page.server.js
108+
/** @type {import('./$types').EntryGenerator} */
109+
export function entries() {
110+
return [
111+
{ slug: 'hello-world' },
112+
{ slug: 'another-blog-post' }
113+
];
114+
}
115+
116+
export const prerender = true;
117+
```
118+
119+
`entries` can be an `async` function, allowing you to (for example) retrieve a list of posts from a CMS or database, in the example above.
120+
90121
## ssr
91122

92123
Normally, SvelteKit renders your page on the server first and sends that HTML to the client where it's [hydrated](glossary#hydration). If you set `ssr` to `false`, it renders an empty 'shell' page instead. This is useful if your page is unable to be rendered on the server (because you use browser-only globals like `document` for example), but in most situations it's not recommended ([see appendix](glossary#ssr)).

sites/kit.svelte.dev/src/lib/docs/server/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ export async function read_file(file) {
179179
`export type LayoutServerLoad = Kit.ServerLoad<{${params}}>;`,
180180
`export type RequestHandler = Kit.RequestHandler<{${params}}>;`,
181181
`export type Action = Kit.Action<{${params}}>;`,
182-
`export type Actions = Kit.Actions<{${params}}>;`
182+
`export type Actions = Kit.Actions<{${params}}>;`,
183+
`export type EntryGenerator = () => Promise<Array<{${params}}>> | Array<{${params}}>;`
183184
);
184185
}
185186

0 commit comments

Comments
 (0)