From 75bb6e3849daceb569c595d59d720e74ba64b4a0 Mon Sep 17 00:00:00 2001 From: Onur Temizkan Date: Mon, 19 May 2025 17:49:07 +0100 Subject: [PATCH 1/2] fix(remix): Accept and return plain `ServerBuild` on Cloudflare `instrumentBuild` --- packages/remix/src/cloudflare/index.ts | 8 ++++++-- packages/remix/src/server/instrumentServer.ts | 6 +----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/remix/src/cloudflare/index.ts b/packages/remix/src/cloudflare/index.ts index 8ef997dc9e1c..ee40e6910e94 100644 --- a/packages/remix/src/cloudflare/index.ts +++ b/packages/remix/src/cloudflare/index.ts @@ -1,3 +1,4 @@ +import type { ServerBuild } from '@remix-run/server-runtime'; import { instrumentBuild as instrumentRemixBuild, makeWrappedCreateRequestHandler, @@ -14,11 +15,14 @@ export { makeWrappedCreateRequestHandler, sentryHandleError }; * Instruments a Remix build to capture errors and performance data. * @param build The Remix build to instrument. * @returns The instrumented Remix build. + * + * Note: CreateRequestHandlerFunction from @shopify/remix-oxygen accepts a ServerBuild, not a function unlike the rest of the Remix ecosystem + * That's why we accept and return a ServerBuild type here. */ -export const instrumentBuild: typeof instrumentRemixBuild = build => { +export const instrumentBuild = (build: ServerBuild): ServerBuild => { return instrumentRemixBuild(build, { instrumentTracing: true, - }); + }) as ServerBuild; }; export type { diff --git a/packages/remix/src/server/instrumentServer.ts b/packages/remix/src/server/instrumentServer.ts index 3417188cc7d5..1486080039ca 100644 --- a/packages/remix/src/server/instrumentServer.ts +++ b/packages/remix/src/server/instrumentServer.ts @@ -432,11 +432,7 @@ export function instrumentBuild( export const makeWrappedCreateRequestHandler = (options?: { instrumentTracing?: boolean }) => function (origCreateRequestHandler: CreateRequestHandlerFunction): CreateRequestHandlerFunction { - return function ( - this: unknown, - build: ServerBuild | (() => ServerBuild | Promise), - ...args: unknown[] - ): RequestHandler { + return function (this: unknown, build, ...args: unknown[]): RequestHandler { const newBuild = instrumentBuild(build, options); const requestHandler = origCreateRequestHandler.call(this, newBuild, ...args); From ad506ac8ae60d9b400e2e7dead2fb486e386a9b3 Mon Sep 17 00:00:00 2001 From: Onur Temizkan Date: Mon, 19 May 2025 21:49:05 +0100 Subject: [PATCH 2/2] Use generic types instead --- packages/remix/src/cloudflare/index.ts | 8 ++------ packages/remix/src/server/instrumentServer.ts | 19 ++++++++----------- 2 files changed, 10 insertions(+), 17 deletions(-) diff --git a/packages/remix/src/cloudflare/index.ts b/packages/remix/src/cloudflare/index.ts index ee40e6910e94..8ef997dc9e1c 100644 --- a/packages/remix/src/cloudflare/index.ts +++ b/packages/remix/src/cloudflare/index.ts @@ -1,4 +1,3 @@ -import type { ServerBuild } from '@remix-run/server-runtime'; import { instrumentBuild as instrumentRemixBuild, makeWrappedCreateRequestHandler, @@ -15,14 +14,11 @@ export { makeWrappedCreateRequestHandler, sentryHandleError }; * Instruments a Remix build to capture errors and performance data. * @param build The Remix build to instrument. * @returns The instrumented Remix build. - * - * Note: CreateRequestHandlerFunction from @shopify/remix-oxygen accepts a ServerBuild, not a function unlike the rest of the Remix ecosystem - * That's why we accept and return a ServerBuild type here. */ -export const instrumentBuild = (build: ServerBuild): ServerBuild => { +export const instrumentBuild: typeof instrumentRemixBuild = build => { return instrumentRemixBuild(build, { instrumentTracing: true, - }) as ServerBuild; + }); }; export type { diff --git a/packages/remix/src/server/instrumentServer.ts b/packages/remix/src/server/instrumentServer.ts index 1486080039ca..4f67f2ae8b3d 100644 --- a/packages/remix/src/server/instrumentServer.ts +++ b/packages/remix/src/server/instrumentServer.ts @@ -246,12 +246,9 @@ function makeWrappedRootLoader() { }; } -function wrapRequestHandler( +function wrapRequestHandler ServerBuild | Promise)>( origRequestHandler: RequestHandler, - build: - | ServerBuild - | { build: ServerBuild } - | (() => ServerBuild | { build: ServerBuild } | Promise), + build: T, options?: { instrumentTracing?: boolean; }, @@ -278,7 +275,7 @@ function wrapRequestHandler( // check if the build is nested under `build` key if ('build' in resolvedBuild) { - resolvedRoutes = createRoutes(resolvedBuild.build.routes); + resolvedRoutes = createRoutes((resolvedBuild.build as ServerBuild).routes); } else { resolvedRoutes = createRoutes(resolvedBuild.routes); } @@ -407,12 +404,12 @@ function instrumentBuildCallback( /** * Instruments `remix` ServerBuild for performance tracing and error tracking. */ -export function instrumentBuild( - build: ServerBuild | (() => ServerBuild | Promise), +export function instrumentBuild ServerBuild | Promise)>( + build: T, options?: { instrumentTracing?: boolean; }, -): ServerBuild | (() => ServerBuild | Promise) { +): T { if (typeof build === 'function') { return function () { const resolvedBuild = build(); @@ -424,9 +421,9 @@ export function instrumentBuild( } else { return instrumentBuildCallback(resolvedBuild, options); } - }; + } as T; } else { - return instrumentBuildCallback(build, options); + return instrumentBuildCallback(build, options) as T; } }