From 7cd6164290b488187344f1b1632b76e5f916a3ac Mon Sep 17 00:00:00 2001 From: Pedro Cattori Date: Wed, 26 Jul 2023 09:53:39 -0400 Subject: [PATCH] feat(dev,server-runtime): detect built mode via `build.mode` --- .changeset/curly-moons-cover.md | 9 +++++++++ .../remix-dev/__tests__/fixtures/cloudflare/server.ts | 2 +- packages/remix-dev/__tests__/fixtures/deno/server.ts | 2 +- packages/remix-dev/compiler/server/plugins/entry.ts | 1 + packages/remix-dev/server-build.ts | 1 + packages/remix-server-runtime/build.ts | 1 + 6 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 .changeset/curly-moons-cover.md diff --git a/.changeset/curly-moons-cover.md b/.changeset/curly-moons-cover.md new file mode 100644 index 00000000000..48ac6717a4f --- /dev/null +++ b/.changeset/curly-moons-cover.md @@ -0,0 +1,9 @@ +--- +"@remix-run/dev": minor +"@remix-run/server-runtime": minor +--- + +detect built mode via `build.mode` + +Prevents mode mismatch between built Remix server entry and user-land server. +Additionally, all runtimes (including non-Node runtimes) can use `build.mode` to determine if HMR should be performed. diff --git a/packages/remix-dev/__tests__/fixtures/cloudflare/server.ts b/packages/remix-dev/__tests__/fixtures/cloudflare/server.ts index b1681c47979..b2006d7930a 100644 --- a/packages/remix-dev/__tests__/fixtures/cloudflare/server.ts +++ b/packages/remix-dev/__tests__/fixtures/cloudflare/server.ts @@ -4,5 +4,5 @@ import * as build from "@remix-run/dev/server-build"; export const onRequest = createPagesFunctionHandler({ build, getLoadContext: (context) => context.env, - mode: process.env.NODE_ENV, + mode: build.mode, }); diff --git a/packages/remix-dev/__tests__/fixtures/deno/server.ts b/packages/remix-dev/__tests__/fixtures/deno/server.ts index c1bb125f19d..86a2642804e 100644 --- a/packages/remix-dev/__tests__/fixtures/deno/server.ts +++ b/packages/remix-dev/__tests__/fixtures/deno/server.ts @@ -5,7 +5,7 @@ import * as build from "@remix-run/dev/server-build"; const remixHandler = createRequestHandlerWithStaticFiles({ build, - mode: Deno.env.get("NODE_ENV"), + mode: build.mode, getLoadContext: () => ({}), }); diff --git a/packages/remix-dev/compiler/server/plugins/entry.ts b/packages/remix-dev/compiler/server/plugins/entry.ts index edb4fb60b08..a7c9634b94b 100644 --- a/packages/remix-dev/compiler/server/plugins/entry.ts +++ b/packages/remix-dev/compiler/server/plugins/entry.ts @@ -41,6 +41,7 @@ ${Object.keys(config.routes) )};`; }) .join("\n")} + export const mode = ${JSON.stringify(options.mode)}; export { default as assets } from ${JSON.stringify( assetsManifestVirtualModule.id )}; diff --git a/packages/remix-dev/server-build.ts b/packages/remix-dev/server-build.ts index 028c75c8024..7d3af52941d 100644 --- a/packages/remix-dev/server-build.ts +++ b/packages/remix-dev/server-build.ts @@ -6,6 +6,7 @@ throw new Error( " by the Remix compiler at build time." ); +export const mode: ServerBuild["mode"] = undefined!; export const assets: ServerBuild["assets"] = undefined!; export const entry: ServerBuild["entry"] = undefined!; export const routes: ServerBuild["routes"] = undefined!; diff --git a/packages/remix-server-runtime/build.ts b/packages/remix-server-runtime/build.ts index 00c5e82980a..59d6f2a8bd0 100644 --- a/packages/remix-server-runtime/build.ts +++ b/packages/remix-server-runtime/build.ts @@ -7,6 +7,7 @@ import type { AppLoadContext } from "./data"; * The output of the compiler for the server build. */ export interface ServerBuild { + mode: string; entry: { module: ServerEntryModule; };