@shopify/cli
Version:
A CLI tool to build for the Shopify platform
62 lines (55 loc) • 1.71 kB
text/typescript
// Virtual entry point for the app
import {storefrontRedirect} from '@shopify/hydrogen';
import {createRequestHandler} from '@shopify/hydrogen/oxygen';
import {createHydrogenRouterContext} from '~/lib/context';
/**
* Export a fetch handler in module format.
*/
export default {
async fetch(
request: Request,
env: Env,
executionContext: ExecutionContext,
): Promise<Response> {
try {
const hydrogenContext = await createHydrogenRouterContext(
request,
env,
executionContext,
);
/**
* Create a Remix request handler and pass
* Hydrogen's Storefront client to the loader context.
*/
const handleRequest = createRequestHandler({
// eslint-disable-next-line import/no-unresolved
build: await import('virtual:react-router/server-build'),
mode: process.env.NODE_ENV,
getLoadContext: () => hydrogenContext,
});
const response = await handleRequest(request);
if (hydrogenContext.session.isPending) {
response.headers.set(
'Set-Cookie',
await hydrogenContext.session.commit(),
);
}
if (response.status === 404) {
/**
* Check for redirects only when there's a 404 from the app.
* If the redirect doesn't exist, then `storefrontRedirect`
* will pass through the 404 response.
*/
return storefrontRedirect({
request,
response,
storefront: hydrogenContext.storefront,
});
}
return response;
} catch (error) {
console.error(error);
return new Response('An unexpected error occurred', {status: 500});
}
},
};