Skip to content

Guard setFetchHandler call in non-runnable dev entrypoint to fix sessions + middleware + Cloudflare adapter in dev mode - #17936

Merged
matthewp merged 2 commits into
mainfrom
factory/fix-17927
Sep 8, 2026
Merged

matthewp merged 2 commits into
mainfrom
factory/fix-17927

Conversation

@astro-factory

@astro-factory astro-factory Bot commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

Changes

  • Fixes a dev-mode-only error ([unstorage] [cloudflare] Invalid binding SESSION: undefined) that occurred when using Astro sessions with middleware and the Cloudflare adapter. The non-runnable dev entrypoint (virtual/dev.ts) was unconditionally calling app.setFetchHandler(fetchable), which caused BaseApp.render() to take the custom-handler code path and resolve the manifest via getAmbientManifest() instead of the app's own manifest — the one that injectSessionBinding() had mutated with the real KV binding.
  • Guards the setFetchHandler call with the isDefaultFetchHandler flag (already exported by virtual:astro:fetchable), matching the existing pattern in handle-request.ts. When no user src/fetch.ts exists, the BaseApp constructor's own DefaultFetchHandler is preserved, its instanceof check passes, and the correct manifest is used for session binding resolution. Also eliminates the spurious warning about a missing custom src/fetch.ts.

Testing

  • Adds a new "sessions in dev mode with middleware" suite in packages/integrations/cloudflare/test/sessions.test.ts covering the exact failure scenario: Cloudflare adapter + sessions + middleware in dev mode returning a 200 with a valid session cookie instead of a 500.

Docs

  • No docs update needed; this restores already-documented behavior that regressed in dev mode.

Closes #17927

