SvelteKit (v2) - Prerender Dynamic Paths for Static Site Generation
Posted on Jan 3, 2024 in Blog Posts
At the time of writing:
-
svelte
's version is4.2.8
-
@sveltejs/kit
's version is2.0.6
-
@sveltejs/adapter-static
's version is3.0.1
.
Overview
In this post, we'll explore real-world examples for using entries to prerender dynamic paths when using the static adapter for static site generation (SSG) in SvelteKit . This site is deployed to AWS S3 as a static site, and so I've learned a few tricks along the way.
SvelteKit using the static adapter will automatically generate any non-dynamic paths and dynamic paths it finds via crawling links on the site from the root route. This covers a lot of use cases, especially if your site links to all of the dynamic pages.
If your site doesn't link to all of the dynamic pages though, SvelteKit doesn't know those pages exist or that it needs to prerender them.
Prerender Methods
There are configurations to direct SvelteKit to prerender for dynamic path variables, and it offers two methods to do so.
Kit Config Prerender Entries
In the svelte.config.js
file, SvelteKit can be directed to a list of pages to prerender or start crawling from to prerender.1 This by default is set to ["*"]
, which means SvelteKit will prerender any routes that don't require parameters. It doesn't prerender routes with parameters because it doesn't know what those parameters should be. Any optional parameters are evaluated as being undefined.
If there are only a few dynamic path pages to prerender, or there are pages that link to all the dynamic path pages to prerender, listing them all in the config can be a good option.
If there are a lot of dynamic paths to prerender, the next option may be a better fit.
Page Options Entries Generator Function
Alternatively, a list of pages to prerender can be provided via the page options entries
export, which should be an entry generator function. 2
Entry generator functions can be synchronous or asynchronous and should return an array of entry objects, or return a promise of array of entry objects.
Each entry object should contain the parameters to generate the page.
For route /post/[slug]/[lng]
, the entry object would look like:
The entry generator function can be anything that returns an array of entry objects, or a promise of the same.
For example, if there was a route /post/[slug]
, the returned objects in the array would look like {slug: "hello-world"}
to cover the dynamic path variable.
Typescript types are available to +page.ts
, +page.server.ts
, or +server.ts
by importing EntryGenerator
from ./$types
.
As another example, let's look at a route /[lng]/post/[slug]
. SvelteKit needs to know about both the lng
and the slug
for this one to cover all the content.
Entry Generation Strategies
Arrays or Objects
On this site, there is a category map with slugs, names, etc. It looks like this:
If we have a route like /posts/[category]
, we could use the postCategorySlugsArray
directly.
If we have a route like [lng]/posts/[category]
, we can use an entry generator function to map the array to prerender entries.
File Paths
If the site is something like a blog using markdown files for content, you may need to generate a path for each file.
Suppose there is a route /post/[slug]
to render individual posts, and each slug corresponds to a markdown file of the same name in the posts
directory.
We can create an entries generator that reads each file name and formats it so it doesn't include the file extension.