-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(v2_dev): instructions for using and migrating to
v2_dev
- Loading branch information
Showing
4 changed files
with
353 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
--- | ||
title: "@remix-run/{adapter}" | ||
order: 2 | ||
order: 3 | ||
--- | ||
|
||
# Server Adapters | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,260 @@ | ||
--- | ||
title: "@remix-run/dev CLI (v2)" | ||
order: 2 | ||
new: true | ||
--- | ||
|
||
The Remix CLI comes from the `@remix-run/dev` package. It also includes the compiler. Make sure it is in your `package.json` `devDependencies` so it doesn't get deployed to your server. | ||
|
||
To get a full list of available commands and flags, run: | ||
|
||
```sh | ||
npx @remix-run/dev -h | ||
``` | ||
|
||
## `remix build` | ||
|
||
Builds your app for production. This command will set `process.env.NODE_ENV` to `production` and minify the output for deployment. | ||
|
||
```sh | ||
remix build | ||
``` | ||
|
||
### Options | ||
|
||
| Option | flag | config | default | | ||
| ---------------------------------------- | ------------- | ------ | ------- | | ||
| Generate sourcemaps for production build | `--sourcemap` | N/A | `false` | | ||
|
||
## `remix dev` | ||
|
||
Builds your app and spins up the Remix dev server alongside your app server. | ||
|
||
The dev server will: | ||
|
||
1. Set `NODE_ENV` to `development` | ||
2. Watch your app code for changes and trigger rebuilds | ||
3. Restart your app server whenever rebuilds succeed | ||
4. Send code updates to the browser via Live Reload and HMR + Hot Data Revalidation | ||
|
||
### With `remix-serve` | ||
|
||
Enable the v2 dev server: | ||
|
||
```js filename=remix.config.js | ||
module.exports = { | ||
future: { | ||
v2_dev: true, | ||
}, | ||
}; | ||
``` | ||
|
||
That's it! | ||
|
||
### With custom app server | ||
|
||
If you used a template to get started, hopefully it has integration with the v2 dev server out-of-the-box. | ||
If not, you can follow these steps to integrate your project with `v2_dev`: | ||
|
||
1. Enable the v2 dev server: | ||
|
||
```js filename=remix.config.js | ||
module.exports = { | ||
future: { | ||
v2_dev: true, | ||
}, | ||
}; | ||
``` | ||
|
||
2. Replace your dev scripts in `package.json` and use `-c` to specify your app server command: | ||
|
||
```json | ||
{ | ||
"dev": "remix dev -c 'node ./server.js'" | ||
} | ||
``` | ||
|
||
3. Ensure `broadcastDevReady` is called when your app server is up and running: | ||
|
||
```js filename=server.js lines=[12,25-27] | ||
import path from "node:path"; | ||
|
||
import { broadcastDevReady } from "@remix-run/node"; | ||
import express from "express"; | ||
|
||
const BUILD_DIR = path.resolve(__dirname, "build"); | ||
const build = require(BUILD_DIR); | ||
|
||
const app = express(); | ||
|
||
// ... code for setting up your express app goes here ... | ||
|
||
app.all( | ||
"*", | ||
createRequestHandler({ | ||
build, | ||
mode: process.env.NODE_ENV, | ||
}) | ||
); | ||
|
||
const port = 3000; | ||
app.listen(port, () => { | ||
console.log(`👉 http://localhost:${port}`); | ||
|
||
if (process.env.NODE_ENV === "development") { | ||
broadcastDevReady(build); | ||
} | ||
}); | ||
``` | ||
|
||
<docs-info> | ||
|
||
For CloudFlare, use `logDevReady` instead of `broadcastDevReady`. | ||
|
||
Why? `broadcastDevReady` uses `fetch` to send a ready message to the dev server, | ||
but CloudFlare does not support async I/O like `fetch` outside of request handling. | ||
|
||
</docs-info> | ||
|
||
### Options | ||
|
||
Options priority order is: 1. flags, 2. config, 3. defaults. | ||
|
||
| Option | flag | config | default | | ||
| --------------- | ------------------ | ---------------- | ------------------------------------------------- | | ||
| Command | `-c` / `--command` | `command` | `remix-serve <server build path>` | | ||
| No restart | `--no-restart` | `restart: false` | `restart: true` | | ||
| Scheme | `--scheme` | `scheme` | `https` if TLS key/cert are set, otherwise `http` | | ||
| Host | `--host` | `host` | `localhost` | | ||
| Port | `--port` | `port` | Dynamically chosen open port | | ||
| TLS key | `--tls-key` | `tlsKey` | N/A | | ||
| TLS certificate | `--tls-cert` | `tlsCert` | N/A | | ||
|
||
<docs-info> | ||
|
||
The scheme/host/port options only affect the Remix dev server, and **do not affect your app server**. | ||
Your app will run on your app server's normal URL. | ||
|
||
You most likely won't want to configure the scheme/host/port for the dev server, | ||
as those are implementation details used internally for hot updates. | ||
They exist in case you need fine-grain control, for example Docker networking or using specific open ports. | ||
|
||
</docs-info> | ||
|
||
For example, to override the port used by the dev server via config: | ||
|
||
```js filename=remix.config.js | ||
module.exports = { | ||
future: { | ||
v2_dev: { | ||
port: 8001, | ||
}, | ||
}, | ||
}; | ||
``` | ||
|
||
### Keep app server running across rebuilds | ||
|
||
By default, the Remix dev server restarts your app server when rebuilds occur. | ||
This is a simple way to ensure that your app server is up-to-date with the latest code changes. | ||
|
||
If you'd like to opt-out of this behavior use the `--no-restart` flag: | ||
|
||
```sh | ||
remix dev --no-restart -c 'node ./server.js' | ||
``` | ||
|
||
🚨 BUT that means you are now on the hook for applying changes to your running app server _and_ telling the dev server when those changes have been applied. | ||
|
||
> With great power comes great responsibility. | ||
Check out our [templates][templates] for examples on how to use `import` cache busting to apply code changes to your app server while it keeps running. | ||
|
||
If you're using CJS but looking at an ESM template, you'll need to swap out `import` cache busting with `require` cache busting: | ||
|
||
```diff | ||
- const stat = fs.statSync(BUILD_DIR); | ||
- build = import(BUILD_DIR + "?t=" + stat.mtimeMs); | ||
+ for (const key in require.cache) { | ||
+ if (key.startsWith(BUILD_DIR)) { | ||
+ delete require.cache[key]; | ||
+ } | ||
+ } | ||
+ build = require(BUILD_DIR) | ||
``` | ||
|
||
#### Pick up changes from other packages | ||
|
||
If you are using a monorepo, you might want Remix to perform hot updates not only when your app code changes, but whenever you change code in any of your apps dependencies. | ||
|
||
For example, you could have a UI library package (`packages/ui`) that is used within your Remix app (`packages/app`). | ||
To pick up changes in `packages/ui`, you can configure [watchPaths][watch-paths] to include your packages. | ||
|
||
#### Keep in-memory data and connections across rebuilds | ||
|
||
Every time you re-import code to apply changes to your app server, that code will be run. | ||
Rerunning each changed module works great in most cases, but sometimes you want to want to keep stuff around. | ||
|
||
For example, it'd be nice if your app only connected to your database once and kept that connection around across rebuilds. | ||
But since the connection is held in-memory, re-imports will wipe those out and cause your app to reconnect. | ||
|
||
Luckily, there's a trick to get around this: use `global` as a cache for keeping things in-memory across rebuilds! | ||
Here's a nifty utility adapted from [Jon Jensen's code][jenseng-code] for [his Remix Conf 2023 talk][jenseng-talk]: | ||
|
||
```ts filename=app/utils/remember.ts | ||
export function remember<T>(key: string, value: T) { | ||
const g = global as any; | ||
g.__singletons ??= {}; | ||
g.__singletons[key] ??= value; | ||
return g.__singletons[key]; | ||
} | ||
``` | ||
|
||
And here's how to use it to keep stuff around across rebuilds: | ||
|
||
```ts filename=app/utils/db.server.ts | ||
import { PrismaClient } from "@prisma/client"; | ||
|
||
import { remember } from "~/utils/remember"; | ||
|
||
// hard-code a unique key so we can look up the client when this module gets re-imported | ||
export const db = remember("db", new PrismaClient()); | ||
``` | ||
|
||
### How to set up local HTTPS | ||
|
||
For this example, let's use [mkcert][mkcert]. | ||
After you have it installed, make sure to: | ||
|
||
- Create a local Certificate Authority if you haven't already done so | ||
- Use `NODE_EXTRA_CA_CERTS` for Node compatibility | ||
|
||
```sh | ||
mkcert -install # create a local CA | ||
export NODE_EXTRA_CA_CERTS="$(mkcert -CAROOT)/rootCA.pem" # tell Node to use our local CA | ||
``` | ||
|
||
Now, create the TLS key and certificate: | ||
|
||
```sh | ||
mkcert -key-file key.pem -cert-file cert.pem localhost | ||
``` | ||
|
||
👆 You can change `localhost` to something else if you are using custom hostnames. | ||
|
||
Next, use the `key.pem` and `cert.pem` to get HTTPS working locally with your app server. | ||
This depends on what you are using for your app server. | ||
For example, here's how you could use HTTPS with an Express server: | ||
|
||
```ts filename=server.js | ||
import fs from "node:fs"; | ||
import https from "node:https"; | ||
|
||
import express from "express"; | ||
|
||
const app = express(); | ||
|
||
// ... code setting up your express app goes here ... | ||
|
||
// | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters