Skip to content
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

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

Closed
Rich-Harris opened this issue Mar 24, 2023 · 5 comments · Fixed by #9571
Closed

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

Rich-Harris opened this issue Mar 24, 2023 · 5 comments · Fixed by #9571
Assignees
Labels
feature request New feature or request
Milestone

Comments

@Rich-Harris
Copy link
Member

Rich-Harris commented Mar 24, 2023

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.

@Rich-Harris Rich-Harris added this to the non-urgent milestone Mar 24, 2023
@Rich-Harris Rich-Harris added the feature request New feature or request label Mar 24, 2023
@rgglez

This comment was marked as off-topic.

@PuruVJ
Copy link
Contributor

PuruVJ commented Mar 30, 2023

THIS!! Really needed this for a while now 🧡

One question though: Why is the return type Array<{ slug: string }>? Could just be Array<string>? Unless you plan to provide other options in the returning array... 🤔

@elliott-with-the-longest-name-on-github
Copy link
Contributor

@PuruVJ

We can provide type-safety for the params you return -- i.e. if you're on /[year]/[month]/[[day]] we can enforce that the type you return is Array<{ year: string, month: string, day?: string }>. This provides a great deal more safety than just returning a string does! If you want to provide an Array<string>, though, the top-level config option still remains.

@elliott-with-the-longest-name-on-github

This comment was marked as off-topic.

@madeleineostoja
Copy link

LOVE this idea, the crawler is great as a default but relying on it solely has been a footgun a few times for me. For example something as simple as building a blog — if there aren’t any posts yet sveltekit errors out saying the page can’t be pre-rendered because it’s not linked to

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants