Skip to content

Commit

Permalink
fix(wrangler): Fix pages dev watch mode [_worker.js] (#6150)
Browse files Browse the repository at this point in the history
* fix(wrangler): Fix `pages dev` watch mode [_worker.js]

The watch mode in `pages dev` for Advanced Mode projects
is currently partially broken, as it only watches for
changes in the `_worker.js` file, but not for changes in
any of its imported dependencies. This means that given
the following `_worker.js` file

```
import { graham } from "./graham-the-dog";
export default {
    fetch(request, env) {
	return new Response(graham)
    }
}
```

`pages dev` will reload for any changes in the `_worker.js`
file itself, but not for any changes in `graham-the-dog.js`,
which is its dependency.

Similarly, `pages dev` will not reload for any changes in
non-JS module imports, such as wasm/html/binary module
imports.

This commit addresses all the aforementioned issues.

Fixes #4824

* fixes
  • Loading branch information
CarmenPopoviciu committed Jul 4, 2024
1 parent 5623aa2 commit d993409
Show file tree
Hide file tree
Showing 3 changed files with 314 additions and 145 deletions.
22 changes: 22 additions & 0 deletions .changeset/poor-ads-poke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
"wrangler": patch
---

fix: Fix `pages dev` watch mode [_worker.js]

The watch mode in `pages dev` for Advanced Mode projects is currently partially broken, as it only watches for changes in the "\_worker.js" file, but not for changes in any of its imported dependencies. This means that given the following "\_worker.js" file

```
import { graham } from "./graham-the-dog";
export default {
fetch(request, env) {
return new Response(graham)
}
}
```

`pages dev` will reload for any changes in the `_worker.js` file itself, but not for any changes in `graham-the-dog.js`, which is its dependency.

Similarly, `pages dev` will not reload for any changes in non-JS module imports, such as wasm/html/binary module imports.

This commit fixes all the aforementioned issues.
76 changes: 74 additions & 2 deletions packages/wrangler/e2e/pages-dev.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ describe("pages dev", () => {

await setTimeout(5_000);

await worker.readUntil(/Failed to bundle/);
await worker.readUntil(/Failed to build/);

// And then make sure Wrangler hasn't crashed
await helper.seed({
Expand Down Expand Up @@ -234,7 +234,7 @@ describe("pages dev", () => {

await setTimeout(5_000);

await worker.readUntil(/Unexpected error building Functions directory/);
await worker.readUntil(/Failed to build Functions/);

// And then make sure Wrangler hasn't crashed
await helper.seed({
Expand Down Expand Up @@ -645,6 +645,78 @@ describe("pages dev", () => {
expect(hello).toMatchInlineSnapshot('"Updated Worker!"');
});

it("should support modifying dependencies during dev session (_worker)", async () => {
const helper = new WranglerE2ETestHelper();

await helper.seed({
"pets/bear.js": dedent`
export const bear = "BEAR!"
`,
"_worker.js": dedent`
import { bear } from "./pets/bear"
export default {
fetch(request, env) {
return new Response(bear)
}
}`,
});

const port = await getPort();
const worker = helper.runLongLived(`wrangler pages dev --port ${port} .`);
const { url } = await worker.waitForReady();

let bear = await fetchText(url);
expect(bear).toMatchInlineSnapshot('"BEAR!"');

await helper.seed({
"pets/bear.js": dedent`
export const bear = "We love BEAR!"
`,
});

await worker.waitForReload();

bear = await fetchText(url);
expect(bear).toMatchInlineSnapshot('"We love BEAR!"');
});

it("should support modifying external modules during dev session (_worker)", async () => {
const helper = new WranglerE2ETestHelper();

await helper.seed({
"graham.html": dedent`
<h1>Graham the dog</h1>
`,
"_worker.js": dedent`
import html from "./graham.html"
export default {
fetch(request, env) {
return new Response(html)
}
}`,
});

const port = await getPort();
const worker = helper.runLongLived(`wrangler pages dev --port ${port} .`);
const { url } = await worker.waitForReady();

let graham = await fetchText(url);
expect(graham).toMatchInlineSnapshot('"<h1>Graham the dog</h1>"');

await helper.seed({
"graham.html": dedent`
<h1>Graham is the bestest doggo</h1>
`,
});

await worker.waitForReload();

graham = await fetchText(url);
expect(graham).toMatchInlineSnapshot(
'"<h1>Graham is the bestest doggo</h1>"'
);
});

it("should support modifying _routes.json during dev session", async () => {
const helper = new WranglerE2ETestHelper();

Expand Down
Loading

0 comments on commit d993409

Please sign in to comment.