Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 34 additions & 4 deletions src/content/docs/en/guides/sessions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}
});
```

<ReadMore>
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.
</ReadMore>


## 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.
Expand Down
25 changes: 12 additions & 13 deletions src/content/docs/en/reference/configuration-reference.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -1228,24 +1228,24 @@ Set custom HTTP response headers to be sent in `astro dev` and `astro preview`.
</p>

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

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

Loading