Skip to content

Specify which pages to prerender without relying on the crawler #9506

@Rich-Harris

Description

@Rich-Harris

Describe the problem

By default, SvelteKit will find pages to prerender by starting at every parameter-less route (we call these entries), prerendering them (if possible), and crawling the resulting HTML to find new links.

You can specify new entries in svelte.config.js if you have pages that need to be prerendered but which are not, for whatever reason, findable by following <a> tags.

This works, but the ergonomics aren't super great, and there's no type safety.

Describe the proposed solution

Next has a couple of solutions to this — getStaticPaths for old apps, and generateStaticParams if you're using the App Router.

We could have our own version of generateStaticParams — for consistency with the configuration option, I'd propose entries:

// src/routes/blog/[slug]/+page.server.js
export async function entries() {
  const posts = await get_posts_from_cms();

  return posts.map((post) => {
    return { slug: post.slug };
  });
}

The return value could be typed (including automatically, thanks to https://svelte.dev/blog/zero-config-type-safety).

In entries was specified for a page, the resulting entries would be included in *, meaning that they would be included by default, but you would still have total control over which entries were considered:

export default {
  kit: {
    prerender: {
      // this is the default config, so can be omitted
      entries: ['*'] // include all parameter-less routes, plus pages that specify `entries`
    }
  }
};

export default {
  kit: {
    prerender: {
      entries: ['*', '/x/y/z'] // include all of the above _plus_ `/x/y/z`
    }
  }
};

export default {
  kit: {
    prerender: {
      entries: ['/x/y/z'] // only prerender `/x/y/z`
    }
  }
};

For consistency we should probably allow the same for +server.js files.

Metadata

Metadata

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions