-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
feat: full sessions support w/ environment api #14696
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
Changes from all commits
c2856f4
9af11de
0ed30fb
cf02c11
1d28e33
ddd8e79
d6dfa81
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import type { PluginOption } from 'vite'; | ||
|
|
||
| const VIRTUAL_CONFIG_ID = 'virtual:astro-cloudflare:config'; | ||
| const RESOLVED_VIRTUAL_CONFIG_ID = '\0' + VIRTUAL_CONFIG_ID; | ||
florian-lefebvre marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| interface CloudflareConfig { | ||
| sessionKVBindingName: string; | ||
| } | ||
|
|
||
| export function createConfigPlugin(config: CloudflareConfig): PluginOption { | ||
| return { | ||
| name: 'vite:astro-cloudflare-config', | ||
| resolveId(id) { | ||
| if (id === VIRTUAL_CONFIG_ID) { | ||
| return RESOLVED_VIRTUAL_CONFIG_ID; | ||
| } | ||
| }, | ||
| load(id) { | ||
| if (id === RESOLVED_VIRTUAL_CONFIG_ID) { | ||
| return `export const sessionKVBindingName = ${JSON.stringify(config.sessionKVBindingName)};`; | ||
| } | ||
| }, | ||
| }; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,44 @@ | ||
| --- | ||
| export const prerender = false; // Not needed with 'server' output | ||
| const cart = await Astro.session?.get('cart'); | ||
| const user = await Astro.session?.get('user'); | ||
| let user = await Astro.session?.get('user'); | ||
|
|
||
| if(Astro.request.method === 'POST') { | ||
| const data = await Astro.request.formData(); | ||
| const newId = data.get('id'); | ||
| const newName = data.get('name'); | ||
|
|
||
| if(newId && newName) { | ||
| user = { | ||
| id: newId, | ||
| name: newName | ||
| }; | ||
| Astro.session?.set('user', user); | ||
| } | ||
| } | ||
| --- | ||
|
|
||
| <h1> | ||
| Sessions | ||
| </h1> | ||
| <h2> | ||
| Cart | ||
| </h2> | ||
| <p> | ||
| <a href="/checkout">🛒 {cart?.length ?? 0} items</a> | ||
| </p> | ||
| <html> | ||
| <head> | ||
| <meta charset="utf-8" /> | ||
| <title>Sessions</title> | ||
| </head> | ||
| <body> | ||
| <h1>Sessions</h1> | ||
| <h2>Cart</h2> | ||
| <p> | ||
| <a href="/checkout">🛒 {cart?.length ?? 0} items</a> | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Checkout doesn't exist. Tested locally and it gives 404
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| </p> | ||
|
|
||
| <h2>User</h2> | ||
| <p>Id: {user?.id}</p> | ||
| <p>Name: {user?.name}</p> | ||
|
|
||
| <h2> | ||
| User | ||
| </h2> | ||
| <p> | ||
| Id: {user?.id} | ||
| </p> | ||
| <p> | ||
| Name: {user?.name} | ||
| </p> | ||
| <form method="post"> | ||
| <h3>Change</h3> | ||
| <input type="text" name="id" value={user?.id} /> | ||
| <input type="text" name="name" value={user?.name} /> | ||
| <input type="submit" value="Submit" /> | ||
| </form> | ||
| </body> | ||
| </html> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,5 +11,11 @@ | |
| }, | ||
| "images": { | ||
| "binding": "IMAGES" | ||
| } | ||
| }, | ||
| "kv_namespaces": [ | ||
| { | ||
| "binding": "SESSION", | ||
| "id": "SESSION" | ||
| } | ||
|
Comment on lines
+15
to
+19
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| ] | ||
| } | ||
florian-lefebvre marked this conversation as resolved.
Show resolved
Hide resolved
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| declare module 'virtual:astro-cloudflare:config' { | ||
| export const sessionKVBindingName: string; | ||
florian-lefebvre marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Additional exports can be added here in the future | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.