Server Side Rendering
Paraglide JS provides first-class support for server-side rendering (SSR) through its paraglideMiddleware()
and async context management.
Using paraglideMiddleware()
The paraglideMiddleware()
handles request-scoped locale management automatically:
Key features:
- Automatically manages async local storage context
- Handles URL localization/delocalization
- Ensures locale state isolation between requests
Automatic re-directs
The paraglideMiddleware()
automatically re-directs requests to the appropriate localized URL.
For example, assume that the cookie strategy preceeds the url strategy. If a request from a client is made where the client's locale cookie is set to de
, the paraglideMiddleware()
will re-direct the request from https://example.com/page
to https://example.com/de/seite
.
If the automatic redirects are not desired, you can increase the precedence of the url
strategy:
Disabling Async Local Storage
You can use disableAsyncLocalStorage: true
to disable the use of Node.js' AsyncLocalStorage. Paraglide JS uses Node.js' AsyncLocalStorage to maintain request context isolation. This is crucial for:
- Preventing locale information from leaking between concurrent requests
- Ensuring consistent URL origin resolution
- Maintaining a clean separation of request-specific state
- Cloudflare Workers
- Vercel Edge Functions
- AWS Lambda (single-request mode)
- Other isolated runtime contexts
Best Practices
-
Always enable AsyncLocalStorage for:
- Traditional Node.js servers
- Docker containers handling multiple requests
- Any environment with request pooling
-
Test isolation by making parallel requests:
-
Monitor for these warning signs:
- Random locale switches during load testing
- Incorrect URL origins in logs
- Session data appearing in wrong requests
Serverless Environments
For edge functions and serverless platforms, you can use per-request overrides because each request is isolated: