Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix asset watching in x-dev-env #6908

Merged
merged 3 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT, if we are using it here it is not really a "Pages util" anymore.

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
Loading