Skip to content

Commit

Permalink
fix: simplify logic and cache the client per hostname on the server
Browse files Browse the repository at this point in the history
  • Loading branch information
Plopix committed Jul 23, 2024
1 parent bbf06fa commit 8805839
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 28 deletions.
20 changes: 8 additions & 12 deletions shared/application/use-cases/checkout/handleSaveCart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,14 @@ export default async (body: any, { apiClient }: Deps, markets?: string[]) => {
quantity: item.quantity,
}));

if (cartId) {
try {
return await hydrateCart(localCartItems, { apiClient }, cartId, markets);
} catch (error: any) {
if (error.message.includes('placed')) {
console.log('Cart has been placed, creating a new one');
return await hydrateCart(localCartItems, { apiClient }, undefined, markets);
}
throw error;
try {
return await hydrateCart(localCartItems, { apiClient }, cartId, markets);
} catch (error: any) {
if (error.message.includes('placed')) {
console.log('Cart has been placed, creating a new one');
return await hydrateCart(localCartItems, { apiClient }, undefined, markets);
}
throw error;
}
if (!cartId) {
return await hydrateCart(localCartItems, { apiClient }, undefined, markets);
}

};
47 changes: 31 additions & 16 deletions shared/application/use-cases/storefront.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
createFilesystemAdapter,
createMemoryAdapter,
TStoreFrontConfig,
TStoreFront,
} from '@crystallize/js-storefrontaware-utils';
import { configureStorage } from './storage.server';
import {
Expand All @@ -20,6 +21,13 @@ const storage = configureStorage(`${process.env?.STORAGE_DSN}`, {
prefix: 'superfast-',
});

declare global {
var __api_clients: Record<string, {
shared: TStoreFront;
secret: TStoreFront;
}>;
}

export const getStoreFront = async (hostname: string) => {
const adapter = (() => {
// if there is a file for the StoreFront, use that, note that it's safer to use the MemoryAdapter
Expand Down Expand Up @@ -85,24 +93,31 @@ export const getStoreFront = async (hostname: string) => {

const createClientOptions: CreateClientOptions | undefined = process.env?.ENABLE_JS_API_CLIENT_PROFILING
? {
profiling: {
onRequestResolved: ({ resolutionTimeMs, serverTimeMs }, query, variables) => {
const timings = `[JS-API-CLIENT]: Request processed in ${serverTimeMs}ms, resolved in ${resolutionTimeMs}ms.`;
if (process.env?.ENABLE_JS_API_CLIENT_PROFILING === 'verbose') {
console.log(timings, { query, variables });
return;
}
console.log(timings);
},
},
}
profiling: {
onRequestResolved: ({ resolutionTimeMs, serverTimeMs }, query, variables) => {
const timings = `[JS-API-CLIENT]: Request processed in ${serverTimeMs}ms, resolved in ${resolutionTimeMs}ms.`;
if (process.env?.ENABLE_JS_API_CLIENT_PROFILING === 'verbose') {
console.log(timings, { query, variables });
return;
}
console.log(timings);
},
},
}
: undefined;

const [shared, secret] = await Promise.all([
createStoreFront(adapter, false, createClientOptions),
createStoreFront(adapter, true, createClientOptions),
]);
return { shared, secret };

if (!globalThis.__api_clients) {
globalThis.__api_clients = {};
}
if (!globalThis.__api_clients[hostname]) {
const [shared, secret] = await Promise.all([
createStoreFront(adapter, false, createClientOptions),
createStoreFront(adapter, true, createClientOptions),
]);
globalThis.__api_clients[hostname] = { shared, secret };
}
return globalThis.__api_clients[hostname];
};

/**
Expand Down

0 comments on commit 8805839

Please sign in to comment.