From 93ff2d3207cfb798ded1e55fd67015e175212897 Mon Sep 17 00:00:00 2001 From: Wes Bos Date: Thu, 23 Feb 2023 09:10:33 -0500 Subject: [PATCH] fix netlify edge functions with assets --- netlify-server.ts | 63 ----------------------------------------------- server.ts | 5 ++-- 2 files changed, 3 insertions(+), 65 deletions(-) delete mode 100644 netlify-server.ts diff --git a/netlify-server.ts b/netlify-server.ts deleted file mode 100644 index d016a1d5..00000000 --- a/netlify-server.ts +++ /dev/null @@ -1,63 +0,0 @@ -import type { AppLoadContext, ServerBuild } from '@netlify/remix-runtime' -import { createRequestHandler as createRemixRequestHandler } from '@netlify/remix-runtime' -import type { Context } from '@netlify/edge-functions' - -type LoadContext = AppLoadContext & Context - -/** - * A function that returns the value to use as `context` in route `loader` and - * `action` functions. - * - * You can think of this as an escape hatch that allows you to pass - * environment/platform-specific values through to your loader/action. - */ -export type GetLoadContextFunction = (request: Request, context: Context) => Promise | LoadContext - -export type RequestHandler = (request: Request, context: LoadContext) => Promise - -export function createRequestHandler({ - build, - mode, - getLoadContext, -}: { - build: ServerBuild - mode?: string - getLoadContext?: GetLoadContextFunction -}): RequestHandler { - const remixHandler = createRemixRequestHandler(build, mode) - - const assetPath = build.assets.url.split('/').slice(0, -1).join('/') - - return async (request: Request, context: LoadContext): Promise => { - const { pathname } = new URL(request.url) - // Skip the handler for static files - if (pathname.startsWith(`${assetPath}/`)) { - // Temporary fix - passing the request to the Netlify static asset handler causes a 203 Not Content error. Passing the through remix works, but I asssume isn't ideal - // console.log('Skipping Remix handler for static file', pathname) - // return; - } - try { - const loadContext = (await getLoadContext?.(request, context)) || context - - const response = await remixHandler(request, loadContext) - - // A useful header for debugging - response.headers.set('x-nf-runtime', 'Edge') - - if (response.status === 404) { - // Check if there is a matching static file - const originResponse = await context.next({ - sendConditionalRequest: true, - }) - if (originResponse.status !== 404) { - return originResponse - } - } - return response - } catch (error: unknown) { - console.error(error) - - return new Response('Internal Error', { status: 500 }) - } - } -} diff --git a/server.ts b/server.ts index 87c1eedb..94a3230e 100644 --- a/server.ts +++ b/server.ts @@ -1,7 +1,6 @@ // Import path interpreted by the Remix compiler import * as build from "@remix-run/dev/server-build"; -// import { createRequestHandler } from "@netlify/remix-edge-adapter"; -import { createRequestHandler } from "./netlify-server"; +import { createRequestHandler } from "@netlify/remix-edge-adapter"; export default createRequestHandler({ build, @@ -12,4 +11,6 @@ export default createRequestHandler({ export const config = { cache: "manual", path: "/*", + // Pass all assets to the netlify asset server + excluded_patterns: ["/_assets/*"], };