Skip to content
Open
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
7 changes: 6 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,9 @@ B2B_API_HOST=https://api-b2b.bigcommerce.com
BIGCOMMERCE_ACCESS_TOKEN=

# URL for the local buyer portal instance. Uncomment if developing locally.
# LOCAL_BUYER_PORTAL_HOST=http://localhost:3001
# LOCAL_BUYER_PORTAL_HOST=http://localhost:3001

# If you don't need local buyer portal but you need to work in a different environment
# this variable will supply the correct microapps cdn url.
# It will default to "production".
# BUYER_PORTAL_ENVIRONMENT=production
8 changes: 3 additions & 5 deletions core/b2b/loader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ const EnvironmentSchema = z.object({
BIGCOMMERCE_STORE_HASH: z.string({ message: 'BIGCOMMERCE_STORE_HASH is required' }),
BIGCOMMERCE_CHANNEL_ID: z.string({ message: 'BIGCOMMERCE_CHANNEL_ID is required' }),
LOCAL_BUYER_PORTAL_HOST: z.string().url().optional(),
STAGING_B2B_CDN_ORIGIN: z.string().optional(),
BUYER_PORTAL_ENVIRONMENT: z.enum(['production', 'staging', 'integration']).optional().default('production'),
});

export async function B2BLoader() {
const {
BIGCOMMERCE_STORE_HASH,
BIGCOMMERCE_CHANNEL_ID,
LOCAL_BUYER_PORTAL_HOST,
STAGING_B2B_CDN_ORIGIN,
BUYER_PORTAL_ENVIRONMENT,
} = EnvironmentSchema.parse(process.env);

const session = await auth();
Expand All @@ -34,13 +34,11 @@ export async function B2BLoader() {
);
}

const environment = STAGING_B2B_CDN_ORIGIN === 'true' ? 'staging' : 'production';

return (
<ScriptProduction
cartId={session?.user?.cartId}
channelId={BIGCOMMERCE_CHANNEL_ID}
environment={environment}
environment={BUYER_PORTAL_ENVIRONMENT}
storeHash={BIGCOMMERCE_STORE_HASH}
token={session?.b2bToken}
/>
Expand Down
12 changes: 10 additions & 2 deletions core/b2b/script-production.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,22 @@ interface Props {
storeHash: string;
channelId: string;
token?: string;
environment: 'staging' | 'production';
environment: 'staging' | 'production' | 'integration';
cartId?: string | null;
}

const CDN_BY_ENV: Record<Props['environment'], string> = {
production: 'https://microapps.bigcommerce.com',
staging: 'https://microapps.staging.zone',
integration: 'https://microapps.integration.zone',
};

export function ScriptProduction({ cartId, storeHash, channelId, token, environment }: Props) {
useB2BAuth(token);
useB2BCart(cartId);

const src = `${CDN_BY_ENV[environment]}/b2b-buyer-portal/headless.js`;

return (
<>
<Script id="b2b-config">
Expand All @@ -35,7 +43,7 @@ export function ScriptProduction({ cartId, storeHash, channelId, token, environm
data-channelid={channelId}
data-environment={environment}
data-storehash={storeHash}
src={'https://microapps.bigcommerce.com/b2b-buyer-portal/headless.js'}
src={src}
type="module"
/>
</>
Expand Down