-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1f11818
commit ba16940
Showing
5 changed files
with
173 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
declare global { | ||
/** | ||
* A global `process` object is only available during build to access NODE_ENV. | ||
*/ | ||
const process: {env: {NODE_ENV: 'production' | 'development'} & Env}; | ||
|
||
/** | ||
* Declare expected Env parameter in fetch handler. | ||
*/ | ||
interface Env { | ||
SESSION_SECRET: string; | ||
PUBLIC_STOREFRONT_API_TOKEN: string; | ||
PRIVATE_STOREFRONT_API_TOKEN: string; | ||
PUBLIC_STORE_DOMAIN: string; | ||
PUBLIC_STOREFRONT_ID: string; | ||
} | ||
} | ||
|
||
/** | ||
* Declare local additions to `AppLoadContext` to include the session utilities we injected in `server.ts`. | ||
*/ | ||
declare module '@shopify/remix-oxygen' { | ||
export interface AppLoadContext { | ||
waitUntil: ExecutionContext['waitUntil']; | ||
session; | ||
storefront; | ||
env: Env; | ||
} | ||
} | ||
|
||
// Needed to make this file a module. | ||
export {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Virtual entry point for the app | ||
import * as remixBuild from '@remix-run/dev/server-build'; | ||
import {createRequestHandler} from '@remix-run/server-runtime'; | ||
import {createStorefrontClient} from '@shopify/hydrogen'; | ||
|
||
import {HydrogenSession} from '~/lib/session.server'; | ||
import {getLocaleFromRequest} from '~/lib/utils'; | ||
|
||
/** | ||
* Export a fetch handler in module format. | ||
*/ | ||
export default async function (request: Request): Promise { | ||
try { | ||
/** | ||
* This has to be done so messy because process.env can't be destructured | ||
* and only variables explicitly named are present inside a Vercel Edge Function. | ||
* See https://github.com/vercel/next.js/pull/31237/files | ||
*/ | ||
const env: Env = { | ||
SESSION_SECRET: '', | ||
PUBLIC_STOREFRONT_API_TOKEN: '', | ||
PRIVATE_STOREFRONT_API_TOKEN: '', | ||
PUBLIC_STORE_DOMAIN: '', | ||
}; | ||
env.SESSION_SECRET = process.env.SESSION_SECRET; | ||
env.PUBLIC_STOREFRONT_API_TOKEN = process.env.PUBLIC_STOREFRONT_API_TOKEN; | ||
env.PRIVATE_STOREFRONT_API_TOKEN = process.env.PRIVATE_STOREFRONT_API_TOKEN; | ||
env.PUBLIC_STORE_DOMAIN = process.env.PUBLIC_STORE_DOMAIN; | ||
/** | ||
* Open a cache instance in the worker and a custom session instance. | ||
*/ | ||
if (!env?.SESSION_SECRET) { | ||
throw new Error('SESSION_SECRET process.environment variable is not set'); | ||
} | ||
|
||
const [session] = await Promise.all([ | ||
HydrogenSession.init(request, [process.env.SESSION_SECRET]), | ||
]); | ||
|
||
/** | ||
* Create Hydrogen's Storefront client. | ||
*/ | ||
const {storefront} = createStorefrontClient({ | ||
buyerIp: request.headers.get('x-forwarded-for') ?? undefined, | ||
i18n: getLocaleFromRequest(request), | ||
publicStorefrontToken: env.PUBLIC_STOREFRONT_API_TOKEN, | ||
privateStorefrontToken: env.PRIVATE_STOREFRONT_API_TOKEN, | ||
storeDomain: env.PUBLIC_STORE_DOMAIN, | ||
storefrontId: env.PUBLIC_STOREFRONT_ID, | ||
// storefrontId: process.env.PUBLIC_STOREFRONT_ID, | ||
// requestGroupId: request.headers.get('request-id'), | ||
}); | ||
|
||
const handleRequest = createRequestHandler(remixBuild as any, 'production'); | ||
|
||
const response = await handleRequest(request, { | ||
session, | ||
storefront, | ||
env, | ||
waitUntil: () => Promise.resolve(), | ||
}); | ||
|
||
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}); | ||
} | ||
|
||
return response; | ||
} catch (error) { | ||
// eslint-disable-next-line no-console | ||
console.error(error); | ||
return new Response('An unexpected error occurred', {status: 500}); | ||
} | ||
} |