-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat(cloudflare): add KV session storage support #13514
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
🦋 Changeset detectedLatest commit: fd17d36 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 |
astro
@astrojs/cloudflare
@astrojs/netlify
@astrojs/node
@astrojs/vercel
commit: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome 🔥
| }; | ||
| } | ||
|
|
||
| declare global { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we comment on the usage and the why of these two globals?
| * "kv_namespaces": [ | ||
| * { | ||
| * "binding": "SESSION", | ||
| * "id": "<your kv namespace id here>" | ||
| * } | ||
| * ] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we tell users what will happen if they forget to add this namespace? Since we can't catch the error at runtime, the least we can do tell users what will happen if they miss this step
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added an explicit message saying what the error would be
| options: { | ||
| // This is the name of the global variable where the KV namespace is stored | ||
| // in the worker. It is assigned to this inside the request handler. | ||
| binding: '__ASTRO_SESSION', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whether this is a nitpick or not, I foresee a use case where users might want to override this because they don't want to leak information relative to their framework. However, I think it's fine to keep it like this for now, and re-iterate if users need to overrde it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They can override the actual binding name: this is just the name of the variable we use to pass it to unstorage.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've changed it to use the same name as the binding, because otherwise the error message from unstorage would be confusing.
|
I've added this to the 5.6 milestone just for the purposes of blogs, as it's an experimental feature |
Changes
Adds support for automatically-provisioning session storage for Cloudflare KV
Automatically configures Cloudflare KV storage when experimental sessions are enabled
If the
experimental.sessionflag is enabled when using the Cloudflare adapter, Astro will automatically configure the session storage using the Cloudflare KV driver. You can still manually configure the session storage if you need to use a different driver or want to customize the session storage configuration. If you want to use sessions, you will need to create the KV namespace and declare it in your wrangler config. You can do this using the Wrangler CLI:This will log the id of the created namespace. You can then add it to your
wrangler.json/wrangler.tomlfile like this:By default it uses the binding name
SESSION, but if you want to use a different binding name you can do so by passing thesessionKVBindingNameoption to the adapter. For example:Testing
Adds fixture and test suite. To try it on your own site, use the preview release of
@astrojs/cloudflareDocs
Needs updated docs