Skip to content

Commit 511be3d

Browse files
authored
Add log message when 'sec-fetch-mode: navigate' is responsible for assets routing decision in 'wrangler dev' (#8887)
1 parent 085a565 commit 511be3d

File tree

9 files changed

+153
-44
lines changed

9 files changed

+153
-44
lines changed

.changeset/cute-carpets-sneeze.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@cloudflare/workers-shared": minor
3+
"miniflare": minor
4+
---
5+
6+
Add log message when `Sec-Fetch-Mode: navigate` is responsible for assets routing decision in `wrangler dev`

fixtures/workers-with-assets-spa/tests/index.test.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
import { resolve } from "node:path";
2+
import { setTimeout } from "timers/promises";
23
import { toMatchImageSnapshot } from "jest-image-snapshot";
34
import { Browser, chromium } from "playwright-chromium";
45
import { afterAll, beforeAll, describe, it } from "vitest";
56
import { runWranglerDev } from "../../shared/src/run-wrangler-long-lived";
67

78
describe("Workers + Assets + SPA", () => {
8-
let ip: string, port: number, stop: (() => Promise<unknown>) | undefined;
9+
let ip: string,
10+
port: number,
11+
stop: (() => Promise<unknown>) | undefined,
12+
getOutput: () => string;
913
let browser: Browser | undefined;
1014

1115
beforeAll(async () => {
12-
({ ip, port, stop } = await runWranglerDev(resolve(__dirname, ".."), [
13-
"--port=0",
14-
"--inspector-port=0",
15-
]));
16+
({ ip, port, stop, getOutput } = await runWranglerDev(
17+
resolve(__dirname, ".."),
18+
["--port=0", "--inspector-port=0"]
19+
));
1620

1721
browser = await chromium.launch({
1822
headless: !process.env.VITE_DEBUG_SERVE,
@@ -176,6 +180,12 @@ describe("Workers + Assets + SPA", () => {
176180
const blogTitleLocator = page.getByRole("heading", { name: "Blog" });
177181
await blogTitleLocator.waitFor({ state: "attached" });
178182

183+
expect(
184+
getOutput().match(
185+
/GET \/blog 200 OK \(.*\) `Sec-Fetch-Mode: navigate` header present - using `not_found_handling` behavior/
186+
)
187+
).toBeTruthy();
188+
179189
const blogSlugInput = page.getByRole("textbox");
180190
blogSlugInput.fill("/blog/some-slug-here");
181191

packages/miniflare/src/plugins/assets/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ export const ASSETS_PLUGIN: Plugin<typeof AssetsOptionsSchema> = {
141141
...options.assets.assetConfig,
142142
redirects: parsedRedirects,
143143
headers: parsedHeaders,
144+
debug: true,
144145
};
145146

146147
const id = options.assets.workerName;

packages/miniflare/src/workers/core/entry.worker.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,14 +300,22 @@ function colourFromHTTPStatus(status: number): Colorize {
300300
return blue;
301301
}
302302

303+
const ADDITIONAL_RESPONSE_LOG_HEADER_NAME = "X-Mf-Additional-Response-Log";
304+
303305
function maybeLogRequest(
304306
req: Request,
305307
res: Response,
306308
env: Env,
307309
ctx: ExecutionContext,
308310
startTime: number
309-
) {
310-
if (env[CoreBindings.JSON_LOG_LEVEL] < LogLevel.INFO) return;
311+
): Response {
312+
res = new Response(res.body, res); // Ensure mutable headers
313+
const additionalResponseLog = res.headers.get(
314+
ADDITIONAL_RESPONSE_LOG_HEADER_NAME
315+
);
316+
res.headers.delete(ADDITIONAL_RESPONSE_LOG_HEADER_NAME);
317+
318+
if (env[CoreBindings.JSON_LOG_LEVEL] < LogLevel.INFO) return res;
311319

312320
const url = new URL(req.url);
313321
const statusText = (res.statusText.trim() || STATUS_CODES[res.status]) ?? "";
@@ -316,6 +324,9 @@ function maybeLogRequest(
316324
colourFromHTTPStatus(res.status)(`${bold(res.status)} ${statusText} `),
317325
grey(`(${Date.now() - startTime}ms)`),
318326
];
327+
if (additionalResponseLog) {
328+
lines.push(` ${grey(additionalResponseLog)}`);
329+
}
319330
const message = reset(lines.join(""));
320331

321332
ctx.waitUntil(
@@ -325,6 +336,8 @@ function maybeLogRequest(
325336
body: message,
326337
})
327338
);
339+
340+
return res;
328341
}
329342

330343
function handleProxy(request: Request, env: Env) {
@@ -423,7 +436,7 @@ export default <ExportedHandler<Env>>{
423436
response = maybeInjectLiveReload(response, env, ctx);
424437
response = ensureAcceptableEncoding(clientAcceptEncoding, response);
425438
if (env[CoreBindings.LOG_REQUESTS]) {
426-
maybeLogRequest(request, response, env, ctx, startTime);
439+
response = maybeLogRequest(request, response, env, ctx, startTime);
427440
}
428441
return response;
429442
} catch (e: any) {

packages/workers-shared/asset-worker/src/configuration.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ export const normalizeConfiguration = (
2222
},
2323
account_id: configuration?.account_id ?? -1,
2424
script_id: configuration?.script_id ?? -1,
25+
debug: configuration?.debug ?? false,
2526
};
2627
};

0 commit comments

Comments
 (0)