Skip to content

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
wants to merge 6 commits into
base: build/v2
Choose a base branch
from

Conversation

JerryWu1234
Copy link
Contributor

@JerryWu1234 JerryWu1234 commented Aug 15, 2025

feat: enhance locale handling with AsyncLocalStorage support for server-side requests

#7806

What is it?

  • Feature / enhancement
  • Bug
  • Docs / tests / types / typos
  • Infra

Description

Checklist

  • My code follows the developer guidelines of this project
  • I performed a self-review of my own code
  • I added a changeset with pnpm change
  • I made corresponding changes to the Qwik docs
  • I added new tests to cover the fix / functionality

Copy link

changeset-bot bot commented Aug 15, 2025

🦋 Changeset detected

Latest commit: 1c341e4

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@JerryWu1234 JerryWu1234 changed the title feat: enhance locale handling with AsyncLocalStorage support for serv… feat: enhance locale handling with AsyncLocalStorage support for server-side requests Aug 15, 2025
Copy link

pkg-pr-new bot commented Aug 15, 2025

Open in StackBlitz

npm i https://pkg.pr.new/QwikDev/qwik/@qwik.dev/core@7826
npm i https://pkg.pr.new/QwikDev/qwik/@qwik.dev/router@7826
npm i https://pkg.pr.new/QwikDev/qwik/eslint-plugin-qwik@7826
npm i https://pkg.pr.new/QwikDev/qwik/create-qwik@7826

commit: 1c341e4

Copy link
Contributor

github-actions bot commented Aug 15, 2025

built with Refined Cloudflare Pages Action

⚡ Cloudflare Pages Deployment

Name Status Preview Last Commit
qwik-docs ✅ Ready (View Log) Visit Preview 1c341e4

@JerryWu1234 JerryWu1234 self-assigned this Aug 15, 2025
@JerryWu1234 JerryWu1234 marked this pull request as ready for review August 15, 2025 09:37
@JerryWu1234 JerryWu1234 requested a review from a team as a code owner August 15, 2025 09:37
@JerryWu1234
Copy link
Contributor Author

@wmertens does it need a test?.

Copy link
Member

@wmertens wmertens left a comment

Choose a reason for hiding this comment

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

Good start but I think Core should not be influenced by Router.

@JerryWu1234
Copy link
Contributor Author

@wmertens done .please check it again , thanks

@JerryWu1234 JerryWu1234 moved this from Backlog to Waiting For Review in Qwik Development Aug 19, 2025
@@ -37,9 +37,10 @@ export const resolveHead = (
}
return data;
}) as any as ResolveSyncValue;
const storeEv = (globalThis as any).qcAsyncRequestStore;
Copy link
Member

Choose a reason for hiding this comment

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

can you rename this to hasAsyncStore and add a comment // Qwik Core will also be using the async store if this is present

Actually, it would be better to export the store from where it is made and to import it.

instead of globalThis, make it export let asyncRequestStore and then in the other places it's used just import it. That way, we can be sure it is initialized correctly, and we don't pollute globalThis.

Comment on lines +33 to +41
try {
const locale = localAsyncStore?.getStore?.()?.locale;
if (locale) {
return locale;
}
} catch {
// ignore and fallback
}

Copy link
Member

Choose a reason for hiding this comment

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

if (localAsyncStore) {
  const locale = localAsyncStore.getStore()?.locale
  if (locale) {
    return locale
  }
}

don't add try/catch and conditionals when not needed, it has a runtime cost.

Comment on lines +6 to +10
type LocaleStore = { locale: string | undefined };

type LocaleAsyncStore = import('node:async_hooks').AsyncLocalStorage<LocaleStore>;

let localAsyncStore: LocaleAsyncStore | undefined;
Copy link
Member

Choose a reason for hiding this comment

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

import type {AsyncLocalStorage} from 'node:async_hooks'

let asyncStore: AsyncLocalStorage<{locale?: string}> | undefined

Comment on lines +62 to +68
try {
if (localAsyncStore?.run) {
return localAsyncStore.run({ locale }, fn);
}
} catch {
// ignore and fallback
}
Copy link
Member

Choose a reason for hiding this comment

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

if (localAsyncStore) {
  return localAsyncStore.run({ locale }, fn);
}

Comment on lines +88 to +97
// On the server, prefer setting the locale on the local per-request store
try {
const store = localAsyncStore?.getStore?.();
if (store) {
store.locale = locale;
return;
}
} catch {
// ignore and fallback
}
Copy link
Member

Choose a reason for hiding this comment

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

see above

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Waiting For Review
Development

Successfully merging this pull request may close these issues.

2 participants