Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(templates/express): dynamically import chokidar so it doesn't crash production #7078

Merged
merged 1 commit into from
Sep 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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