Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 5 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,8 @@ B2B_API_HOST=https://api-b2b.bigcommerce.com
B2B_API_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

# Base URL for a production build of Buyer Portal. Uncomment if connecting to a deployed custom Buyer Portal.
# Example URL format: ${PROD_BUYER_PORTAL_URL}/index.js
# PROD_BUYER_PORTAL_URL=https://my-b2b-catalyst.com/b2b/20260101
15 changes: 15 additions & 0 deletions core/b2b/loader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import { auth } from '~/auth';

import { ScriptDev } from './script-dev';
import { ScriptProduction } from './script-production';
import { ScriptProductionCustom } from './script-production-custom';

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(),
PROD_BUYER_PORTAL_URL: z.string().url().optional(),
Comment thread
CNanninga marked this conversation as resolved.
Outdated
STAGING_B2B_CDN_ORIGIN: z.string().optional(),
});

Expand All @@ -17,6 +19,7 @@ export async function B2BLoader() {
BIGCOMMERCE_STORE_HASH,
BIGCOMMERCE_CHANNEL_ID,
LOCAL_BUYER_PORTAL_HOST,
PROD_BUYER_PORTAL_URL,
STAGING_B2B_CDN_ORIGIN,
} = EnvironmentSchema.parse(process.env);

Expand All @@ -33,6 +36,18 @@ export async function B2BLoader() {
/>
);
}

if (PROD_BUYER_PORTAL_URL) {
return (
<ScriptProductionCustom
cartId={session?.user?.cartId}
channelId={BIGCOMMERCE_CHANNEL_ID}
storeHash={BIGCOMMERCE_STORE_HASH}
token={session?.b2bToken}
prodUrl={PROD_BUYER_PORTAL_URL}
/>
);
}

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

Expand Down
67 changes: 67 additions & 0 deletions core/b2b/script-production-custom.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
'use client';

import Script from 'next/script';

import { useB2BAuth } from './use-b2b-auth';
import { useB2BCart } from './use-b2b-cart';

/**
* Use these vars if using build hashes in B2B Buyer Portal files.
*/
const hashIndex = undefined;
const hashIndexLegacy = undefined;
const hashPolyfills = undefined;
Comment thread
CNanninga marked this conversation as resolved.
Outdated

interface Props {
storeHash: string;
channelId: string;
token?: string;
cartId?: string | null;
prodUrl: string;
}

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

return (
<>
<Script>
{`
window.b3CheckoutConfig = {
routes: {
dashboard: '/#/dashboard',
},
}
window.B3 = {
setting: {
store_hash: '${storeHash}',
channel_id: ${channelId},
},
}
`}
</Script>
<Script
type="module"
crossOrigin=""
src={`${prodUrl}/index${hashIndex ? `.${hashIndex}` : ''}.js`}
></Script>
<Script
noModule
crossOrigin=""
src={`${prodUrl}/polyfills-legacy${hashPolyfills ? `.${hashPolyfills}` : ''}.js`}
></Script>
<Script
noModule
crossOrigin=""
src={`${prodUrl}/index-legacy${hashIndexLegacy ? `.${hashIndexLegacy}` : ''}.js`}
></Script>
</>
);
}
Loading