-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
[next]: session drivers #12951
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
Merged
Merged
[next]: session drivers #12951
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
21f3ea0
[next]: session drivers
florian-lefebvre eaad6fa
guide
florian-lefebvre 17f0bda
session driver reference
florian-lefebvre a4ebd8f
fix: link
florian-lefebvre 460efff
Update src/content/docs/en/reference/session-driver-reference.mdx
florian-lefebvre d1ee3b6
Update src/content/docs/en/guides/upgrade-to/v6.mdx
florian-lefebvre 8fb5ce3
Apply suggestions from code review
florian-lefebvre 109dc31
feedback
florian-lefebvre c2b72d5
Merge branch 'feat/sessions-drivers-changes' of https://github.com/wi…
florian-lefebvre d180314
Apply suggestions from code review
florian-lefebvre b2faf29
Apply suggestions from code review
florian-lefebvre 41ab00d
Merge branch 'v6' into feat/sessions-drivers-changes
sarah11918 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
191 changes: 191 additions & 0 deletions
191
src/content/docs/en/reference/session-driver-reference.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,191 @@ | ||
| --- | ||
| title: Astro Session Driver API | ||
| sidebar: | ||
| label: Session Driver API | ||
| i18nReady: true | ||
| tableOfContents: | ||
| minHeadingLevel: 2 | ||
| maxHeadingLevel: 4 | ||
| --- | ||
| import Since from '~/components/Since.astro'; | ||
|
|
||
| Astro [sessions](/en/guides/sessions/) allow to share data between requests for on-demand rendered pages. They require an Astro Session Driver to store session data. | ||
|
|
||
| ## Built-in drivers | ||
|
|
||
| Astro exports built-in session drivers from `astro/config`: | ||
|
|
||
| ```js | ||
| import { sessionDrivers } from 'astro/config' | ||
| ``` | ||
|
|
||
| Any [unstorage driver](https://unstorage.unjs.io/drivers) can be used, for example: | ||
|
|
||
| ```js title="astro.config.mjs" ins={5-7} ins=" sessionDrivers " | ||
| import { defineConfig, sessionDrivers } from 'astro/config' | ||
|
|
||
| export default defineConfig({ | ||
| session: { | ||
| driver: sessionDrivers.redis({ | ||
| url: process.env.REDIS_URL | ||
| }), | ||
| } | ||
| }) | ||
| ``` | ||
|
|
||
| :::note | ||
| Some drivers may need extra packages to be installed. Some drivers may also require environment variables or credentials to be set. See the [Unstorage documentation](https://unstorage.unjs.io/drivers) for more information. | ||
| ::: | ||
|
|
||
| ## Building a session driver | ||
|
|
||
| A session driver is made of two parts: | ||
|
|
||
| - The [driver config](#the-session-driver-config), which lets Astro know what implementation to use at runtime and what config to forward | ||
| - The [driver implementation](#the-session-driver-implementation), which handles the storage logic at runtime | ||
|
|
||
| ### The session driver config | ||
|
|
||
| A `SessionDriverConfig` is an object containing a required runtime [`entrypoint`](#entrypoint) and an optional [`config`](#config). The preferred method for implementing it is to export a function that returns this object and takes the configuration as an optional parameter. | ||
|
|
||
| The following example defines a memory driver config: | ||
|
|
||
| ```ts title="driver/config.ts" | ||
| import type { SessionDriverConfig } from 'astro' | ||
|
|
||
| export interface Config { | ||
| max?: number; | ||
| } | ||
|
|
||
| export function memoryDriver(config: Config = {}): SessionDriverConfig { | ||
| return { | ||
| entrypoint: new URL('./runtime.js', import.meta.url), | ||
| config, | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
florian-lefebvre marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| It is then registered in the Astro config: | ||
|
|
||
| ```ts title="astro.config.ts" | ||
| import { defineConfig } from 'astro/config' | ||
| import { memoryDriver } from './driver/config' | ||
|
|
||
| export default defineConfig({ | ||
| session: { | ||
| driver: memoryDriver({ | ||
| max: 500 | ||
| }) | ||
| } | ||
| }) | ||
| ``` | ||
|
|
||
| #### `entrypoint` | ||
|
|
||
| <p> | ||
|
|
||
| **Type:** `string | URL` | ||
| <Since v="6.0.0" /> | ||
| </p> | ||
|
|
||
| Defines the entrypoint for the [driver implementation](#the-session-driver-implementation). | ||
|
|
||
| #### `config` | ||
|
|
||
| <p> | ||
|
|
||
| **Type:** `Record<string, any> | undefined` | ||
| <Since v="6.0.0" /> | ||
| </p> | ||
|
|
||
| Defines the serializable config passed to [driver implementation](#the-session-driver-implementation) at runtime. | ||
|
|
||
| ### The session driver implementation | ||
|
|
||
| A `SessionDriver` is an object responsible for [storing](#setitem), [retrieving](#getitem) and [deleting](#removeitem) data when [using sessions at runtime](/en/reference/api-reference/#session) (e.g. `context.session.set()`). You can implement it in your session driver module by exporting a default function that takes the [driver config](#config) as parameter. | ||
sarah11918 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| The following example implements a memory driver: | ||
florian-lefebvre marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ```ts title="driver/runtime.ts" | ||
| import type { SessionDriver } from 'astro' | ||
| import type { Config } from './config' | ||
| import { LRUCache } from 'lru-cache' | ||
|
|
||
| export default function(config: Config): SessionDriver { | ||
| const cache = new LRUCache({ max: config.max }) | ||
| return { | ||
| setItem: async (key, value) => { | ||
| cache.set(key, value) | ||
| }, | ||
| getItem: async (key) => { | ||
| return cache.get(key) | ||
| }, | ||
| removeItem: async (key) => { | ||
| cache.delete(key) | ||
| }, | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| #### `setItem()` | ||
|
|
||
| <p> | ||
|
|
||
| **Type:** `(key: string, value: any) => Promise<void>` | ||
| <Since v="6.0.0" /> | ||
| </p> | ||
|
|
||
| Defines a function that sets session data by key. | ||
|
|
||
| #### `getItem()` | ||
|
|
||
| <p> | ||
|
|
||
| **Type:** `(key: string) => Promise<any>` | ||
| <Since v="6.0.0" /> | ||
| </p> | ||
|
|
||
| Defines a function that retrieves session data by key. | ||
|
|
||
| #### `removeItem()` | ||
|
|
||
| <p> | ||
|
|
||
| **Type:** `(key: string) => Promise<void>` | ||
| <Since v="6.0.0" /> | ||
| </p> | ||
|
|
||
| Defines a function that removes session data by key. | ||
|
|
||
| ## Unstorage compatibility | ||
|
|
||
| Unstorage driver types are compatible with Astro's `SessionDriver` type. | ||
|
|
||
| That means you can use an unstorage package export as an [entrypoint](#entrypoint). For example: | ||
|
|
||
| ```ts title="driver/config.ts" {5} | ||
| import type { SessionDriverConfig } from 'astro' | ||
|
|
||
| export function configuredRedisDriver(): SessionDriverConfig { | ||
| return { | ||
| entrypoint: 'unstorage/drivers/redis', | ||
florian-lefebvre marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| config: { | ||
| tls: true | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| Alternatively, you can import and use an unstorage driver directly in the implementation. For example: | ||
|
|
||
| ```ts title="driver/runtime.ts" {2} | ||
| import type { SessionDriver } from 'astro' | ||
| import redisDriver from "unstorage/drivers/redis"; | ||
|
|
||
| export default function(config): SessionDriver { | ||
| return redisDriver({ | ||
| ...config, | ||
| tls: true | ||
| }) | ||
| } | ||
| ``` | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.