Skip to content

fix(astro): read Cloudflare env via cloudflare:workers, not removed locals.runtime.env#1558

Open
pauldiv wants to merge 1 commit into
Thinkmill:mainfrom
pauldiv:fix/cloudflare-locals-runtime-env
Open

fix(astro): read Cloudflare env via cloudflare:workers, not removed locals.runtime.env#1558
pauldiv wants to merge 1 commit into
Thinkmill:mainfrom
pauldiv:fix/cloudflare-locals-runtime-env

Conversation

@pauldiv

@pauldiv pauldiv commented Jul 15, 2026

Copy link
Copy Markdown

Summary

Fixes #1554. GitHub-mode API routes (/api/keystatic/github/*) crash with a 500 under @astrojs/cloudflare on Astro 6+ — Astro.locals.runtime.env was removed, and keystatic-astro-api.js still reads it.

Confirmed the crash both in astro dev and a real deployed Cloudflare Worker (via wrangler tail), and confirmed this fix resolves it in both, on our own project (Astro 7.0.9, @astrojs/cloudflare 14.1.3, wrangler 4.110.0).

What changed

packages/astro/src/api.tsx now reads the Cloudflare runtime env via a dynamic cloudflare:workers import — exactly what the removed API's own error message recommends — instead of context.locals.runtime.env. Falls back to the old shape (in its own try/catch, since that access itself throws on Astro v6+) for other adapters or older Astro versions, so the fix stays adapter-agnostic rather than hardcoding a Cloudflare-only assumption into a shared file.

Also flagged in the issue but not attempted here: the GitHub App creation wizard (/keystatic guided setup) has a separate, unrelated bug under this same adapter combo (module is not defined, React/workerd bundling) — didn't touch that in this PR, wanted to keep this one focused on the API crash.

Test plan

  • Reproduced the original crash against our own deployed Worker via wrangler tail
  • Applied the equivalent fix locally via patch-package first, confirmed it resolves the crash (replaced with a clean "missing credentials" error, then a working OAuth redirect once credentials were set) both in astro dev and the real deployment
  • Ported the fix to this repo's actual TypeScript source (not just the compiled output) and isolated-checked it with tsc for syntax/type correctness
  • Haven't run this repo's own test suite / full monorepo build — happy to address any feedback from CI

Added a changeset (patch bump for @keystatic/astro).

…ocals.runtime.env

Astro v6 removed Astro.locals.runtime.env. GitHub-mode API routes
(/api/keystatic/github/*) crashed with a 500 under @astrojs/cloudflare
on Astro 6+, confirmed both in astro dev and a real deployed Worker.

Reads the Cloudflare runtime env via a dynamic cloudflare:workers
import instead, matching what the removed API's own error message
recommends. Falls back to the previous context.locals.runtime.env
shape (wrapped in its own try/catch, since that access itself throws
on Astro v6+) for other adapters or older Astro versions, so this
stays adapter-agnostic rather than Cloudflare-only.

Fixes Thinkmill#1554
@pauldiv
pauldiv requested a review from emmatown as a code owner July 15, 2026 08:54
@changeset-bot

changeset-bot Bot commented Jul 15, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 2d794b8

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

This PR includes changesets to release 1 package
Name Type
@keystatic/astro 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

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cloudflare adapter (Astro 6): /api/keystatic/* crashes with 'Astro.locals.runtime.env has been removed in Astro v6'

1 participant