π Patch Changes
- #13591
5dd2d3fThanks @florian-lefebvre! - Removes unused code
5dd2d3f Thanks @florian-lefebvre! - Removes unused code#13527 2fd6a6b Thanks @ascorbic! - The experimental session API introduced in Astro 5.1 is now stable and ready for production use.
Sessions are used to store user state between requests for on-demand rendered pages. You can use them to store user data, such as authentication tokens, shopping cart contents, or any other data that needs to persist across requests:
---export const prerender = false; // Not needed with 'server' outputconst cart = await Astro.session.get('cart');---
<a href="/checkout">π {cart?.length ?? 0} items</a>Sessions require a storage driver to store the data. The Node, Cloudflare and Netlify adapters automatically configure a default driver for you, but other adapters currently require you to specify a custom storage driver in your configuration.
If you are using an adapter that doesnβt have a default driver, or if you want to choose a different driver, you can configure it using the session configuration option:
import { defineConfig } from 'astro/config';import vercel from '@astrojs/vercel';
export default defineConfig({ adapter: vercel(), session: { driver: 'upstash', },});Sessions are available in on-demand rendered pages, API endpoints, actions and middleware.
In pages and components, you can access the session using Astro.session:
---const cart = await Astro.session.get('cart');---
<a href="/checkout">π {cart?.length ?? 0} items</a>In endpoints, actions, and middleware, you can access the session using context.session:
export async function GET(context) { const cart = await context.session.get('cart'); return Response.json({ cart });}If you attempt to access the session when there is no storage driver configured, or in a prerendered page, the session object will be undefined and an error will be logged in the console:
---export const prerender = true;const cart = await Astro.session?.get('cart'); // Logs an error. Astro.session is undefined---If you were previously using the experimental API, please remove the experimental.session flag from your configuration:
import { defineConfig } from 'astro/config';import node from '@astrojs/node';
export default defineConfig({ adapter: node({ mode: "standalone", }), experimental: { session: true, },});See the sessions guide for more information.
042d1de]:
1e11f5e]:
c6bf6b3 Thanks @lee-arnold! - Fixes the image endpoint entrypoint#13145 8d4e566 Thanks @ascorbic! - Automatically configures filesystem storage when experimental session enabled
If the experimental.session flag is enabled when using the Node adapter, Astro will automatically configure session storage using the filesystem driver. You can still manually configure session storage if you need to use a different driver or want to customize the session storage configuration.
See the experimental session docs for more information on configuring session storage.
#11679 ea71b90 Thanks @florian-lefebvre! - Adds stable support for astro:env
#11770 cfa6a47 Thanks @Princesseuh! - Removed support for the Squoosh image service. As the underlying library libsquoosh is no longer maintained, and the image service sees very little usage we have decided to remove it from Astro.
Our recommendation is to use the base Sharp image service, which is more powerful, faster, and more actively maintained.
import { squooshImageService } from "astro/config";import { defineConfig } from "astro/config";
export default defineConfig({ image: { service: squooshImageService() }});If you are using this service, and cannot migrate to the base Sharp image service, a third-party extraction of the previous service is available here: https://github.com/Princesseuh/astro-image-service-squoosh
#11535 932bd2e Thanks @matthewp! - Move polyfills up before awaiting the env module in the Node.js adapter.
Previously the env setting was happening before the polyfills were applied. This means that if the Astro env code (or any dependencies) depended on crypto, it would not be polyfilled in time.
Polyfills should be applied ASAP to prevent races. This moves it to the top of the Node adapter.
5848d97 Thanks @florian-lefebvre! - Fixes astro:env getSecret compatibility2bdca27 Thanks @florian-lefebvre! - Adds support for experimental astro:env released in Astro 4.10d0248bc Thanks @dkobierski! - Fixes suppressed logs when error occurs28e33a2f9c04373eae5da2e6edb0dc2981bce790 Thanks @castarco! - Fixes a bug where the preview server wrongly appends trailing slashes to subresource URLs.83f9105cd50e2756d02ca2be73ab84f9d582d3f8 Thanks @lilnasy! - Prevents crashes caused by rejections of offshoot promises.d5277df5a4d1e9a8a7b6c8d7b87912e13a163f7f Thanks @Princesseuh! - Fixes an issue where malformed requests could cause the server to error in certain cases.b47dcaa25968ec85ba96fce23381c94a94e389f6 Thanks @SatanshuMishra! - Fixes the server.host option to properly listen on all network interfaces when set to true8cd38f02456640c063552aef00b2b8a216b3935d Thanks @log101! - Fixes custom headers are not added to the Node standalone server responses in preview mode041fdd5c89920f7ccf944b095f29e451f78b0e28 Thanks @ematipico! - Adds experimental support for internationalization domains