Skip to content

Commit e996c53

Browse files
committed
Add support for basePath
1 parent 2ce6454 commit e996c53

File tree

10 files changed

+119
-142
lines changed

10 files changed

+119
-142
lines changed

.changeset/blue-beds-crash.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+
Add support for basePath

examples/bugs/gh-219/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
"dependencies": {
1717
"@hookform/resolvers": "^3.9.1",
1818
"@libsql/client": "^0.14.0",
19-
"@opennextjs/aws": "^3.3.1",
2019
"@t3-oss/env-nextjs": "^0.11.1",
2120
"@tanstack/react-table": "^8.20.6",
2221
"better-sqlite3": "^11.7.0",

packages/cloudflare/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
"homepage": "https://github.com/opennextjs/opennextjs-cloudflare",
5454
"dependencies": {
5555
"@dotenvx/dotenvx": "catalog:",
56-
"@opennextjs/aws": "3.5.6",
56+
"@opennextjs/aws": "https://pkg.pr.new/@opennextjs/aws@821",
5757
"enquirer": "^2.4.1",
5858
"glob": "catalog:",
5959
"ts-tqdm": "^0.8.6"

packages/cloudflare/src/api/overrides/incremental-cache/kv-incremental-cache.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ class KVIncrementalCache implements IncrementalCache {
5656
// if there is no lastModified property, the file was stored during build-time cache population.
5757
return {
5858
value: entry,
59-
// __BUILD_TIMESTAMP_MS__ is injected by ESBuild.
60-
lastModified: (globalThis as { __BUILD_TIMESTAMP_MS__?: number }).__BUILD_TIMESTAMP_MS__,
59+
lastModified: globalThis.__BUILD_TIMESTAMP_MS__,
6160
};
6261
} catch (e) {
6362
error("Failed to get from cache", e);

packages/cloudflare/src/api/overrides/incremental-cache/static-assets-incremental-cache.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ class StaticAssetsIncrementalCache implements IncrementalCache {
3333

3434
return {
3535
value: await response.json(),
36-
// __BUILD_TIMESTAMP_MS__ is injected by ESBuild.
37-
lastModified: (globalThis as { __BUILD_TIMESTAMP_MS__?: number }).__BUILD_TIMESTAMP_MS__,
36+
lastModified: globalThis.__BUILD_TIMESTAMP_MS__,
3837
};
3938
} catch (e) {
4039
error("Failed to get from cache", e);

packages/cloudflare/src/cli/build/build.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export async function build(
7070
// Compile middleware
7171
await createMiddleware(options, { forceOnlyBuildOnce: true });
7272

73-
createStaticAssets(options);
73+
createStaticAssets(options, { useBasePath: true });
7474

7575
if (config.dangerous?.disableIncrementalCache !== true) {
7676
const { useTagCache, metaFiles } = createCacheAssets(options);

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import path from "node:path";
22
import { fileURLToPath } from "node:url";
33

4-
import type { BuildOptions } from "@opennextjs/aws/build/helper";
4+
import { loadConfig } from "@opennextjs/aws/adapters/config/util.js";
5+
import type { BuildOptions } from "@opennextjs/aws/build/helper.js";
56
import { build } from "esbuild";
67

78
/**
@@ -12,6 +13,9 @@ export async function compileInit(options: BuildOptions) {
1213
const templatesDir = path.join(currentDir, "../../templates");
1314
const initPath = path.join(templatesDir, "init.js");
1415

16+
const nextConfig = loadConfig(path.join(options.appBuildOutputPath, ".next"));
17+
const basePath = nextConfig.basePath ?? "";
18+
1519
await build({
1620
entryPoints: [initPath],
1721
outdir: path.join(options.outputDir, "cloudflare"),
@@ -22,6 +26,7 @@ export async function compileInit(options: BuildOptions) {
2226
platform: "node",
2327
define: {
2428
__BUILD_TIMESTAMP_MS__: JSON.stringify(Date.now()),
29+
__NEXT_BASE_PATH__: JSON.stringify(basePath),
2530
},
2631
});
2732
}

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ function initRuntime() {
9191

9292
Object.assign(globalThis, {
9393
Request: CustomRequest,
94-
//@ts-expect-error Inline at build time by ESBuild
9594
__BUILD_TIMESTAMP_MS__: __BUILD_TIMESTAMP_MS__,
95+
__NEXT_BASE_PATH__: __NEXT_BASE_PATH__,
9696
});
9797
}
9898

@@ -126,3 +126,12 @@ function populateProcessEnv(url: URL, env: CloudflareEnv) {
126126
},
127127
});
128128
}
129+
130+
/* eslint-disable no-var */
131+
declare global {
132+
// Build timestamp
133+
var __BUILD_TIMESTAMP_MS__: number;
134+
// Next basePath
135+
var __NEXT_BASE_PATH__: string;
136+
}
137+
/* eslint-enable no-var */

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ export default {
2525
}
2626

2727
// Fallback for the Next default image loader.
28-
if (url.pathname === "/_next/image") {
28+
if (url.pathname === `${globalThis.__NEXT_BASE_PATH__}/_next/image`) {
2929
const imageUrl = url.searchParams.get("url") ?? "";
3030
return imageUrl.startsWith("/")
31-
? env.ASSETS?.fetch(new URL(imageUrl, request.url))
31+
? env.ASSETS?.fetch(`http://assets.local${imageUrl}`)
3232
: fetch(imageUrl, { cf: { cacheEverything: true } });
3333
}
3434

0 commit comments

Comments
 (0)