Replies: 1 comment
-
@jacob-ebey and I chatted about this and he suggested exposing a Middleware API for the dev server as well. Something like: import express from "express"
import remixDevServerMiddleware from "@remix-run/dev"
let app = express()
app.use(remixDevServerMiddlware({
config,
liveReload, // boolean
loadContext: {/* user defined */}
}) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Relates to #4838
How things are today: "CLI as an API"
Today, users can only interface with the compiler and dev server via CLI commands:
remix build
: run the compiler, usually withNODE_ENV="production"
for prod buildsremix watch
: run the compiler in watch mode AND run a Websocket server to broadcast change notifications to the browser for live reloadremix dev
: does whatremix watch
does plus also runs a dev server to handle requestsExternal dev servers (i.e.
server.ts
instead of runningremix-serve build
1) often run two processes in dev mode2:The CLI (
remix watch
) is currently the only way to run (1) compiler watch mode and (2) Websocket server broadcasting from@remix-run/dev
for an external server.Issues with "CLI as an API"
The main limitation of this approach is that the live reload loop (from
remix watch
) runs in a separate process from the actual dev server inserver.{js,ts}
.Remix users are painfully trying to work around this limitations in a couple places, notably:
Unfortunately, this makes race conditions possible between (1) the Websocket server managed by
remix watch
sending out a broadcast for a change and (2) whatever mechanism is used to detect that a Remix (re)build happened inserver.ts
.3How does
remix dev
do it correctly?We don't run into this issue with the official Remix Dev Server (i.e.
remix dev
).Why?
Because
remix dev
does not run the live reload loop in another process.Instead it directly calls the
compiler.watch
function, so that it can use callbacks to be directly notified by the live reload loop when a (re)build occurs.Proposal
To fix these issues, let's expose a Node API for the compiler and dev server.
That way, external servers can call
compiler.watch
ordevServer.liveReload
withinserver.ts
to coordinate with the live reload loop, the same wayremix dev
does it.Compiler API
Good news!
The
compiler/
directory inremix-dev/
already exposes this exact API! ✅Now we just need to expose that in
remix-dev/index.ts
:Dev server API
Ideally, (1) the Websocket server code from the
watch
command and (2) the express dev server from thedev
command should be moved to adevServer/
directory withinremix-dev/
that exposes the proposed API.Then the CLI can access these via:
I have an example implementation of this interface in the BYOC prototypes.
But its really just moving a bunch of dev server logic out of the CLI
commands.ts
file into a better home.Update: PR #4528 implements
liveReload
andserve
, so these would just need to be exported fromdevServer/
inremix-dev/index.ts
CLI
The CLI commands could be tightened to just be
build
anddev
.Note that the current
remix watch
command is confusing because it is not the analog tocompiler.watch
, but rather wrapscompiler.watch
with a Websocket server for broadcasting changes to the browser.Footnotes
remix-serve build
seems misleading asbuild
command name implies that this command compiles the app, but in reality it merely serves the existing built app. I think a better name would benpx remix-server start
or maybenpx remix serve
(if we can call out to@remix-run/serve
from within@remix-run/dev
). ↩Example taken from the
express
template ↩You can see the author of Purge cache on file changes on dev servers #4453 try to hack around the race condition by introducing
devServerBroadcastDelay: 100
↩Beta Was this translation helpful? Give feedback.
All reactions