Skip to content

Commit

Permalink
(feature): disable autoreload
Browse files Browse the repository at this point in the history
  • Loading branch information
aabccd021 committed Dec 25, 2024
1 parent 78b2390 commit 025d98d
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 5 deletions.
4 changes: 4 additions & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
# https://github.com/nix-community/nix-direnv
if ! has nix_direnv_version || ! nix_direnv_version 3.0.6; then
source_url "https://raw.githubusercontent.com/nix-community/nix-direnv/3.0.6/direnvrc" "sha256-RYcUJaRMf8oF5LznDrlCXbkOQrywm0HDv1VjYGaJGdM="
fi
use flake
2 changes: 2 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ jobs:

- uses: HatsuneMiku3939/direnv-action@v1

- run: nix flake check

- run: bun install

- run: tsc
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ jobs:

- uses: HatsuneMiku3939/direnv-action@v1

- run: nix flake check

- run: bun install

- run: tsc
Expand Down
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ Bun.serve({

## Options

### `eventPath` and `scriptPath`

You can specify URL paths used for server-sent events and live reloader script.

```ts
Expand All @@ -46,14 +44,20 @@ Bun.serve({
// Live reload script path
// default: "/__dev__/reload.js"
scriptPath: "/__reload.js",

// Wether to enable auto reload.
// If false, you need to manually call `reloadClients` function to reload clients.
// default: true
autoReload: false
},
),
});
```

## Manually reload clients

You can manually reload clients (refresh tabs) by calling `reloadClients` function.
You can manually reload clients (refresh tabs) by calling `reloadClients` function,
in addition to auto reload feature.

```ts
import { withHtmlLiveReload, reloadClients } from "bun-html-live-reload";
Expand Down
10 changes: 9 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
declare global {
var clients: Set<ReadableStreamDefaultController> | undefined;
var autoReload: boolean | undefined;
}

globalThis.clients ??= new Set<ReadableStreamDefaultController>();
globalThis.autoReload = globalThis.autoReload ?? true;

export function reloadClients(): void {
if (globalThis.clients !== undefined) {
Expand All @@ -12,7 +14,9 @@ export function reloadClients(): void {
}
}

reloadClients();
if (globalThis.autoReload) {
reloadClients();
}

export type LiveReloadOptions = {
/**
Expand Down Expand Up @@ -43,8 +47,12 @@ export function withHtmlLiveReload(
options?: {
eventPath?: string;
scriptPath?: string;
autoReload?: false;
},
): Fetch {
if (options?.autoReload === false) {
globalThis.autoReload = false;
}
return async (req): Promise<Response> => {
if (req.method !== "GET") {
return handler(req);
Expand Down
52 changes: 52 additions & 0 deletions no-autoreload.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { afterEach, expect, test } from "bun:test";
import { copyFileSync, mkdtempSync } from "node:fs";
import { chromium } from "playwright";

const serverCodeInit = `
import { withHtmlLiveReload } from "./bun-html-live-reload.ts";
Bun.serve({
fetch: withHtmlLiveReload(async () => {
return new Response("<div>Init</div>", {
headers: { "Content-Type": "text/html" },
}, { autoReload: false });
}),
});
`;

let close: () => Promise<void> | undefined;

afterEach(async () => {
await close?.();
});

test("can disable auto reload", async () => {
const systemTmp = process.env["TMPDIR"] ?? "/tmp";
const tmpdir = mkdtempSync(`${systemTmp}/bun-`);
const serverPath = `${tmpdir}/server.ts`;
const libPath = `${tmpdir}/bun-html-live-reload.ts`;

await Bun.write(serverPath, serverCodeInit);

copyFileSync(`${import.meta.dir}/index.ts`, libPath);

const child = Bun.spawn(["bun", "--hot", serverPath], { stderr: "ignore" });

const browser = await chromium.launch();

close = async (): Promise<void> => {
child?.kill();
await browser.close();
};

const context = await browser.newContext();
const page = await context.newPage();
await page.goto("http://localhost:3000");

expect(await page.innerText("div")).toBe("Init");

await Bun.write(serverPath, serverCodeInit);
await page.waitForEvent("framenavigated");

expect(await page.innerText("div")).toBe("Init");
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "bun-html-live-reload",
"description": "HTML live reload for Bun",
"version": "1.0.1",
"version": "1.0.2",
"module": "index.ts",
"type": "module",
"author": "aabccd021",
Expand Down

0 comments on commit 025d98d

Please sign in to comment.