Skip to content

Commit d310919

Browse files
authored
Add support for basePath (#574)
1 parent 2ce6454 commit d310919

File tree

13 files changed

+137
-147
lines changed

13 files changed

+137
-147
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

.changeset/silly-icons-battle.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+
bump @opennextjs/aws to 3.5.7

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": "3.5.7",
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/bundle-server.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ export async function updateWorkerBundledCode(
176176
[
177177
"'require(this.middlewareManifestPath)'",
178178
(code) => patches.inlineMiddlewareManifestRequire(code, buildOpts),
179+
{ isOptional: true },
179180
],
180181
[
181182
"`require.resolve` call",

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/build/open-next/createServerBundle.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@ import { installDependencies } from "@opennextjs/aws/build/installDeps.js";
1414
import type { CodePatcher } from "@opennextjs/aws/build/patch/codePatcher.js";
1515
import { applyCodePatches } from "@opennextjs/aws/build/patch/codePatcher.js";
1616
import {
17+
patchEnvVars,
1718
patchFetchCacheForISR,
1819
patchFetchCacheSetMissingWaitUntil,
20+
patchNextServer,
1921
patchUnstableCacheForISR,
2022
} from "@opennextjs/aws/build/patch/patches/index.js";
23+
// TODO: import from patches/index.js when https://github.com/opennextjs/opennextjs-aws/pull/827 is released
24+
import { patchBackgroundRevalidation } from "@opennextjs/aws/build/patch/patches/patchBackgroundRevalidation.js";
2125
import logger from "@opennextjs/aws/logger.js";
2226
import { minifyAll } from "@opennextjs/aws/minimize-js.js";
2327
import type { ContentUpdater } from "@opennextjs/aws/plugins/content-updater.js";
@@ -190,6 +194,9 @@ async function generateBundle(
190194
patchFetchCacheSetMissingWaitUntil,
191195
patchFetchCacheForISR,
192196
patchUnstableCacheForISR,
197+
patchNextServer,
198+
patchEnvVars,
199+
patchBackgroundRevalidation,
193200
// Cloudflare specific patches
194201
patchResRevalidate,
195202
...additionalCodePatches,
@@ -220,6 +227,7 @@ async function generateBundle(
220227
...(disableNextPrebundledReact ? ["applyNextjsPrebundledReact"] : []),
221228
...(disableRouting ? ["withRouting"] : []),
222229
...(isAfter142 ? ["patchAsyncStorage"] : []),
230+
...(isAfter141 ? ["appendPrefetch"] : []),
223231
],
224232
}),
225233
openNextReplacementPlugin({

0 commit comments

Comments
 (0)