Skip to content

Commit

Permalink
fix(templates/express): dynamically import chokidar so it doesn't c…
Browse files Browse the repository at this point in the history
…rash production (#7078)

Signed-off-by: Logan McAnsh <logan@mcan.sh>
  • Loading branch information
mcansh committed Sep 20, 2023
1 parent 3082512 commit 3faf9d0
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions templates/express/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import * as url from "node:url";

import { createRequestHandler } from "@remix-run/express";
import { broadcastDevReady, installGlobals } from "@remix-run/node";
import chokidar from "chokidar";
import compression from "compression";
import express from "express";
import morgan from "morgan";
Expand Down Expand Up @@ -38,15 +37,18 @@ app.use(express.static("public", { maxAge: "1h" }));

app.use(morgan("tiny"));

app.all(
"*",
process.env.NODE_ENV === "development"
? createDevRequestHandler(initialBuild)
: createRequestHandler({
build: initialBuild,
mode: initialBuild.mode,
})
);
app.all("*", async (...args) => {
if (process.env.NODE_ENV === "development") {
const handler = await createDevRequestHandler(initialBuild);
return handler(...args);
}

const handler = createRequestHandler({
build: initialBuild,
mode: initialBuild.mode,
});
return handler(...args);
});

const port = process.env.PORT || 3000;
app.listen(port, async () => {
Expand Down Expand Up @@ -74,14 +76,15 @@ async function reimportServer() {
* @param {ServerBuild} initialBuild
* @returns {import('@remix-run/express').RequestHandler}
*/
function createDevRequestHandler(initialBuild) {
async function createDevRequestHandler(initialBuild) {
let build = initialBuild;
async function handleServerUpdate() {
// 1. re-import the server build
build = await reimportServer();
// 2. tell Remix that this app server is now up-to-date and ready
broadcastDevReady(build);
}
const chokidar = await import("chokidar");
chokidar
.watch(VERSION_PATH, { ignoreInitial: true })
.on("add", handleServerUpdate)
Expand Down

0 comments on commit 3faf9d0

Please sign in to comment.