Skip to content

Conversation

@matthewp
Copy link
Contributor

Changes

  • Create virtual module (virtual:astro-cloudflare:config) to expose configurable bindings
  • Update Cloudflare handler to dynamically access KV binding via virtual module
  • Remove hardcoded SESSION binding name, now fully configurable via sessionKVBindingName
  • Remove unused __env__ global variable
  • Add sessions form test fixture for Cloudflare integration
  • Sessions now fully integrated with Astro environment API

Testing

Tested with Cloudflare Pages local dev server:

  • Sessions can be read via Astro.session?.get()
  • Sessions can be written via Astro.session?.set()
  • Custom binding names work correctly
  • Dev server properly initializes session config with platform proxy

Docs

No docs changes needed - sessions functionality documented in existing guide. Custom binding name option already documented in integration options.

- create virtual module for cloudflare config (sessionKVBindingName)
- handler dynamically accesses binding from env via virtual module
- remove hardcoded SESSION binding name, now configurable
- remove unused __env__ global
- add test fixture with sessions form
- sessions now fully integrated with astro environment api
@changeset-bot
Copy link

changeset-bot bot commented Oct 30, 2025

⚠️ No Changeset found

Latest commit: d6dfa81

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a 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 Oct 30, 2025
@matthewp
Copy link
Contributor Author

@copilot can you run lint pnpm run lint:ci and fix the issues.

Copy link

Copilot AI commented Oct 30, 2025

@matthewp I've opened a new pull request, #14697, to work on those changes. Once the pull request is ready, I'll request review from you.

Copilot AI and others added 2 commits October 30, 2025 15:10
* Initial plan

* fix: organize imports and formatting in handler.ts

Co-authored-by: matthewp <[email protected]>

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: matthewp <[email protected]>
@ematipico
Copy link
Member

@matthewp what I fail to understand is what we're fixing/making better. Can you highlight that, please?

Copy link
Member

@ematipico ematipico left a comment

Choose a reason for hiding this comment

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

Tested locally, it works

<h1>Sessions</h1>
<h2>Cart</h2>
<p>
<a href="/checkout">🛒 {cart?.length ?? 0} items</a>
Copy link
Member

Choose a reason for hiding this comment

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

Checkout doesn't exist. Tested locally and it gives 404

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was already in here, look at the diff. I think you added it :D

I only added to the demo to confirm that it was indeed saving to the cache.

Comment on lines +15 to +19
"kv_namespaces": [
{
"binding": "SESSION",
"id": "SESSION"
}
Copy link
Member

Choose a reason for hiding this comment

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

Could you let me know where you found the docs for this? I couldn't make it working

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@matthewp
Copy link
Contributor Author

@ematipico The previous implementation of sessions depended on globals which of course we can't rely on any more. So the fix is to provide the session on the config object, here: https://github.com/withastro/astro/pull/14696/files#diff-2f4ffbe3ae5b73f769512941577c209c575ce394e67c9f0f8f28711d056d9eebR43-R48

@matthewp matthewp merged commit 7fcd225 into feat/environment-api Oct 31, 2025
7 of 17 checks passed
@matthewp matthewp deleted the feat/sessions-environment-api branch October 31, 2025 16:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

4 participants