fix: skip calling respond for server-side fetch on prerendered pages - #13377
Conversation
When using server-side fetch for internal requests, if a server route is matched from the server `manifest.js`, it gets called without making a real HTTP request. However, prerendered routes are not included on this list! This is fine when routes are prerendered with `export const prerender = true;`, but will cause issues with a non-prerendered route also matches the same URL as any prerendered routes. This commit adds `prerendered_routes: Set<string>` to the `manifest.js`, which skips calling the non-prerendered route.
Doing a server-side `fetch()` (e.g. `isSubRequest: true`) to a route that is prerendered runs `hooks.server.js`, which is different behaviour from a normal `fetch()`.
🦋 Changeset detectedLatest commit: 4b54db0 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
|
Confirmed this also fixes #12778 Re the trailing slash problematics: that could indeed be problematic in the sense of you would get a false negative if you request |
|
Thanks for handling the case where there is a trailing slash (and passing an array is a lot simpler!).
I was thinking of the same edge case 😆. I suspect it might happen due to a bug in somebody's code, but in the worst case, we're just adding the overhead of an extra HTTP call, so I doubt anybody would complain. |
Server-side
fetch()(e.g. whenisSubRequest: true) currently has a few bugs when called on aprerenderedroute.export const prerender = 'auto')hooks.server.jsrun on them, but only whenisSubRequest: trueI've fixed both of these issues by adding a
prerendered_routes: Set<string>to the manifest. Then, we instantly call a real HTTPfetch()if the path matches, just like when calling server-sidefetch()on assets (when the adapter doesn't support reading assets from the file-system).During dev/build, there are no
prerendered_routes, so it's left empty in the manifest (although that does mean themanifest-full.jsalso excludes it!)Fixes #12778
Fixes #12739
Potential issues
prerendered.paths(which I'm using to setprerendered_routes), it only has the routes without a trailing slash. Would this be an issue? If so, the code might get a lot more complicated!kit/packages/kit/src/types/private.d.ts
Lines 189 to 190 in 873a09e
prerendered_redirects, so that we can follow the redirect chain without having to make a real HTTP request, but I think that should be fine if this is under the private_field in themanifest.jsfile.Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
Tests
pnpm testand lint the project withpnpm lintandpnpm checkpnpm testfails onmainlocally for me, so let's see if the CI passes on it!Changesets
pnpm changesetand following the prompts. Changesets that add features should beminorand those that fix bugs should bepatch. Please prefix changeset messages withfeat:,fix:, orchore:.Edits