-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e5b6a3a
commit 82581f7
Showing
6 changed files
with
142 additions
and
43 deletions.
There are no files selected for viewing
This file contains 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,23 @@ | ||
--- | ||
"@inlang/paraglide-sveltekit": minor | ||
--- | ||
|
||
Adds a `disableAsyncLocalStorage` option to `i18n.handle`. This allows you to opt out of using the experimental `AsyncLocalStorage` API. | ||
|
||
**Warning** | ||
Disabling `AsyncLocalStorage` removes the protection against concurrent requests overriding each other's language state. | ||
|
||
Only opt out if `AsyncLocalStorage` if you are certain your environment does not handle concurrent requests in the same process. For example in Vercel Edge functions or Cloudflare Workers. | ||
|
||
In environments where only one request is processed in a given process disabling `AsyncLocalStorage` can yield performance gains. | ||
|
||
**Example** | ||
```ts | ||
// src/hooks.server.js | ||
import { i18n } from "$lib/i18n" | ||
|
||
export const handle = i18n.handle({ | ||
disableAsyncLocalStorage: true // @default = false | ||
}) | ||
|
||
``` |
This file contains 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 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 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
34 changes: 34 additions & 0 deletions
34
inlang/source-code/paraglide/paraglide-sveltekit/src/runtime/hooks/utils.ts
This file contains 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,34 @@ | ||
export type ALS<T> = import("node:async_hooks").AsyncLocalStorage<T> | ||
export type ALSPrototype = typeof import("node:async_hooks").AsyncLocalStorage | ||
|
||
export interface Context<T> { | ||
get(): T | undefined | ||
callAsync: <CB extends () => any>(val: T, cb: CB) => Promise<Awaited<ReturnType<CB>>> | ||
} | ||
|
||
export class ALSContext<T> implements Context<T> { | ||
ctx: ALS<T> | ||
constructor(ALS: ALSPrototype) { | ||
this.ctx = new ALS<T>() | ||
} | ||
|
||
get(): T | undefined { | ||
return this.ctx.getStore() | ||
} | ||
|
||
async callAsync(val: T, cb: () => any) { | ||
return await this.ctx.run(val, cb) | ||
} | ||
} | ||
export class GlobalContext<T> implements Context<T> { | ||
value: T | undefined = undefined | ||
|
||
get(): T | undefined { | ||
return this.value | ||
} | ||
|
||
async callAsync(val: T, cb: () => any) { | ||
this.value = val | ||
return await cb() | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.