diff --git a/src/content/docs/en/guides/sessions.mdx b/src/content/docs/en/guides/sessions.mdx index c18f1f6f7cbc3..8b026c193e5a3 100644 --- a/src/content/docs/en/guides/sessions.mdx +++ b/src/content/docs/en/guides/sessions.mdx @@ -35,18 +35,48 @@ import vercel from '@astrojs/vercel' export default defineConfig({ adapter: vercel() session: { - driver: sessionDrivers.redis({ - url: process.env.REDIS_URL + driver: sessionDrivers.lruCache({ + max: 800, }), } }) ``` +You can also configure the session driver using environment variables. When defined in the configuration file, these variables are inlined at build time. If you need to override them at runtime, define the driver configuration in a separate file. Then use the module as the [driver's entrypoint](/en/reference/session-driver-reference/#entrypoint). + +The following example configures the Redis driver with runtime variables in a file located in your `src` directory: + +```ts title="src/session-driver.ts" +import type { SessionDriverConfig } from "astro"; +import { sessionDrivers } from "astro/config"; + +export default function (): SessionDriverConfig { + return sessionDrivers.redis({ + url: process.env.REDIS_URL, + }); +} +``` + +You can then use this file as the driver entrypoint in your Astro configuration: + +```js title="astro.config.mjs" {7-9} +import { defineConfig, sessionDrivers } from "astro/config"; +import vercel from "@astrojs/vercel"; + +export default defineConfig({ + adapter: vercel(), + session: { + driver: { + entrypoint: new URL('./src/session-driver.ts', import.meta.url), + } + } +}); +``` + - See [the `session` configuration option](/en/reference/configuration-reference/#session-options) for more details on setting a storage driver, and other configurable options. +See [the `session` configuration option](/en/reference/configuration-reference/#session-options) for more details on setting a storage driver, and other configurable options. - ## Interacting with session data The [`session` object](/en/reference/api-reference/#session) allows you to interact with the stored user state (e.g. adding items to a shopping cart) and the session ID (e.g. deleting the session ID cookie when logging out). The object is accessible as `Astro.session` in your Astro components and pages and as `context.session` object in API endpoints, middleware, and actions. diff --git a/src/content/docs/en/reference/configuration-reference.mdx b/src/content/docs/en/reference/configuration-reference.mdx index 2039bb4c8f594..30aeecc53b72e 100644 --- a/src/content/docs/en/reference/configuration-reference.mdx +++ b/src/content/docs/en/reference/configuration-reference.mdx @@ -1228,24 +1228,24 @@ Set custom HTTP response headers to be sent in `astro dev` and `astro preview`.

Configures session storage for your Astro project. This is used to store session data in a persistent way, so that it can be accessed across different requests. -Some adapters may provide a default session driver, but you can override it with your own configuration. -See [the sessions guide](/en/guides/sessions/) for more information. +Some adapters may provide a default session driver, but you can override it with your own configuration: ```js title="astro.config.mjs" - { - session: { - // The name of the Unstorage driver - driver: 'redis', - // The required options depend on the driver - options: { - url: process.env.REDIS_URL, - }, - ttl: 3600, // 1 hour - } +import { defineConfig, sessionDrivers } from 'astro/config' + +export default defineConfig({ + session: { + driver: sessionDrivers.redis({ + // The options are driver-dependent and some may be required. + url: process.env.REDIS_URL + }), } +}) ``` +See [the sessions guide](/en/guides/sessions/) for more information on [configuring session drivers with runtime variables](/en/guides/sessions/#configuring-sessions) and how to [interact with their data](/en/guides/sessions/#interacting-with-session-data). + ### session.driver

@@ -2505,4 +2505,3 @@ Font [variation settings](https://developer.mozilla.org/en-US/docs/Web/CSS/@font ```js variationSettings: "'xhgt' 0.7" ``` -