Skip to content

Commit

Permalink
fix: dynamically import chokidar so it doesn't crash production (#221)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelDeBoey authored Oct 30, 2023
1 parent 3d1a723 commit 0e0cf21
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import prom from "@isaacs/express-prometheus-middleware";
import { createRequestHandler } from "@remix-run/express";
import type { ServerBuild } from "@remix-run/node";
import { broadcastDevReady, installGlobals } from "@remix-run/node";
import chokidar from "chokidar";
import compression from "compression";
import type { RequestHandler } from "express";
import express from "express";
Expand Down Expand Up @@ -91,15 +90,17 @@ async function run() {

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

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

return handler(...args);
});

const port = process.env.PORT || 3000;
app.listen(port, () => {
Expand Down Expand Up @@ -133,14 +134,17 @@ async function run() {
return import(BUILD_URL + "?t=" + stat.mtimeMs);
}

function createDevRequestHandler(initialBuild: ServerBuild): RequestHandler {
async function createDevRequestHandler(
initialBuild: ServerBuild,
): Promise<RequestHandler> {
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 0e0cf21

Please sign in to comment.