diff options
author | 2023-08-28 12:55:22 -0700 | |
---|---|---|
committer | 2023-08-28 12:55:22 -0700 | |
commit | 177e02b304d331b5a3142d5e52d22fb368c4f33e (patch) | |
tree | bfeb88e3a731444e76ecde8e371956074488b580 /docs | |
parent | 726f8aa3ef78956dfb59c27801c961236ed9d8e1 (diff) | |
download | bun-177e02b304d331b5a3142d5e52d22fb368c4f33e.tar.gz bun-177e02b304d331b5a3142d5e52d22fb368c4f33e.tar.zst bun-177e02b304d331b5a3142d5e52d22fb368c4f33e.zip |
docs: hot reloading with Bun.serve
Diffstat (limited to 'docs')
-rw-r--r-- | docs/api/http.md | 12 | ||||
-rw-r--r-- | docs/guides/http/hot.md | 39 |
2 files changed, 9 insertions, 42 deletions
diff --git a/docs/api/http.md b/docs/api/http.md index 1029972a6..ec32274d7 100644 --- a/docs/api/http.md +++ b/docs/api/http.md @@ -189,7 +189,7 @@ Bun.serve({ }); ``` -## Hot reloading +## Object syntax Thus far, the examples on this page have used the explicit `Bun.serve` API. Bun also supports an alternate syntax. @@ -203,15 +203,15 @@ export default { } satisfies Serve; ``` -Instead of passing the server options into `Bun.serve`, export it. This file can be executed as-is; when Bun runs a file with a `default` export containing a `fetch` handler, it passes it into `Bun.serve` under the hood. +Instead of passing the server options into `Bun.serve`, `export default` it. This file can be executed as-is; when Bun sees a file with a `default` export containing a `fetch` handler, it passes it into `Bun.serve` under the hood. -This syntax has one major advantage: it is hot-reloadable out of the box. When any source file is changed, Bun will reload the server with the updated code _without restarting the process_. This makes hot reloads nearly instantaneous. Use the `--hot` flag when starting the server to enable hot reloading. +<!-- This syntax has one major advantage: it is hot-reloadable out of the box. When any source file is changed, Bun will reload the server with the updated code _without restarting the process_. This makes hot reloads nearly instantaneous. Use the `--hot` flag when starting the server to enable hot reloading. --> -```bash +<!-- ```bash $ bun --hot server.ts -``` +``` --> -It's possible to configure hot reloading while using the explicit `Bun.serve` API; for details refer to [Runtime > Hot reloading](/docs/runtime/hot). +<!-- It's possible to configure hot reloading while using the explicit `Bun.serve` API; for details refer to [Runtime > Hot reloading](/docs/runtime/hot). --> ## Streaming files diff --git a/docs/guides/http/hot.md b/docs/guides/http/hot.md index c033e5be5..8bc208893 100644 --- a/docs/guides/http/hot.md +++ b/docs/guides/http/hot.md @@ -10,46 +10,13 @@ bun --hot run index.ts --- -To avoid re-running `Bun.serve()` during `--hot` reloads, you should assign the `Server` instance as a property of `globalThis`. The `globalThis` object survives hot reloads. +Bun detects when you are running an HTTP server with `Bun.serve()`. It reloads your fetch handler when source files change, _without_ restarting the `bun` process. This makes hot reloads nearly instantaneous. ```ts -import { type Serve, type Server } from "bun"; - -// make TypeScript happy -declare global { - var server: Server; -} - -// define server parameters -const serveOptions: Serve = { - port: 3000, - fetch(req) { - return new Response(`Hello world`); - }, -}; - -if (!globalThis.server) { - globalThis.server = Bun.serve(serveOptions); -} else { - globalThis.server.reload(serveOptions); -} -``` - ---- - -To avoid manually calling `server.reload()`, you can use start a server with Bun's [object syntax](/docs/runtime/hot#http-servers). If you `export default` a plain object with a `fetch` handler defined, then run this file with Bun, Bun will start an HTTP server as if you'd passed this object into `Bun.serve()`. - -With this approach, Bun automatically reloads the server when reloads happen. - -See [HTTP > Hot Reloading](<[/docs/api/http](https://bun.sh/docs/api/http#hot-reloading)>) for full docs. - -```ts -import { type Serve } from "bun"; - -export default { +Bun.serve({ port: 3000, fetch(req) { return new Response(`Hello world`); }, -} satisfies Serve; +}); ``` |