UNPKG

@netlify/remix-edge-adapter

Version:

Remix Adapter for Netlify Edge Functions

78 lines (73 loc) 2.3 kB
import { __commonJS, __toESM } from "./chunk-UJCSKKID.mjs"; // src/globals.ts var require_globals = __commonJS({ "src/globals.ts"() { "use strict"; globalThis.process ||= { env: Netlify.env.toObject() }; } }); // src/index.ts var import_globals = __toESM(require_globals()); // src/server.ts import { createRequestHandler as createRemixRequestHandler } from "@netlify/remix-runtime"; function createRequestHandler({ build, mode, getLoadContext }) { const remixHandler = createRemixRequestHandler(build, mode); const assetPath = build.assets.url.split("/").slice(0, -1).join("/"); return async (request, context) => { const { pathname } = new URL(request.url); if (pathname.startsWith(`${assetPath}/`)) { return; } try { const loadContext = await getLoadContext?.(request, context) || context; const response = await remixHandler(request, loadContext); response.headers.set("x-nf-runtime", "Edge"); if (response.status === 404) { const originResponse = await loadContext.next({ sendConditionalRequest: true }); if (originResponse && originResponse?.status !== 404) { return originResponse; } } return response; } catch (error) { console.error(error); return new Response("Internal Error", { status: 500 }); } }; } // src/hydrogen.ts var 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) { await p; } }; var getEnv = () => { if (globalThis.Netlify) { return globalThis.Netlify.env.toObject(); } return process.env; }; var createHydrogenAppLoadContext = async (request, netlifyContext, createAppLoadContext) => { const env = getEnv(); const userHydrogenContext = await createAppLoadContext(request, env, executionContext); return Object.assign(netlifyContext, userHydrogenContext); }; export { createHydrogenAppLoadContext, createRequestHandler };