Skip to content

Commit

Permalink
Fix asset watching in x-dev-env (#6908)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Sunil Pai <[email protected]>
  • Loading branch information
penalosa and threepointone authored Oct 7, 2024
1 parent 5c50949 commit d696850
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/big-waves-hammer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

fix: debounce restarting worker on assets dir file changes when `--x-dev-env` is enabled.
47 changes: 47 additions & 0 deletions packages/wrangler/e2e/dev.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1452,6 +1452,53 @@ describe("watch mode", () => {
response = await fetch(`${url}/hey`);
expect(response.status).toBe(404);
});

it("debounces runtime restarts when assets are modified", async () => {
const helper = new WranglerE2ETestHelper();
await helper.seed({
"wrangler.toml": dedent`
name = "${workerName}"
compatibility_date = "2023-01-01"
main = "src/index.ts"
[assets]
directory = "./public"
`,
"src/index.ts": dedent`
export default {
async fetch(request) {
return new Response("Hello, World!")
}
}
`,
"public/index.html": "Hello from Assets",
});
const worker = helper.runLongLived("wrangler dev");

const { url } = await worker.waitForReady();

// Modify assets multiple times in quick succession

await helper.seed({
"public/a.html": "a",
});

await helper.seed({
"public/b.html": "b",
});

await helper.seed({
"public/c.html": "c",
});

await worker.waitForReload();

// The three changes should be debounced, so only one reload should occur
await expect(worker.waitForReload(5_000)).rejects.toThrowError();

// now check assets are still fetchable
await expect(fetchText(url)).resolves.toBe("Hello from Assets");
});
});

describe.each([
Expand Down
5 changes: 3 additions & 2 deletions packages/wrangler/e2e/helpers/wrangler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ export class WranglerLongLivedCommand extends LongLivedCommand {
return match.groups as { url: string };
}

async waitForReload(): Promise<void> {
async waitForReload(readTimeout?: number): Promise<void> {
await this.readUntil(
/Detected changes, restarted server|Reloading local server\.\.\./
/Detected changes, restarted server|Reloading local server\.\.\./,
readTimeout
);
}
}
Expand Down
11 changes: 8 additions & 3 deletions packages/wrangler/src/api/startDevWorker/BundlerController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { getAssetChangeMessage } from "../../dev";
import { runBuild } from "../../dev/use-esbuild";
import { logger } from "../../logger";
import { isNavigatorDefined } from "../../navigator-user-agent";
import { debounce } from "../../pages/utils";
import { getWranglerTmpDir } from "../../paths";
import { Controller } from "./BaseController";
import { castErrorCause } from "./events";
Expand Down Expand Up @@ -264,16 +265,20 @@ export class BundlerController extends Controller<BundlerControllerEventMap> {
async #ensureWatchingAssets(config: StartDevWorkerOptions) {
await this.#assetsWatcher?.close();

const debouncedRefreshBundle = debounce(() => {
if (this.#currentBundle) {
this.emitBundleCompleteEvent(config, this.#currentBundle);
}
});

if (config.assets?.directory) {
this.#assetsWatcher = watch(config.assets.directory, {
persistent: true,
ignoreInitial: true,
}).on("all", async (eventName, filePath) => {
const message = getAssetChangeMessage(eventName, filePath);
logger.debug(`🌀 ${message}...`);
if (this.#currentBundle) {
this.emitBundleCompleteEvent(config, this.#currentBundle);
}
debouncedRefreshBundle();
});
}
}
Expand Down

0 comments on commit d696850

Please sign in to comment.