Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/blue-beds-crash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opennextjs/cloudflare": patch
---

Add support for basePath
5 changes: 5 additions & 0 deletions .changeset/silly-icons-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opennextjs/cloudflare": patch
---

bump @opennextjs/aws to 3.5.7
1 change: 0 additions & 1 deletion examples/bugs/gh-219/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"dependencies": {
"@hookform/resolvers": "^3.9.1",
"@libsql/client": "^0.14.0",
"@opennextjs/aws": "^3.3.1",
"@t3-oss/env-nextjs": "^0.11.1",
"@tanstack/react-table": "^8.20.6",
"better-sqlite3": "^11.7.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/cloudflare/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"homepage": "https://github.com/opennextjs/opennextjs-cloudflare",
"dependencies": {
"@dotenvx/dotenvx": "catalog:",
"@opennextjs/aws": "3.5.6",
"@opennextjs/aws": "3.5.7",
"enquirer": "^2.4.1",
"glob": "catalog:",
"ts-tqdm": "^0.8.6"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ class KVIncrementalCache implements IncrementalCache {
// if there is no lastModified property, the file was stored during build-time cache population.
return {
value: entry,
// __BUILD_TIMESTAMP_MS__ is injected by ESBuild.
lastModified: (globalThis as { __BUILD_TIMESTAMP_MS__?: number }).__BUILD_TIMESTAMP_MS__,
lastModified: globalThis.__BUILD_TIMESTAMP_MS__,
};
} catch (e) {
error("Failed to get from cache", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ class StaticAssetsIncrementalCache implements IncrementalCache {

return {
value: await response.json(),
// __BUILD_TIMESTAMP_MS__ is injected by ESBuild.
lastModified: (globalThis as { __BUILD_TIMESTAMP_MS__?: number }).__BUILD_TIMESTAMP_MS__,
lastModified: globalThis.__BUILD_TIMESTAMP_MS__,
};
} catch (e) {
error("Failed to get from cache", e);
Expand Down
2 changes: 1 addition & 1 deletion packages/cloudflare/src/cli/build/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export async function build(
// Compile middleware
await createMiddleware(options, { forceOnlyBuildOnce: true });

createStaticAssets(options);
createStaticAssets(options, { useBasePath: true });

if (config.dangerous?.disableIncrementalCache !== true) {
const { useTagCache, metaFiles } = createCacheAssets(options);
Expand Down
1 change: 1 addition & 0 deletions packages/cloudflare/src/cli/build/bundle-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ export async function updateWorkerBundledCode(
[
"'require(this.middlewareManifestPath)'",
(code) => patches.inlineMiddlewareManifestRequire(code, buildOpts),
{ isOptional: true },
],
[
"`require.resolve` call",
Expand Down
7 changes: 6 additions & 1 deletion packages/cloudflare/src/cli/build/open-next/compile-init.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import path from "node:path";
import { fileURLToPath } from "node:url";

import type { BuildOptions } from "@opennextjs/aws/build/helper";
import { loadConfig } from "@opennextjs/aws/adapters/config/util.js";
import type { BuildOptions } from "@opennextjs/aws/build/helper.js";
import { build } from "esbuild";

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

const nextConfig = loadConfig(path.join(options.appBuildOutputPath, ".next"));
const basePath = nextConfig.basePath ?? "";

await build({
entryPoints: [initPath],
outdir: path.join(options.outputDir, "cloudflare"),
Expand All @@ -22,6 +26,7 @@ export async function compileInit(options: BuildOptions) {
platform: "node",
define: {
__BUILD_TIMESTAMP_MS__: JSON.stringify(Date.now()),
__NEXT_BASE_PATH__: JSON.stringify(basePath),
},
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ import { installDependencies } from "@opennextjs/aws/build/installDeps.js";
import type { CodePatcher } from "@opennextjs/aws/build/patch/codePatcher.js";
import { applyCodePatches } from "@opennextjs/aws/build/patch/codePatcher.js";
import {
patchEnvVars,
patchFetchCacheForISR,
patchFetchCacheSetMissingWaitUntil,
patchNextServer,
patchUnstableCacheForISR,
} from "@opennextjs/aws/build/patch/patches/index.js";
// TODO: import from patches/index.js when https://github.com/opennextjs/opennextjs-aws/pull/827 is released
import { patchBackgroundRevalidation } from "@opennextjs/aws/build/patch/patches/patchBackgroundRevalidation.js";
import logger from "@opennextjs/aws/logger.js";
import { minifyAll } from "@opennextjs/aws/minimize-js.js";
import type { ContentUpdater } from "@opennextjs/aws/plugins/content-updater.js";
Expand Down Expand Up @@ -190,6 +194,9 @@ async function generateBundle(
patchFetchCacheSetMissingWaitUntil,
patchFetchCacheForISR,
patchUnstableCacheForISR,
patchNextServer,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

patchNextServer and patchEnvVars probably means that we can get rid of the NextMinimal patch in here. Likely in a different PR though

patchEnvVars,
patchBackgroundRevalidation,
// Cloudflare specific patches
patchResRevalidate,
...additionalCodePatches,
Expand Down Expand Up @@ -220,6 +227,7 @@ async function generateBundle(
...(disableNextPrebundledReact ? ["applyNextjsPrebundledReact"] : []),
...(disableRouting ? ["withRouting"] : []),
...(isAfter142 ? ["patchAsyncStorage"] : []),
...(isAfter141 ? ["appendPrefetch"] : []),
],
}),
openNextReplacementPlugin({
Expand Down
11 changes: 10 additions & 1 deletion packages/cloudflare/src/cli/templates/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ function initRuntime() {

Object.assign(globalThis, {
Request: CustomRequest,
//@ts-expect-error Inline at build time by ESBuild
__BUILD_TIMESTAMP_MS__: __BUILD_TIMESTAMP_MS__,
__NEXT_BASE_PATH__: __NEXT_BASE_PATH__,
});
}

Expand Down Expand Up @@ -126,3 +126,12 @@ function populateProcessEnv(url: URL, env: CloudflareEnv) {
},
});
}

/* eslint-disable no-var */
declare global {
// Build timestamp
var __BUILD_TIMESTAMP_MS__: number;
// Next basePath
var __NEXT_BASE_PATH__: string;
}
/* eslint-enable no-var */
4 changes: 2 additions & 2 deletions packages/cloudflare/src/cli/templates/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ export default {
}

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

Expand Down
Loading