Skip to content

Commit 69e0b4a

Browse files
committed
chore: improve the way we supply b2b cdn url
1 parent ab06c1b commit 69e0b4a

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

.env.example

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,9 @@ B2B_API_HOST=https://api-b2b.bigcommerce.com
5151
BIGCOMMERCE_ACCESS_TOKEN=
5252

5353
# URL for the local buyer portal instance. Uncomment if developing locally.
54-
# LOCAL_BUYER_PORTAL_HOST=http://localhost:3001
54+
# LOCAL_BUYER_PORTAL_HOST=http://localhost:3001
55+
56+
# If you don't need local buyer portal but you need to work in a different environment
57+
# this variable will supply the correct microapps cdn url.
58+
# It will default to "production".
59+
# BUYER_PORTAL_ENVIRONMENT=production

core/b2b/loader.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ const EnvironmentSchema = z.object({
99
BIGCOMMERCE_STORE_HASH: z.string({ message: 'BIGCOMMERCE_STORE_HASH is required' }),
1010
BIGCOMMERCE_CHANNEL_ID: z.string({ message: 'BIGCOMMERCE_CHANNEL_ID is required' }),
1111
LOCAL_BUYER_PORTAL_HOST: z.string().url().optional(),
12-
STAGING_B2B_CDN_ORIGIN: z.string().optional(),
12+
BUYER_PORTAL_ENVIRONMENT: z.enum(['production', 'staging', 'integration']).optional().default('production'),
1313
});
1414

1515
export async function B2BLoader() {
1616
const {
1717
BIGCOMMERCE_STORE_HASH,
1818
BIGCOMMERCE_CHANNEL_ID,
1919
LOCAL_BUYER_PORTAL_HOST,
20-
STAGING_B2B_CDN_ORIGIN,
20+
BUYER_PORTAL_ENVIRONMENT,
2121
} = EnvironmentSchema.parse(process.env);
2222

2323
const session = await auth();
@@ -34,13 +34,11 @@ export async function B2BLoader() {
3434
);
3535
}
3636

37-
const environment = STAGING_B2B_CDN_ORIGIN === 'true' ? 'staging' : 'production';
38-
3937
return (
4038
<ScriptProduction
4139
cartId={session?.user?.cartId}
4240
channelId={BIGCOMMERCE_CHANNEL_ID}
43-
environment={environment}
41+
environment={BUYER_PORTAL_ENVIRONMENT}
4442
storeHash={BIGCOMMERCE_STORE_HASH}
4543
token={session?.b2bToken}
4644
/>

core/b2b/script-production.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,22 @@ interface Props {
99
storeHash: string;
1010
channelId: string;
1111
token?: string;
12-
environment: 'staging' | 'production';
12+
environment: 'staging' | 'production' | 'integration';
1313
cartId?: string | null;
1414
}
1515

16+
const CDN_BY_ENV: Record<Props['environment'], string> = {
17+
production: 'https://microapps.bigcommerce.com',
18+
staging: 'https://microapps.staging.zone',
19+
integration: 'https://microapps.integration.zone',
20+
};
21+
1622
export function ScriptProduction({ cartId, storeHash, channelId, token, environment }: Props) {
1723
useB2BAuth(token);
1824
useB2BCart(cartId);
1925

26+
const src = `${CDN_BY_ENV[environment]}/b2b-buyer-portal/headless.js`;
27+
2028
return (
2129
<>
2230
<Script id="b2b-config">
@@ -35,7 +43,7 @@ export function ScriptProduction({ cartId, storeHash, channelId, token, environm
3543
data-channelid={channelId}
3644
data-environment={environment}
3745
data-storehash={storeHash}
38-
src={'https://microapps.bigcommerce.com/b2b-buyer-portal/headless.js'}
46+
src={src}
3947
type="module"
4048
/>
4149
</>

0 commit comments

Comments
 (0)