-
Notifications
You must be signed in to change notification settings - Fork 1.4k
feat: enhance locale handling with AsyncLocalStorage support for server-side requests #7826
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
Open
JerryWu1234
wants to merge
15
commits into
QwikDev:build/v2
Choose a base branch
from
JerryWu1234:buildv2async
base: build/v2
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
4664b34
feat: enhance locale handling with AsyncLocalStorage support for serv…
JerryWu1234 90890c9
add changeset
JerryWu1234 25d1648
fix: improve locale handling in resolveHead function with AsyncLocalS…
JerryWu1234 0abc9d8
refactor: streamline locale handling with AsyncLocalStorage for serve…
JerryWu1234 27dd795
refactor: remove unnecessary comment and simplify store retrieval in …
JerryWu1234 1c341e4
refactor: simplify AsyncLocalStorage usage and improve locale retriev…
JerryWu1234 92d67da
refactor: unify AsyncLocalStorage usage across router middleware and …
JerryWu1234 ce8d18b
Merge branch 'build/v2' into buildv2async
JerryWu1234 559b9f3
refactor: streamline AsyncLocalStorage usage in locale functions and …
JerryWu1234 0e148b7
refactor: simplify AsyncLocalStorage usage and improve locale retriev…
JerryWu1234 c1c07c6
refactor: remove debug log for AsyncLocalStorage in user-response.ts
JerryWu1234 888e4df
refactor: enhance AsyncLocalStorage integration in request handling a…
JerryWu1234 1609ffd
refactor: remove server-side check in setLocale and update asyncReque…
JerryWu1234 ef75798
refactor: optimize AsyncLocalStorage handling in locale functions and…
JerryWu1234 1293991
refactor: add server-side check for AsyncLocalStorage initialization …
JerryWu1234 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
Some comments aren't visible on the classic Files Changed page.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@qwik.dev/router': patch | ||
'@qwik.dev/core': patch | ||
--- | ||
|
||
enhance locale handling with AsyncLocalStorage support for server-side requests |
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
22 changes: 22 additions & 0 deletions
22
packages/qwik-router/src/middleware/request-handler/async-request-store.ts
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,22 @@ | ||
import { isServer } from 'packages/qwik/dist'; | ||
import type { RequestEventInternal } from './request-event'; | ||
import type { AsyncLocalStorage } from 'node:async_hooks'; | ||
|
||
export type AsyncStore = AsyncLocalStorage<RequestEventInternal>; | ||
|
||
if (isServer) { | ||
import('node:async_hooks') | ||
.then((module) => { | ||
const AsyncLocalStorage = module.AsyncLocalStorage; | ||
asyncRequestStore = new AsyncLocalStorage<RequestEventInternal>(); | ||
}) | ||
.catch((err) => { | ||
console.warn( | ||
'AsyncLocalStorage not available, continuing without it. This might impact concurrent server calls.', | ||
err | ||
); | ||
}); | ||
} | ||
|
||
// Qwik Core will also be using the async store if this is present | ||
export let asyncRequestStore: AsyncStore | undefined = undefined; |
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
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
JerryWu1234 marked this conversation as resolved.
Show resolved
Hide resolved
|
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
67 changes: 67 additions & 0 deletions
67
starters/apps/qwikrouter-test/src/routes/locale-concurrent/index.tsx
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,67 @@ | ||
import { component$, Resource, getLocale } from "@qwik.dev/core"; | ||
import type { RequestHandler } from "@qwik.dev/router"; | ||
import { routeLoader$ } from "@qwik.dev/router"; | ||
|
||
// Simple in-memory barrier to coordinate two concurrent requests in tests. | ||
type Barrier = { | ||
waiters: Set<string>; | ||
promise?: Promise<void>; | ||
resolve?: () => void; | ||
}; | ||
|
||
const barriers = new Map<string, Barrier>(); | ||
|
||
function getBarrier(group: string): Barrier { | ||
let b = barriers.get(group); | ||
if (!b) { | ||
b = { waiters: new Set() }; | ||
barriers.set(group, b); | ||
} | ||
return b; | ||
} | ||
|
||
function waitForBoth(group: string, id: string) { | ||
const barrier = getBarrier(group); | ||
if (!barrier.promise) { | ||
barrier.promise = new Promise<void>( | ||
(resolve) => (barrier.resolve = resolve), | ||
); | ||
} | ||
barrier.waiters.add(id); | ||
if (barrier.waiters.size >= 2) { | ||
barrier.resolve?.(); | ||
} | ||
return barrier.promise!; | ||
} | ||
|
||
export const onRequest: RequestHandler = ({ url, locale }) => { | ||
const qpLocale = url.searchParams.get("locale"); | ||
if (qpLocale) { | ||
locale(qpLocale); | ||
} | ||
}; | ||
|
||
export const useBarrier = routeLoader$(({ url }) => { | ||
const group = url.searchParams.get("group") || "default"; | ||
const id = url.searchParams.get("id") || Math.random().toString(36).slice(2); | ||
return waitForBoth(group, id).then(() => ({ done: true })); | ||
}); | ||
|
||
export default component$(() => { | ||
const barrier = useBarrier(); | ||
return ( | ||
<section> | ||
<p> | ||
Before barrier locale: <span class="locale-before">{getLocale()}</span> | ||
</p> | ||
<Resource | ||
value={barrier} | ||
onResolved={() => ( | ||
<p> | ||
After barrier locale: <span class="locale">{getLocale()}</span> | ||
</p> | ||
)} | ||
/> | ||
</section> | ||
); | ||
}); |
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,37 @@ | ||
import { expect, test } from "@playwright/test"; | ||
|
||
// This test ensures asyncRequestStore locale isolation across concurrent requests. | ||
// It triggers two concurrent server renders to the same route with different locales, | ||
// and uses a server-side barrier so the page reveals the locale only after both renders started. | ||
|
||
test.describe("Qwik Router concurrent locale", () => { | ||
test("should isolate locale per concurrent request", async ({ browser }) => { | ||
const ctx1 = await browser.newContext(); | ||
const ctx2 = await browser.newContext(); | ||
|
||
const page1 = await ctx1.newPage(); | ||
const page2 = await ctx2.newPage(); | ||
|
||
const url1 = | ||
"/qwikrouter-test/locale-concurrent?group=g&id=one&locale=en-US"; | ||
const url2 = | ||
"/qwikrouter-test/locale-concurrent?group=g&id=two&locale=fr-FR"; | ||
|
||
// Start both navigations without waiting them to finish | ||
const nav1 = page1.goto(url1); | ||
const nav2 = page2.goto(url2); | ||
|
||
await Promise.all([nav1, nav2]); | ||
|
||
// Before barrier render, locale is already set and visible in first block | ||
await expect(page1.locator(".locale-before")).toHaveText("en-US"); | ||
await expect(page2.locator(".locale-before")).toHaveText("fr-FR"); | ||
|
||
// After barrier releases, the bottom content renders and must preserve each locale | ||
await expect(page1.locator(".locale")).toHaveText("en-US"); | ||
await expect(page2.locator(".locale")).toHaveText("fr-FR"); | ||
|
||
await ctx1.close(); | ||
await ctx2.close(); | ||
}); | ||
}); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you say it has to be a global for server-functions.ts, but it's not using the global?