Skip to content

Commit

Permalink
chore: more standard app/lib/context
Browse files Browse the repository at this point in the history
  • Loading branch information
serhalp committed Aug 30, 2024
1 parent 24ca92a commit 25e206d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
8 changes: 5 additions & 3 deletions app/lib/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import {createHydrogenContext, InMemoryCache} from '@shopify/hydrogen';
import {AppSession} from '~/lib/session';
import {CART_QUERY_FRAGMENT} from '~/lib/fragments';

type WaitUntil = (promise: Promise<unknown>) => void;
interface ExecutionContext {
waitUntil(promise: Promise<unknown>): Promise<void>;
}

/**
* The context implementation is separate from server.ts
Expand All @@ -11,7 +13,7 @@ type WaitUntil = (promise: Promise<unknown>) => void;
export async function createAppLoadContext(
request: Request,
env: Env,
waitUntil: WaitUntil,
executionContext: ExecutionContext,
) {
/**
* Open a cache instance in the worker and a custom session instance.
Expand All @@ -26,7 +28,7 @@ export async function createAppLoadContext(
env,
request,
cache: new InMemoryCache(),
waitUntil,
waitUntil: executionContext.waitUntil,
session,
i18n: {language: 'EN', country: 'US'},
cart: {
Expand Down
4 changes: 0 additions & 4 deletions netlify.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
command = "npm run build"
publish = "dist/client"

[dev]
# FIXME(serhalp) This is `npm run build` in default Hydrogen config for some reason
command = "npm run dev"

# Set immutable caching for static files, because they have fingerprinted filenames
[[headers]]
for = "/build/*"
Expand Down
20 changes: 11 additions & 9 deletions server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ import {storefrontRedirect} from '@shopify/hydrogen';
import {broadcastDevReady} from '@netlify/remix-runtime';
import {createAppLoadContext} from '~/lib/context';

/**
* Hydrogen expects a `waitUntil` function like the one in the workerd runtime:
* https://developers.cloudflare.com/workers/runtime-apis/context/#waituntil.
* Netlify Edge Functions don't have such a function, but Deno Deploy isolates make a best-effort
* attempt to wait for the event loop to drain, so just awaiting the promise here is equivalent.
*/
const waitUntil = async (p: Promise<unknown>): Promise<void> => {
await p;
const executionContext = {
/**
* Hydrogen expects a `waitUntil` function like the one in the workerd runtime:
* https://developers.cloudflare.com/workers/runtime-apis/context/#waituntil.
* Netlify Edge Functions don't have such a function, but Deno Deploy isolates make a best-effort
* attempt to wait for the event loop to drain, so just awaiting the promise here is equivalent.
*/
async waitUntil(p: Promise<unknown>): Promise<void> {
await p;
},
};

export default async function handler(
Expand All @@ -25,7 +27,7 @@ export default async function handler(

const appLoadContext = {
...netlifyContext,
...(await createAppLoadContext(request, env, waitUntil)),
...(await createAppLoadContext(request, env, executionContext)),
};

if (
Expand Down

0 comments on commit 25e206d

Please sign in to comment.