fix: run reroute in a middleware function before split functions - #16590
fix: run reroute in a middleware function before split functions#16590teemingc wants to merge 42 commits into
reroute in a middleware function before split functions#16590Conversation
|
Install the latest version of pnpm add https://pkg.svelte.dev/@sveltejs/kit/c/29e21be8e7c7f23875442bb75ba1bb8050b5bd3cOpen in |
🦋 Changeset detectedLatest commit: 29e21be The changes in this PR will be included in the next version bump. This PR includes changesets to release 3 packages
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 |
Co-authored-by: vercel[bot] <35613825+vercel[bot]@users.noreply.github.com>
Co-authored-by: Tee Ming <chewteeming01@gmail.com>
| let new_request; | ||
|
|
||
| // `reroute` hooks receive pathnames, so they must not run for route-ID resolution requests | ||
| if (remote_id || is_route_id_resolution_request) { |
There was a problem hiding this comment.
Don't really like that we have to duplicate the "should run reroute" check logic between here and respond.js but I'm not really sure what's the best way to dedupe it right now
| /** | ||
| * @param {URL} url | ||
| */ | ||
| export function get_remote_id(url) { |
There was a problem hiding this comment.
Had to move this to a less packed module. Right now Rolldown can't treeshake anything that dynamically imports a module. This causes things like await import('otel') or await import('node:async_hooks') in any module to completely break treeshaking. We don't want otel bundled into the reroute middleware.
| new_request = new Request(request); | ||
| } else { | ||
| const { url: normalized_url, denormalize } = normalizeUrl(url); | ||
| const resolved_path = await reroute({ url: normalized_url, fetch }); |
There was a problem hiding this comment.
Unfortunately I don't think this is right — normally fetch would be the internal fetch implementation that handles root-relative paths and applies handleFetch (and forwards cookies etc)
There was a problem hiding this comment.
Ugh, you're right and that complicates things... I wonder if we take a different approach and try to handle this in the catch-all serverless function instead.
…16665) alternative to #16590 fixes #11879 This PR makes the assumption that a catch-all serverless function is deployed with no routes in its manifest (the alternative was an explicit flag). It then exports a helper which should be called in such a serverless function after `server.respond` so that it can invoke the correct handler instead of simply returning a 404 since it has no route information in its SSR manifest. The advantage of this approach compared to deploying a middleware that only runs reroute is that it's simpler implementation-wise and it allows the user's `handleFetch`, `init` hooks, etc. to run correctly without any additional effort from us. The disadvantages are that: - `reroute` will run twice: once in the catch-all function then again in the correct function. We _could_ choose to skip the second `reroute` based on some `header` value or context (if any) that can be passed between serverless functions. Otherwise, I chose to omit this so that the `reroute` hook can't be skipped from outside influence (someone setting the header themselves without running `reroute` first) - Slightly bigger serverless function bundle than a middleware which just runs `reroute`. Not that big of an issue I think... --- ### Please don't delete this checklist! Before submitting the PR, please make sure you do the following: - [x] 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 - [x] This message body should clearly illustrate what problems it solves. - [x] 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 - [x] 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:`. ### Edits - [x] Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed. --------- Co-authored-by: vercel[bot] <35613825+vercel[bot]@users.noreply.github.com> Co-authored-by: Rich Harris <richard.a.harris@gmail.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
fixes #11879
See #12296 for context
This PR adds a middleware that runs
reroutebeforehand so that, when there are multiple functions, the correct one is invoked instead of not matching any function matchers.The Vercel middleware uses a simplified version of the rewrite helper from
@vercel/functionsto avoid pulling in the whole bundle.rewritedocs - https://vercel.com/docs/functions/edge-middleware/middleware-api#rewritesrewriteimplementation - https://github.com/vercel/vercel/blob/cefda60a603d60cc35e4697c36e751cca411e6bb/packages/functions/src/middleware.ts#L101The Netlify middleware resolves the URL then passes it to the next handler.
There's currently an issue with Vercel rewrites discarding the SvelteKit form action in the URL because the query parameter doesn't have a value vercel/vercel#12902
TODOS
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 checkChangesets
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