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

feat: route-level entry generators #9571

Merged
merged 23 commits into from May 4, 2023
Merged

Conversation

tcc-sejohnson
Copy link
Contributor

@tcc-sejohnson tcc-sejohnson commented Mar 31, 2023

Closes #9506

This allows exporting an entries function from +page, +page.server, and +server files:

// /[date]/[...file]/+page.server.ts
export const entries = () => {
  return [
    { date: '2022-01-01', file: 'some/path.docx' },
    { date: '2022-02-01', file: 'some/other/path.docx' }
  ]
}

This would cause /2022-01-01/some/path.docx and /2022-02-01/some/other/path.docx to be prerendered, regardless of whether they're reachable by the prerendering crawler.

To aid in debugging and help make this feature as footgun-less as possible, if an entries export creates an entry that matches a route other than its own, it will throw an error during prerendering. I added a config option to handle this similarly to how we handle other prerender errors.

Given the following two routes:

  • /[slug]/[otherSlug]
  • /[slug]/specific

If /[slug]/[otherSlug]/+page.server.ts exports the following:

export const entries: EntryGenerator = () => {
  return [{ slug: 'foo', otherSlug: 'specific' }]
}

This would throw a prerendering error with a descriptive message, because the entry resolves to /foo/specific, which matches [slug]/specific instead of /[slug]/[otherSlug].

TODO:

  • Should we throw an error or something if routes generated from entries are duplicated? Should we just dedup them?
  • Add typing support -- the return type should respect required and optional params
  • Make sure typing and functionality works for export from +server.ts
  • Tests
  • Docs
  • Build auto-typing into the language server
  • Fix the "You can't use this export" error my editor is still giving me???

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

@changeset-bot
Copy link

changeset-bot bot commented Mar 31, 2023

🦋 Changeset detected

Latest commit: fcb442a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@benmccann benmccann changed the title feat: Route-level entry generators feat: route-level entry generators Apr 2, 2023
Copy link
Member

@Rich-Harris Rich-Harris left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks great, i just had one minor piece of feedback about naming. i think we probably need to ship the type updates first though, otherwise people will get yelled at if they try and use this

packages/kit/src/utils/routing.js Outdated Show resolved Hide resolved
packages/kit/src/utils/routing.js Outdated Show resolved Hide resolved
Co-authored-by: Rich Harris <richard.a.harris@gmail.com>
@@ -33,7 +33,7 @@ export const prerender = 'auto';

> 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.

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).
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's too little to add to the docs for this feature. We need some place to explain it properly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree -- I called this out in the PR description. I wasn't sure where we'd want to put this, though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably in page options at the very least, and maybe also reference it in some of the prerender-related docs.

dummdidumm pushed a commit to sveltejs/language-tools that referenced this pull request Apr 28, 2023
@Rich-Harris Rich-Harris merged commit 7a92413 into master May 4, 2023
13 checks passed
@Rich-Harris Rich-Harris deleted the elliott/route-level-entries branch May 4, 2023 03:41
@github-actions github-actions bot mentioned this pull request May 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Specify which pages to prerender without relying on the crawler
4 participants