…eware is present (#17927)

Guard the `setFetchHandler` call in the non-runnable dev entrypoint with
the `isDefaultFetchHandler` flag, matching the existing guard in
`handle-request.ts`. In the workerd dev environment the Vite optimizer
can place `DefaultFetchHandler` in a separate chunk, producing a
different class identity that defeats the `instanceof` fast-path in
`BaseApp.render`. This caused the custom-handler code path to resolve a
different manifest than the one the Cloudflare adapter injected the KV
session binding into, breaking sessions when middleware was present.
@astro-factory astro-factory Bot added the fix verified Reporter confirmed the triage bot fix works label Sep 8, 2026
@changeset-bot

changeset-bot Bot commented Sep 8, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 20f6795

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

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 pkg: integration Related to any renderer integration (scope) pkg: astro Related to the core `astro` package (scope) labels Sep 8, 2026
@codspeed

codspeed Bot commented Sep 8, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 21 untouched benchmarks


Comparing factory/fix-17927 (20f6795) with main (c1a6a89)

Open in CodSpeed

@matthewp
matthewp merged commit 4b92ddc into main Sep 8, 2026
29 checks passed
@matthewp
matthewp deleted the factory/fix-17927 branch September 8, 2026 14:42
@astrobot-houston astrobot-houston mentioned this pull request Sep 8, 2026
dadezzz pushed a commit to dadezzz/university_notes that referenced this pull request Sep 12, 2026
This PR contains the following updates:

| Package | Change | [Age](https://docs.renovatebot.com/merge-confidence/) | [Confidence](https://docs.renovatebot.com/merge-confidence/) |
|---|---|---|---|
| [@astrojs/markdown-remark](https://astro.build) ([source](https://github.com/withastro/astro/tree/HEAD/packages/markdown/remark)) | [`7.3.0` → `7.3.1`](https://renovatebot.com/diffs/npm/@astrojs%2fmarkdown-remark/7.3.0/7.3.1) | ![age](https://developer.mend.io/api/mc/badges/age/npm/@astrojs%2fmarkdown-remark/7.3.1?slim=true) | ![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/@astrojs%2fmarkdown-remark/7.3.0/7.3.1?slim=true) |
| [astro](https://astro.build) ([source](https://github.com/withastro/astro/tree/HEAD/packages/astro)) | [`7.3.1` → `7.3.2`](https://renovatebot.com/diffs/npm/astro/7.3.1/7.3.2) | ![age](https://developer.mend.io/api/mc/badges/age/npm/astro/7.3.2?slim=true) | ![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/astro/7.3.1/7.3.2?slim=true) |

---

### Release Notes

<details>
<summary>withastro/astro (@&#8203;astrojs/markdown-remark)</summary>

### [`v7.3.1`](https://github.com/withastro/astro/blob/HEAD/packages/markdown/remark/CHANGELOG.md#731)

[Compare Source](https://github.com/withastro/astro/compare/@astrojs/markdown-remark@7.3.0...@astrojs/markdown-remark@7.3.1)

##### Patch Changes

- [#&#8203;17896](withastro/astro#17896) [`a548223`](withastro/astro@a548223) Thanks [@&#8203;matthewp](https://github.com/matthewp)! - Fixes `<script>`/`<style>` rendering in MDX so that only literal content (including content injected by remark/rehype plugins) is treated as trusted markup. A dynamic value passed as a `<script>`/`<style>` child (e.g. `<script>{value}</script>`) is now escaped like any other element's content instead of being rendered raw. Use `set:html` to explicitly opt a dynamic value back into raw rendering.

</details>

<details>
<summary>withastro/astro (astro)</summary>

### [`v7.3.2`](https://github.com/withastro/astro/blob/HEAD/packages/astro/CHANGELOG.md#732)

[Compare Source](https://github.com/withastro/astro/compare/astro@7.3.1...astro@7.3.2)

##### Patch Changes

- [#&#8203;17896](withastro/astro#17896) [`a548223`](withastro/astro@a548223) Thanks [@&#8203;matthewp](https://github.com/matthewp)! - Fixes `<script>`/`<style>` rendering in MDX so that only literal content (including content injected by remark/rehype plugins) is treated as trusted markup. A dynamic value passed as a `<script>`/`<style>` child (e.g. `<script>{value}</script>`) is now escaped like any other element's content instead of being rendered raw. Use `set:html` to explicitly opt a dynamic value back into raw rendering.

- [#&#8203;17931](withastro/astro#17931) [`c1a6a89`](withastro/astro@c1a6a89) Thanks [@&#8203;astro-factory](https://github.com/apps/astro-factory)! - Fixes the dev toolbar returning a 504 "Outdated Optimize Dep" error when a workspace-linked package imports a dependency that Vite's initial scan did not discover

- [#&#8203;17908](withastro/astro#17908) [`42e9188`](withastro/astro@42e9188) Thanks [@&#8203;astro-factory](https://github.com/apps/astro-factory)! - Fixes i18n fallback routing replacing the first substring match instead of the actual locale segment, which mangled paths like `/energy/en/about` into `/esergy/en/about`

- [#&#8203;17936](withastro/astro#17936) [`4b92ddc`](withastro/astro@4b92ddc) Thanks [@&#8203;astro-factory](https://github.com/apps/astro-factory)! - Fixes sessions breaking in dev mode with the Cloudflare adapter when middleware is present

- Updated dependencies \[[`a548223`](withastro/astro@a548223)]:
  - [@&#8203;astrojs/markdown-satteri](https://github.com/astrojs/markdown-satteri)@0.4.1

</details>

---

### Configuration

📅 **Schedule**: (UTC)

- Branch creation
  - At any time (no schedule defined)
- Automerge
  - At any time (no schedule defined)

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

👻 **Immortal**: This PR will be recreated if closed unmerged. Get [config help](https://github.com/renovatebot/renovate/discussions) if that's undesired.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Mend Renovate CLI](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0NC43OS42IiwidXBkYXRlZEluVmVyIjoiNDQuNzkuNiIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOltdfQ==-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix verified Reporter confirmed the triage bot fix works pkg: astro Related to the core `astro` package (scope) pkg: integration Related to any renderer integration (scope)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Middleware and Session break SSR with Cloudflare adapter

1 participant