Skip to content

Commit 38a81cb

Browse files
committed
fix: Respect trailing slash config for _next/image route in worker
1 parent 3be30a4 commit 38a81cb

File tree

4 files changed

+14
-1
lines changed

4 files changed

+14
-1
lines changed

.changeset/evil-rabbits-relax.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@opennextjs/cloudflare": patch
3+
---
4+
5+
fix: Respect trailing slash config for \_next/image route in worker

packages/cloudflare/src/cli/build/open-next/compile-init.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export async function compileInit(options: BuildOptions, wranglerConfig: Unstabl
1717
const nextConfig = loadConfig(path.join(options.appBuildOutputPath, ".next"));
1818
const basePath = nextConfig.basePath ?? "";
1919
const deploymentId = nextConfig.deploymentId ?? "";
20+
const trailingSlash = nextConfig.trailingSlash ?? false;
2021

2122
await build({
2223
entryPoints: [initPath],
@@ -31,6 +32,7 @@ export async function compileInit(options: BuildOptions, wranglerConfig: Unstabl
3132
__NEXT_BASE_PATH__: JSON.stringify(basePath),
3233
__ASSETS_RUN_WORKER_FIRST__: JSON.stringify(wranglerConfig.assets?.run_worker_first ?? false),
3334
__DEPLOYMENT_ID__: JSON.stringify(deploymentId),
35+
__TRAILING_SLASH__: JSON.stringify(trailingSlash),
3436
},
3537
});
3638
}

packages/cloudflare/src/cli/templates/init.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ function initRuntime() {
9797
__BUILD_TIMESTAMP_MS__,
9898
__NEXT_BASE_PATH__,
9999
__ASSETS_RUN_WORKER_FIRST__,
100+
__TRAILING_SLASH__,
100101
// The external middleware will use the convertTo function of the `edge` converter
101102
// by default it will try to fetch the request, but since we are running everything in the same worker
102103
// we need to use the request as is.
@@ -155,4 +156,6 @@ declare global {
155156
var __ASSETS_RUN_WORKER_FIRST__: boolean | string[] | undefined;
156157
// Deployment ID
157158
var __DEPLOYMENT_ID__: string;
159+
// Next trailingSlash config
160+
var __TRAILING_SLASH__: boolean;
158161
}

packages/cloudflare/src/cli/templates/worker.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ export default {
3939
}
4040

4141
// Fallback for the Next default image loader.
42-
if (url.pathname === `${globalThis.__NEXT_BASE_PATH__}/_next/image`) {
42+
if (
43+
url.pathname ===
44+
`${globalThis.__NEXT_BASE_PATH__}/_next/image${globalThis.__TRAILING_SLASH__ ? "/" : ""}`
45+
) {
4346
const imageUrl = url.searchParams.get("url") ?? "";
4447
return await fetchImage(env.ASSETS, imageUrl, ctx);
4548
}

0 commit comments

Comments
 (0)