Skip to content

Commit 71f3a75

Browse files
jorgemoyaclaude
andcommitted
fix(core): pass locale to beforeRequest for Accept-Language header
Inside unstable_cache, getLocale() fails silently (returns undefined) because it calls headers() internally. Pass the explicit locale from client.fetch() through to the beforeRequest callback so Accept-Language is set correctly even inside cached contexts. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent da7e7df commit 71f3a75

2 files changed

Lines changed: 9 additions & 5 deletions

File tree

core/client/index.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export const client = createClient({
4747
// We use the default channelId as a fallback, but it is not ideal in some scenarios.
4848
return getChannelIdFromLocale(resolvedLocale) ?? defaultChannelId;
4949
},
50-
beforeRequest: async (fetchOptions) => {
50+
beforeRequest: async (fetchOptions, locale) => {
5151
// We can't serialize a `Headers` object within this method so we have to opt into using a plain object
5252
const requestHeaders: Record<string, string> = {};
5353

@@ -67,10 +67,12 @@ export const client = createClient({
6767
}
6868
}
6969

70-
const locale = await getLocale();
70+
// Use explicit locale when available (required inside unstable_cache where
71+
// getLocale() would fail), fall back to next-intl for normal requests.
72+
const resolvedLocale = locale ?? (await getLocale());
7173

72-
if (locale) {
73-
requestHeaders['Accept-Language'] = locale;
74+
if (resolvedLocale) {
75+
requestHeaders['Accept-Language'] = resolvedLocale;
7476
}
7577

7678
return {

packages/client/src/client.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ interface Config<FetcherRequestInit extends RequestInit = RequestInit> {
2323
getChannelId?: (defaultChannelId: string, locale?: string) => Promise<string> | string;
2424
beforeRequest?: (
2525
fetchOptions?: FetcherRequestInit,
26+
locale?: string,
2627
) => Promise<Partial<FetcherRequestInit> | undefined> | Partial<FetcherRequestInit> | undefined;
2728
onError?: (
2829
error: BigCommerceGQLError,
@@ -52,6 +53,7 @@ class Client<FetcherRequestInit extends RequestInit = RequestInit> {
5253
private getChannelId: (defaultChannelId: string, locale?: string) => Promise<string> | string;
5354
private beforeRequest?: (
5455
fetchOptions?: FetcherRequestInit,
56+
locale?: string,
5557
) => Promise<Partial<FetcherRequestInit> | undefined> | Partial<FetcherRequestInit> | undefined;
5658
private onError?: (
5759
error: BigCommerceGQLError,
@@ -133,7 +135,7 @@ class Client<FetcherRequestInit extends RequestInit = RequestInit> {
133135
locale,
134136
);
135137
const { headers: additionalFetchHeaders = {}, ...additionalFetchOptions } =
136-
(await this.beforeRequest?.(fetchOptions)) ?? {};
138+
(await this.beforeRequest?.(fetchOptions, locale)) ?? {};
137139

138140
const response = await fetch(graphqlUrl, {
139141
method: 'POST',

0 commit comments

Comments
 (0)