Skip to content
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

feat(core): add localized data-fetching #1703

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/sixty-pots-lay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@bigcommerce/catalyst-core": minor
---

Adds localized data fetching withing the beforeRequest client helper. If information is translated (currently possible to update via the Admin GraphQL API) then we will return the translated product data. See https://developer.bigcommerce.com/docs/store-operations/catalog/graphql-admin/product-basic-info for more information on how to use overrides.
62 changes: 37 additions & 25 deletions core/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
import { createClient } from '@bigcommerce/catalyst-client';
import { headers } from 'next/headers';
import { getLocale } from 'next-intl/server';
import { getLocale as getServerLocale } from 'next-intl/server';

import { getChannelIdFromLocale } from '../channels.config';
import { backendUserAgent } from '../userAgent';

const getLocale = async () => {
try {
const locale = await getServerLocale();

return locale;
} catch {
/**
* Next-intl `getLocale` only works on the server, and when middleware has run.
*
* Instances when `getLocale` will not work:
* - Requests in middlewares
* - Requests in `generateStaticParams`
* - Request in api routes
* - Requests in static sites without `setRequestLocale`
*/
}
};

export const client = createClient({
storefrontToken: process.env.BIGCOMMERCE_STOREFRONT_TOKEN ?? '',
xAuthToken: process.env.BIGCOMMERCE_ACCESS_TOKEN ?? '',
Expand All @@ -15,37 +33,31 @@ export const client = createClient({
(process.env.NODE_ENV !== 'production' && process.env.CLIENT_LOGGER !== 'false') ||
process.env.CLIENT_LOGGER === 'true',
getChannelId: async (defaultChannelId: string) => {
/**
* Next-intl `getLocale` only works on the server, and when middleware has run.
*
* Instances when `getLocale` will not work:
* - Requests in middlewares
* - Requests in `generateStaticParams`
* - Request in api routes
* - Requests in static sites without `setRequestLocale`
*
* We use the default channelId as a fallback, but it is not ideal in some scenarios.
* */
try {
const locale = await getLocale();

return getChannelIdFromLocale(locale) ?? defaultChannelId;
} catch {
return defaultChannelId;
}
const locale = await getLocale();

// We use the default channelId as a fallback, but it is not ideal in some scenarios.
return getChannelIdFromLocale(locale) ?? defaultChannelId;
},
beforeRequest: async (fetchOptions) => {
// We can't serialize a `Headers` object within this method so we have to opt into using a plain object
const requestHeaders: Record<string, string> = {};
const locale = await getLocale();

if (fetchOptions?.cache && ['no-store', 'no-cache'].includes(fetchOptions.cache)) {
const ipAddress = (await headers()).get('X-Forwarded-For');

if (ipAddress) {
return {
headers: {
'X-Forwarded-For': ipAddress,
'True-Client-IP': ipAddress,
},
};
requestHeaders['X-Forwarded-For'] = ipAddress;
requestHeaders['True-Client-IP'] = ipAddress;
}
}

if (locale) {
requestHeaders['Accept-Language'] = locale;
}

return {
headers: requestHeaders,
};
},
});
Loading