Skip to content

Commit 393c7fe

Browse files
committed
refactor the dev reload experience
1 parent 05aa286 commit 393c7fe

19 files changed

Lines changed: 883 additions & 513 deletions

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# master
22

3+
- Fix dev reload experience of the Vite dev setup.
34
- Expose `RequestController` and `Handlers` as record-of-functions APIs so user code can migrate from `requestController->RequestController.setStatus(404)` to `requestController.setStatus(404)` and from `handler->ResX.Handlers.handleRequest({...})` to `handler.handleRequest({...})`; the old free-function surface is still available but deprecated.
45
- BREAKING: Remove `ResX.BunUtils.serveStaticFile`; static assets now go through generated `ResXAssets.staticAssetRoutes`.
56
- Add `__rawProps?: Dict.t<JSON.t>` on JSX DOM props as a low-level escape hatch for arbitrary attributes (escaped values, invalid/non-serializable entries ignored, may emit duplicates after typed props), with accompanying tests and README docs.

README.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ Let's set everything up. Start by setting up `vite.config.js`:
8080

8181
```javascript
8282
import { defineConfig } from "vite";
83-
import { resXVitePlugin } from "rescript-x";
83+
import resXVitePlugin from "rescript-x/res-x-vite-plugin.mjs";
8484

8585
export default defineConfig({
8686
plugins: [
@@ -123,11 +123,11 @@ There! If you want, you can also set up a bunch of scripts in `package.json` tha
123123
{
124124
"scripts": {
125125
"start": "NODE_ENV=production bun run src/App.js",
126-
"build": "NODE_ENV=production && bun run build:vite && bun run build:res",
126+
"build": "NODE_ENV=production bun run build:vite && bun run build:res",
127127
"build:vite": "vite build",
128128
"build:res": "rescript",
129129
"clean:res": "rescript clean",
130-
"dev:res": "rescript build -w",
130+
"dev:res": "rescript watch",
131131
"dev:server": "bun --watch run src/App.js",
132132
"dev:vite": "vite",
133133
"dev": "concurrently 'bun:dev:*'"
@@ -207,15 +207,17 @@ let portString = server->Bun.Server.port->Int.toString
207207
208208
Console.log(`Listening! on localhost:${portString}`)
209209
210-
// Run the dev server, responsible for hot module reloading etc, when in dev mode.
210+
// Run the small dev socket server used to trigger page refreshes after backend restarts.
211211
if ResX.BunUtils.isDev {
212212
ResX.BunUtils.runDevServer(~port)
213213
}
214214
```
215215

216216
Note that there's plenty of more things you can configure here, but for the sake of keeping it simple we'll just go with the basics.
217217

218-
You can now start up the dev environment: `bun run dev`. Open up `localhost:9000` and you should see your "Start page!" string.
218+
You can now start up the dev environment: `bun run dev`. Open the Vite URL, for example `http://localhost:9000`, and you should see your "Start page!" string.
219+
220+
In dev, browse the app through the Vite server, not the raw Bun app server port. ResX serves dev assets with root-relative URLs from the Vite origin and performs a full page refresh after the backend restarts and reconnects.
219221

220222
There's a ton more to ResX of course, but this should get you started.
221223

@@ -373,7 +375,7 @@ Then, include it in your ReScript:
373375
</head>
374376
```
375377

376-
There! It's now available to you, and Vite will both transform and hot module reload the asset if it's possible.
378+
There! It's now available to you, and Vite will transform it for both dev and production builds. In dev, assets are served from the Vite origin using root-relative URLs.
377379

378380
#### Thinking about client side JavaScript
379381

@@ -1218,7 +1220,8 @@ render: async ({path}) => {
12181220
ResX comes with its own Vite plugin that takes care of all configuration for you. It will:
12191221

12201222
- Ensure all ResX assets are handled and included properly
1221-
- Ensure that Hot Module Reloading works for all assets and that Vite dev mode is properly wired up to your local ResX dev server
1223+
- Proxy your app server behind the Vite dev origin
1224+
- Expose a same-origin dev socket so backend restarts trigger a hard page refresh once the app is ready again
12221225

12231226
> Note: Right now, using ResX with more elaborate Vite config than what's preconfigured for you might be problematic. This will change in the future though so that ResX is just another part of your Vite config. Open issues please when you find use cases you'd like supported but that doesn't work now.
12241227

bun.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demo/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ cd demo
1919
bun run dev
2020
```
2121

22+
Open the Vite URL that it prints, typically `http://localhost:9000`. In dev, the browser should use the Vite server origin rather than the raw Bun app port.
23+
2224
For a production-style local run:
2325

2426
```sh

demo/bun.lock

Lines changed: 38 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)