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
2 changes: 2 additions & 0 deletions src/content/docs/en/guides/integrations-guide/cloudflare.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,8 @@ const cart = await Astro.session?.get('cart');

By default, the KV binding is named `SESSION`. To use a different name, set the [`sessionKVBindingName`](#sessionkvbindingname) option in the adapter config.

If your project does not use sessions, you can set [`session: false`](/en/guides/sessions/#disabling-sessions) in your Astro config. The adapter will not configure a KV binding, no KV namespace will be provisioned when you deploy, and the session runtime will be excluded from your Worker bundle.

:::note
Writes to Cloudflare KV are [eventually consistent](https://developers.cloudflare.com/kv/concepts/how-kv-works/#consistency) between regions. This means that changes are available immediately within the same region but may take up to 60 seconds to propagate globally. This won't affect most users as they are unlikely to switch regions between requests, but it may be a consideration for some use cases, such as VPN users.
:::
Expand Down
2 changes: 2 additions & 0 deletions src/content/docs/en/guides/integrations-guide/netlify.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ The Astro [Sessions API](/en/guides/sessions/) allows you to easily store user d

Astro automatically configures [Netlify Blobs](https://docs.netlify.com/blobs/overview/) for session storage when using the Netlify adapter. If you would prefer to use a different session storage driver, you can specify it in your Astro config. See [the `session` configuration reference](/en/reference/configuration-reference/#sessiondriver) for more details.

If your project does not use sessions, you can set [`session: false`](/en/guides/sessions/#disabling-sessions) in your Astro config. The adapter will not configure Netlify Blobs for session storage, and the session runtime will be excluded from your function bundle.

### Caching Pages

On-demand rendered pages without any dynamic content can be cached to improve performance and lower resource usage.
Expand Down
2 changes: 2 additions & 0 deletions src/content/docs/en/guides/integrations-guide/node.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,8 @@ The Astro [Sessions API](/en/guides/sessions/) allows you to easily store user d

Astro uses the local filesystem for session storage when using the Node adapter. If you would prefer to use a different session storage driver, you can specify it in your Astro config. See [the `session` configuration reference](/en/reference/configuration-reference/#sessiondriver) for more details.

If your project does not use sessions, you can set [`session: false`](/en/guides/sessions/#disabling-sessions) in your Astro config. The adapter will not configure the filesystem session driver, and the session runtime will be excluded from your server bundle.

## Environment variables

When using the [`astro:env`](/en/guides/environment-variables/#type-safe-environment-variables) secrets or `process.env` at runtime, neither Astro nor the adapter loads environment variables for you.
Expand Down
24 changes: 24 additions & 0 deletions src/content/docs/en/guides/sessions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,30 @@ The following example takes advantage of [Unstorage compatibility](/en/reference
```
</Steps>

## Disabling sessions

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## Disabling sessions
### Disabling sessions

I think this might be better served as an <h3> so it sits under “Configuring sessions” instead of top level.

Before

  • Overview
  • Configuring sessions
    • Overriding the configuration at runtime
  • Disabling sessions
  • Interacting with session data
    • Astro components and pages
    • API endpoints
    • Actions
    • Middleware
  • Session data types

After

  • Overview
  • Configuring sessions
    • Overriding the configuration at runtime
    • Disabling sessions
  • Interacting with session data
    • Astro components and pages
    • API endpoints
    • Actions
    • Middleware
  • Session data types


<p>
<Since v="7.1.0" />

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may not make the Astro 7.1 release, so this will need to be updated accordingly.

</p>

If your project does not use sessions, you can set `session: false` to opt out of session support entirely:

```js title="astro.config.mjs" ins={4}
import { defineConfig } from 'astro/config';

export default defineConfig({
session: false,
});
```

When sessions are disabled, adapters that normally provide a default session driver (e.g. [Node](/en/guides/integrations-guide/node/#sessions), [Cloudflare](/en/guides/integrations-guide/cloudflare/#sessions), and [Netlify](/en/guides/integrations-guide/netlify/#sessions)) will not configure one, and the session runtime and its dependencies are excluded from your server bundle. This can be useful for serverless and edge runtimes, where a smaller bundle reduces cold start time.

`Astro.session` and `context.session` will be `undefined`, exactly as in a project with no session storage configured. Code that checks for session availability (e.g. `Astro.session?.get()`) continues to work without changes.

:::note
The session runtime is automatically excluded from your server bundle whenever no session driver is configured, even without setting `session: false`. Setting `session: false` additionally tells your adapter not to provide its default driver.
:::

## 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
4 changes: 2 additions & 2 deletions src/content/docs/en/reference/api-reference.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -882,15 +882,15 @@ Allows customizing how the cookie is serialized.

<p>

**Type:** `AstroSession`
**Type:** `AstroSession | undefined`

<Since v="5.7.0" />

</p>

`session` is an object that allows data to be stored between requests for [routes rendered on demand](/en/guides/on-demand-rendering/). It is associated with a cookie that contains the session ID only: the data itself is not stored in the cookie.

The session is created when first used, and the session cookie is automatically set. The `session` object is `undefined` if no session storage has been configured, or if the current route is prerendered, and will log an error if you try to use it.
The session is created when first used, and the session cookie is automatically set. The `session` object is `undefined` if no session storage has been configured, if [sessions are disabled](/en/guides/sessions/#disabling-sessions) with `session: false`, or if the current route is prerendered, and will log an error if you try to use it.

See [the session guide](/en/guides/sessions/) for more information on how to use sessions in your Astro project.

Expand Down
2 changes: 2 additions & 0 deletions src/content/docs/en/reference/modules/astro-fetch.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ await sessions(state);
// ...render pipeline...
```

If no session driver is configured, or sessions are [disabled](/en/guides/sessions/#disabling-sessions) with `session: false`, this handler registers no provider and `ctx.session` will be `undefined`.

### `trailingSlash()`

<p>
Expand Down
Loading