Repository navigation
fix(vercel): run edge middleware when isr is enabled - #17687
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: f1d38ae The changes in this PR will be included in the next version bump. This PR includes changesets to release 24 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 |
|
@asmyshlyaev177 can you please file an issue first? |
Sure, will do in 30 minutes. |
Created an issue #17732 Couple of deployed versions before and after the fix: |
There was a problem hiding this comment.
Reviewed PR #17687 (withastro/astro, head 8ba1628 against base e28d227) statically.
Merge requirements from the PR description are implemented for the most part: on-demand ISR routes now point to _middleware when an edge middleware file exists; the generated middleware’s next() forwards cached routes to /_isr and excluded/unmatched routes to /_render; internal _image/_server-islands routes still bypass middleware; prerendered pages have no route entry; redirects remain ahead of the middleware in the route table; the ISR prerender config survives; and the no-middleware fixture behaves as before.
However, the new /_isr forward URL drops the client’s original query parameters, which breaks query-param usage on cached pages/endpoints under ISR + edge middleware. The test only checks that the query does not leak into x_astro_path, not that it is preserved, so this is a correctness gap.
Changeset is present (@astrojs/vercel patch). New fixtures use workspace:* dependencies and are included in pnpm-lock.yaml. The new test file matches the package’s test glob (test/**/!(hosted).test.ts).
Verdict: changes required.
Context: PR #17687 and its description.
Scope: full changed diff of PR #17687.
Changeset: present and covers @astrojs/vercel.
Limitations: static review only; no project code, tests, builds, or checks were run.
GitHub context: collected via the provided read-only PR tools.
This review was made by an LLM. The analysis may be wrong, and reports might be incorrect.
|
@ematipico could you ping the bot to review it again? Tnx. |
There was a problem hiding this comment.
Findings
No findings.
Merge Requirement Assessment
Status: satisfied
- Run edge middleware before the ISR cache when
middlewareMode: 'edge'andisrare both enabled — The adapter now routes on-demand pages to_middlewareinstead of_isrwhen a middleware entry point exists, and the generated edge middleware forwardsnext()to/_isr?x_astro_path=…&x_astro_path_token=…for ISR-backed routes and to/_renderotherwise. This is covered by the newisr-with-edge-middlewarefixture and its assertions on bothconfig.jsonroutedests and the runtime forwarding logic of the builtmiddleware.mjs. - Preserve ISR behavior for
isr.exclude, internal endpoints, and prerendered pages —isr.excludepaths are added to the middleware’s excluded-pattern list (checked first) and still forwarded to/_render._imageand_server-islandscontinue to bypass middleware and go straight to_render. Prerendered pages receive no route entry and are served as static files, as asserted by the tests. - Fix query-string loss when middleware forwards a request —
serverless/entrypoint.tsnow parses the trustedx-astro-pathheader withURLso the query string is preserved instead of being encoded into the pathname. The new tests cover query preservation for both middleware-header and route-rewrite paths. - Keep routing unchanged when there is no middleware file — The
isr-edge-no-middlewarefixture asserts that routes still point directly at/_isrand no_middleware.funcis built. - Changeset —
.changeset/isr-edge-middleware.mdis present and correctly scoped as a@astrojs/vercelpatch.
Questions
- Several tests in
packages/integrations/vercel/test/isr-edge-middleware.test.tsreplace and restoreglobalThis.fetch. Ifastro-scripts testruns tests concurrently (thenode:testdefault), those stubs could leak between concurrent tests and produce flaky failures. Is the test harness configured to run this file serially?
Review Status
Verdict: ready to merge based on static review
Context: PR #17687 plus its description
Scope: Full PR diff (base e28d22782bdc641261d0eca8ad00ba248a93d640, head bf1f7afed96586234f5851aa4f4ccb7d790c45a1)
Changeset: present and covers @astrojs/vercel
Limitations: none
Validation: Static review only; no project code, tests, builds, or checks were run.
GitHub context: collected
Git scope: PR base/head verified
This review was made by an LLM. The analysis may be wrong, and reports might be incorrect.
ematipico
left a comment
There was a problem hiding this comment.
Changeset must be reworded, otherwise all good
| Fixes `middlewareMode: 'edge'` being ignored when `isr` is enabled | ||
|
|
||
| The middleware edge function was built and deployed, but every route was pointed at the ISR function instead, so nothing invoked it. Middleware only ran inside the ISR function — which ISR skips entirely on a cache hit — meaning middleware silently stopped running once an entry was warm. | ||
|
|
||
| Routes backed by ISR now go through the middleware edge function first, and `next()` forwards to the ISR function so cached responses are still served. Routes listed in `isr.exclude` continue to forward to the serverless function. | ||
|
|
||
| Query strings are also no longer lost when middleware forwards a request: the entrypoint now keeps the query the middleware sends alongside the real path. |
There was a problem hiding this comment.
Can you reword the changeset so it follows our guidelines? Here some pointers https://contribute.docs.astro.build/docs-for-code-changes/changesets/#tips-and-examples
There was a problem hiding this comment.
Updated, hope it's better.
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.Reproduction
Repo with reproduction (2 branches) https://github.com/asmyshlyaev177/astro-isr-middleware-repro
And deployed versions:
https://astro-isr-middleware-repro.vercel.app/
https://astro-isr-mw-patched.vercel.app/