diff options
author | 2025-04-15 02:36:51 -0700 | |
---|---|---|
committer | 2025-04-15 10:36:51 +0100 | |
commit | 59af0cbd0a36a983cd9fefbbca669dae84ce1e94 (patch) | |
tree | 31848d4cb87e580a87877080db3e0d963b15dd15 /packages/integrations/node | |
parent | d038030770b294e811beb99c9478fbe4b4cbb968 (diff) | |
download | astro-@astrojs/cloudflare@12.5.0.tar.gz astro-@astrojs/cloudflare@12.5.0.tar.zst astro-@astrojs/cloudflare@12.5.0.zip |
[ci] release (#13610)astro@5.7.0@astrojs/node@9.2.0@astrojs/netlify@6.3.0@astrojs/markdoc@0.14.0@astrojs/cloudflare@12.5.0
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Diffstat (limited to 'packages/integrations/node')
-rw-r--r-- | packages/integrations/node/CHANGELOG.md | 87 | ||||
-rw-r--r-- | packages/integrations/node/package.json | 2 |
2 files changed, 88 insertions, 1 deletions
diff --git a/packages/integrations/node/CHANGELOG.md b/packages/integrations/node/CHANGELOG.md index 5b86cca17..b4df5833b 100644 --- a/packages/integrations/node/CHANGELOG.md +++ b/packages/integrations/node/CHANGELOG.md @@ -1,5 +1,92 @@ # @astrojs/node +## 9.2.0 + +### Minor Changes + +- [#13527](https://github.com/withastro/astro/pull/13527) [`2fd6a6b`](https://github.com/withastro/astro/commit/2fd6a6b7aa51a4713af7fac37d5dfd824543c1bc) Thanks [@ascorbic](https://github.com/ascorbic)! - The experimental session API introduced in Astro 5.1 is now stable and ready for production use. + + Sessions are used to store user state between requests for [on-demand rendered pages](https://astro.build/en/guides/on-demand-rendering/). You can use them to store user data, such as authentication tokens, shopping cart contents, or any other data that needs to persist across requests: + + ```astro + --- + export const prerender = false; // Not needed with 'server' output + const cart = await Astro.session.get('cart'); + --- + + <a href="/checkout">🛒 {cart?.length ?? 0} items</a> + ``` + + ## Configuring session storage + + Sessions require a storage driver to store the data. The Node, Cloudflare and Netlify adapters automatically configure a default driver for you, but other adapters currently require you to specify a custom storage driver in your configuration. + + If you are using an adapter that doesn't have a default driver, or if you want to choose a different driver, you can configure it using the `session` configuration option: + + ```js + import { defineConfig } from 'astro/config'; + import vercel from '@astrojs/vercel'; + + export default defineConfig({ + adapter: vercel(), + session: { + driver: 'upstash', + }, + }); + ``` + + ## Using sessions + + Sessions are available in on-demand rendered pages, API endpoints, actions and middleware. + + In pages and components, you can access the session using `Astro.session`: + + ```astro + --- + const cart = await Astro.session.get('cart'); + --- + + <a href="/checkout">🛒 {cart?.length ?? 0} items</a> + ``` + + In endpoints, actions, and middleware, you can access the session using `context.session`: + + ```js + export async function GET(context) { + const cart = await context.session.get('cart'); + return Response.json({ cart }); + } + ``` + + If you attempt to access the session when there is no storage driver configured, or in a prerendered page, the session object will be `undefined` and an error will be logged in the console: + + ```astro + --- + export const prerender = true; + const cart = await Astro.session?.get('cart'); // Logs an error. Astro.session is undefined + --- + ``` + + ## Upgrading from Experimental to Stable + + If you were previously using the experimental API, please remove the `experimental.session` flag from your configuration: + + ```diff + import { defineConfig } from 'astro/config'; + import node from '@astrojs/node'; + + export default defineConfig({ + adapter: node({ + mode: "standalone", + }), + - experimental: { + - session: true, + - }, + }); + ``` + + See [the sessions guide](https://docs.astro.build/en/guides/sessions/) for more information. + ## 9.1.3 ### Patch Changes diff --git a/packages/integrations/node/package.json b/packages/integrations/node/package.json index 62530c276..f928d52a8 100644 --- a/packages/integrations/node/package.json +++ b/packages/integrations/node/package.json @@ -1,7 +1,7 @@ { "name": "@astrojs/node", "description": "Deploy your site to a Node.js server", - "version": "9.1.3", + "version": "9.2.0", "type": "module", "types": "./dist/index.d.ts", "author": "withastro", |