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
44 changes: 33 additions & 11 deletions src/content/docs/en/guides/sessions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,21 @@ When you need a different configuration (e.g. to connect to an external service)
The following example takes advantage of [Unstorage compatibility](/en/reference/session-driver-reference/#unstorage-compatibility) to configure the Redis driver in its own entrypoint:

<Steps>
1. Install the [`unstorage` package](https://unstorage.unjs.io/guide):
1. Install the [`unstorage`](https://www.npmjs.com/package/unstorage) and [`ioredis`](https://www.npmjs.com/package/ioredis) packages:
<PackageManagerTabs>
<Fragment slot="npm">
```sh
npm install unstorage
npm install unstorage ioredis
```
</Fragment>
<Fragment slot="pnpm">
```sh
pnpm add unstorage
pnpm add unstorage ioredis
```
</Fragment>
<Fragment slot="yarn">
```sh
yarn add unstorage
yarn add unstorage ioredis
```
</Fragment>
</PackageManagerTabs>
Expand All @@ -83,29 +83,51 @@ The following example takes advantage of [Unstorage compatibility](/en/reference
import { REDIS_HOST, REDIS_PORT } from "astro:env";

export default function (): SessionDriver {
return redisDriver({
const driver = redisDriver({
host: REDIS_HOST,
port: REDIS_PORT,
});

return {
async getItem(key) {
return await driver.getItem(key);
},
async setItem(key, value) {
await driver.setItem?.(key, value, {});
},
async removeItem(key) {
await driver.removeItem?.(key, {});
},
};
}
```

3. Use this file as the driver's entrypoint in your Astro configuration:
```js title="astro.config.mjs" {11-13}
```js title="astro.config.mjs" {21-23}
import { defineConfig, envField, sessionDrivers } from "astro/config";
import vercel from "@astrojs/vercel";

export default defineConfig({
adapter: vercel(),
env: {
REDIS_HOST: envField.string({ context: "server", access: "public", default: "localhost" }),
REDIS_PORT: envField.number({ context: "server", access: "public", default: 6379 }),
schema: {
REDIS_HOST: envField.string({
context: "server",
access: "public",
default: "localhost",
}),
REDIS_PORT: envField.number({
context: "server",
access: "public",
default: 6379,
}),
},
},
session: {
driver: {
entrypoint: new URL('./src/session-driver.ts', import.meta.url),
}
}
entrypoint: new URL("./src/session-driver.ts", import.meta.url),
},
},
});
```
</Steps>
Expand Down
52 changes: 33 additions & 19 deletions src/content/docs/en/reference/session-driver-reference.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -159,33 +159,47 @@ Defines a function that removes session data by key.

## Unstorage compatibility

Unstorage driver types are compatible with Astro's `SessionDriver` type.
The [built-in drivers](#built-in-drivers) provide the same drivers as Unstorage. When you need greater customization, you can [build your own driver](#building-a-session-driver) based on any [Unstorage driver](https://unstorage.unjs.io/drivers).

That means you can use an unstorage package export as an [entrypoint](#entrypoint). For example:
Install the `unstorage` package and pass the driver specifier as the [entrypoint](#entrypoint):

```ts title="driver/config.ts" {5}
import type { SessionDriverConfig } from 'astro'
import type { SessionDriverConfig } from "astro";

export function configuredRedisDriver(): SessionDriverConfig {
return {
entrypoint: 'unstorage/drivers/redis',
config: {
tls: true
}
}
return {
entrypoint: "unstorage/drivers/redis",
config: {
tls: true,
},
};
}
```

Alternatively, you can import and use an unstorage driver directly in the implementation. For example:
You can also import an Unstorage driver and wrap it in your own implementation. This can be useful if you want to add extra logic to the driver or [override the configuration at runtime](/en/guides/sessions/#overriding-the-configuration-at-runtime).

```ts title="driver/runtime.ts" {2}
import type { SessionDriver } from 'astro'
import redisDriver from "unstorage/drivers/redis";
The following example implements a Redis driver with a default `ttl` of 7 days:

export default function(config): SessionDriver {
return redisDriver({
...config,
tls: true
})
```ts title="driver/runtime.ts"
import type { SessionDriver } from "astro";
import redisDriver, { type RedisOptions } from "unstorage/drivers/redis";

export default function (config: RedisOptions): SessionDriver {
const driver = redisDriver({
...config,
ttl: config.ttl ?? 60 * 60 * 24 * 7, // default to 7 days
});

return {
async getItem(key) {
return await driver.getItem(key);
},
async setItem(key, value) {
await driver.setItem?.(key, value, {});
},
async removeItem(key) {
await driver.removeItem?.(key, {});
},
};
}
```
```
Loading