fix(vercel): run edge middleware when isr is enabled - #17687
Open
asmyshlyaev177 wants to merge 4 commits into
Open
fix(vercel): run edge middleware when isr is enabled#17687asmyshlyaev177 wants to merge 4 commits into
asmyshlyaev177 wants to merge 4 commits into
Conversation
With `isr` set, every on-demand route's `dest` pointed at the ISR function. The middleware edge function was still built and deployed, but nothing routed to it, so `middlewareMode: 'edge'` was silently inert: middleware only ran inside the ISR function, and ISR skips that function entirely on a cache hit. Point those routes at the middleware function instead, and have the generated `next()` forward to `_isr` for routes the ISR function backs, so cached responses are still served. Routes matched by `isr.exclude` keep forwarding to `_render`, and `_image` / `_server-islands` stay on the serverless function as before.
🦋 Changeset detectedLatest commit: d9b0c39 The changes in this PR will be included in the next version bump. This PR includes changesets to release 23 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changes
With
isrenabled,middlewareMode: 'edge'is silently inert.The adapter builds and deploys
_middleware.func, but every on-demand route'sdestis set to the ISR function, so nothing ever routes to it. Middlewarestill runs — but only inside the ISR function, which Vercel skips entirely on a
cache hit. The observable result is middleware that works on a cold entry and
then stops running once the entry is warm, with no error and no log.
This is the ordering problem: middleware has to run before the cache is
consulted, not behind it.
_middlewarerather than_isrwhen a middlewareentry point exists, so the edge function is actually reached.
so
next()forwards to/_isr?x_astro_path=…for a route the ISR functionbacks, and
/_renderfor one it doesn't. Cached responses are still servedfrom cache; only the entry point moves.
_imageand_server-islandskeep going straight to_render, unchanged.isr.excludestill resolve to_renderthroughnext().x_astro_pathcarries the original pathname, so the ISR cache key stays therequest path rather than collapsing to
/_isr;x_astro_path_tokenis thebuild token added in #17370. Without
isr, or without a middleware file,nothing about the output changes.
Testing
New:
packages/integrations/vercel/test/isr-edge-middleware.test.ts— 16 testsover two fixtures.
isr-with-edge-middlewareasserts against the real build output, since the bugis entirely a property of the emitted
config.json:_middleware_imageand_server-islandsstill resolve to_renderredirectsstill resolve ahead of the middlewareexpirationsurviveand then imports the generated
middleware.mjswithfetchstubbed, to checkwhere
next()actually forwards:/_isr, withx_astro_pathand a token/_isr?x_astro_path=/cached/42, the real path, because thatpath is the cache key
isr.excluderoute →/_renderx_astro_pathisr-edge-no-middlewarecoversmiddlewareMode: 'edge'with no middleware filepresent: routes go straight to
_isras before and no middleware function isbuilt.
Verified as a regression guard by reverting
src/and re-running: 4 of the 16fail, all of them on
dest.Also ran the full suite — core unit (3307), core integration (1240), all 18
integration packages including
@astrojs/vercel(58), language-tools (98), ande2e in chrome and firefox. No new failures; the pre-existing ones
(
test/fonts.test.tscancellations, a handful of e2e) don't touch the adapter.One caveat worth stating plainly: this is verified against build output and the
generated module, not against a live Vercel deployment. Confirmation on a real
project would be welcome.