Skip to content

fix(vercel): run edge middleware when isr is enabled - #17687

Merged
ematipico merged 10 commits into
withastro:mainfrom
asmyshlyaev177:isr_vercel_middleware_cache
Aug 19, 2026
Merged

ematipico merged 10 commits into
withastro:mainfrom
asmyshlyaev177:isr_vercel_middleware_cache

Conversation

@asmyshlyaev177

@asmyshlyaev177 asmyshlyaev177 commented Aug 13, 2026 •

Copy link
Copy Markdown
Contributor

Changes

With isr enabled, middlewareMode: 'edge' is silently inert.

The adapter builds and deploys _middleware.func, but every on-demand route's
dest is set to the ISR function, so nothing ever routes to it. Middleware
still 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.

  • Route on-demand pages at _middleware rather than _isr when a middleware
    entry point exists, so the edge function is actually reached.
  • Collect those route patterns and inline them into the generated middleware,
    so next() forwards to /_isr?x_astro_path=… for a route the ISR function
    backs, and /_render for one it doesn't. Cached responses are still served
    from cache; only the entry point moves.
  • _image and _server-islands keep going straight to _render, unchanged.
  • Routes matched by isr.exclude still resolve to _render through next().
  • Prerendered pages are untouched: no route entry, served as static files.

x_astro_path carries the original pathname, so the ISR cache key stays the
request path rather than collapsing to /_isr; x_astro_path_token is the
build 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 tests
over two fixtures.

isr-with-edge-middleware asserts against the real build output, since the bug
is entirely a property of the emitted config.json:

  • pages, dynamic routes, endpoints and the 404 resolve to _middleware
  • _image and _server-islands still resolve to _render
  • prerendered pages get no route entry and ship as static HTML
  • configured redirects still resolve ahead of the middleware
  • the ISR prerender config and its expiration survive

and then imports the generated middleware.mjs with fetch stubbed, to check
where next() actually forwards:

  • a cached route → /_isr, with x_astro_path and a token
  • a dynamic route → /_isr?x_astro_path=/cached/42, the real path, because that
    path is the cache key
  • an isr.exclude route → /_render
  • a query string does not leak into x_astro_path
  • the response still reaches the middleware, headers intact

isr-edge-no-middleware covers middlewareMode: 'edge' with no middleware file
present: routes go straight to _isr as before and no middleware function is
built.

Verified as a regression guard by reverting src/ and re-running: 4 of the 16
fail, 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), and
e2e in chrome and firefox. No new failures; the pre-existing ones
(test/fonts.test.ts cancellations, 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/

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

changeset-bot Bot commented Aug 13, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: f1d38ae

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

This PR includes changesets to release 24 packages
Name Type
@astrojs/vercel Patch
@test/astro-vercel-basic Patch
@test/astro-vercel-image Patch
@test/astro-vercel-integration-assets Patch
@test/vercel-isr-edge-no-middleware Patch
@test/vercel-isr-with-edge-middleware Patch
@test/vercel-isr Patch
@test/vercel-max-duration Patch
@test/vercel-edge-middleware-with-edge-file Patch
@test/vercel-edge-middleware-without-edge-file Patch
@test/astro-vercel-no-output Patch
@test/astro-vercel-prerendered-error-pages Patch
@test/astro-vercel-redirects-serverless Patch
@test/astro-vercel-redirects Patch
@test/vercel-server-islands-static Patch
@test/vercel-server-islands Patch
@test/astro-vercel-serverless-prerender Patch
@test/astro-vercel-serverless-with-dynamic-routes Patch
@test/astro-vercel-static-assets Patch
@test/vercel-static-headers Patch
@test/astro-vercel-static Patch
@test/vercel-streaming Patch
@test/astro-vercel-with-web-analytics-enabled-output-as-static Patch
vercel-hosted-astro-project Patch

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

@github-actions github-actions Bot added the pkg: integration Related to any renderer integration (scope) label Aug 13, 2026
@ematipico

Copy link
Copy Markdown
Member

@asmyshlyaev177 can you please file an issue first?

@asmyshlyaev177

Copy link
Copy Markdown
Contributor Author

@asmyshlyaev177 can you please file an issue first?

Sure, will do in 30 minutes.

@asmyshlyaev177

Copy link
Copy Markdown
Contributor Author

@ematipico ematipico added the pr: astro-review Triggers a bot to do an automated review label Aug 18, 2026
@astro-factory astro-factory Bot removed the pr: astro-review Triggers a bot to do an automated review label Aug 18, 2026

@astro-factory astro-factory Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

@asmyshlyaev177

Copy link
Copy Markdown
Contributor Author

@ematipico could you ping the bot to review it again? Tnx.

@matthewp matthewp added the pr: astro-review Triggers a bot to do an automated review label Aug 18, 2026
@astro-factory astro-factory Bot removed the pr: astro-review Triggers a bot to do an automated review label Aug 18, 2026

@astro-factory astro-factory Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Findings

No findings.

Merge Requirement Assessment

Status: satisfied

  • Run edge middleware before the ISR cache when middlewareMode: 'edge' and isr are both enabled — The adapter now routes on-demand pages to _middleware instead of _isr when a middleware entry point exists, and the generated edge middleware forwards next() to /_isr?x_astro_path=…&x_astro_path_token=… for ISR-backed routes and to /_render otherwise. This is covered by the new isr-with-edge-middleware fixture and its assertions on both config.json route dests and the runtime forwarding logic of the built middleware.mjs.
  • Preserve ISR behavior for isr.exclude, internal endpoints, and prerendered pages — isr.exclude paths are added to the middleware’s excluded-pattern list (checked first) and still forwarded to /_render. _image and _server-islands continue 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.ts now parses the trusted x-astro-path header with URL so 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-middleware fixture asserts that routes still point directly at /_isr and no _middleware.func is built.
  • Changeset — .changeset/isr-edge-middleware.md is present and correctly scoped as a @astrojs/vercel patch.

Questions

  • Several tests in packages/integrations/vercel/test/isr-edge-middleware.test.ts replace and restore globalThis.fetch. If astro-scripts test runs tests concurrently (the node:test default), 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 ematipico left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Changeset must be reworded, otherwise all good

Comment thread .changeset/isr-edge-middleware.md Outdated
Comment on lines +5 to +11
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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated, hope it's better.

@ematipico ematipico left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you! 🙌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg: integration Related to any renderer integration (scope)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants