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
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ import memoryQueue from "@opennextjs/cloudflare/overrides/queue/memory-queue";
export default defineCloudflareConfig({
incrementalCache: r2IncrementalCache,
queue: memoryQueue,
enableCacheInterception: true,
});
1 change: 0 additions & 1 deletion examples-cloudflare/e2e/app-router/open-next.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const baseConfig = defineCloudflareConfig({
}),
// `CACHE_PURGE_ZONE_ID` and `CACHE_PURGE_API_TOKEN` are required to enable cache purge
// cachePurge: purgeCache({ type: "durableObject" }),
enableCacheInterception: true,
queue: queueCache(doQueue),
});

Expand Down
1 change: 0 additions & 1 deletion examples-cloudflare/e2e/experimental/open-next.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,4 @@ export default defineCloudflareConfig({
},
}),
queue: doQueue,
enableCacheInterception: true,
});
1 change: 0 additions & 1 deletion examples-cloudflare/e2e/pages-router/open-next.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ import memoryQueue from "@opennextjs/cloudflare/overrides/queue/memory-queue";

export default defineCloudflareConfig({
incrementalCache: r2IncrementalCache,
enableCacheInterception: true,
queue: memoryQueue,
});
4 changes: 0 additions & 4 deletions examples/app-pages-router/open-next.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ export default {
},
loader: "fs-dev",
},
dangerous: {
enableCacheInterception: true,
useAdapterOutputs: true,
},
// You can override the build command here so that you don't have to rebuild next every time you make a change
// buildCommand: "echo 'No build command'",
} satisfies OpenNextConfig;
2 changes: 0 additions & 2 deletions examples/app-router/open-next.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ export default {

dangerous: {
middlewareHeadersOverrideNextConfigHeaders: true,
useAdapterOutputs: true,
enableCacheInterception: true,
},

imageOptimization: {
Expand Down
5 changes: 0 additions & 5 deletions examples/experimental/open-next.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ export default {
loader: "fs-dev",
},

dangerous: {
enableCacheInterception: true,
useAdapterOutputs: true,
},

// You can override the build command here so that you don't have to rebuild next every time you make a change
//buildCommand: "echo 'No build command'",
} satisfies OpenNextConfig;
5 changes: 0 additions & 5 deletions examples/pages-router/open-next.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ export default {
loader: "fs-dev",
},

dangerous: {
enableCacheInterception: true,
useAdapterOutputs: true,
},

// You can override the build command here so that you don't have to rebuild next every time you make a change
//buildCommand: "echo 'No build command'",
};
20 changes: 1 addition & 19 deletions packages/cloudflare/src/api/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,6 @@ export type CloudflareOverrides = {
*/
cachePurge?: Override<CDNInvalidationHandler>;

/**
* Enable cache interception
* Should be `false` when PPR is used
* @default false
*/
enableCacheInterception?: boolean;

/**
* Route preloading behavior.
* Using a value other than "none" can result in higher CPU usage on cold starts.
Expand All @@ -64,14 +57,7 @@ export type CloudflareOverrides = {
* @returns the OpenNext configuration object
*/
export function defineCloudflareConfig(config: CloudflareOverrides = {}): OpenNextConfig {
const {
incrementalCache,
tagCache,
queue,
cachePurge,
enableCacheInterception = true,
routePreloadingBehavior = "none",
} = config;
const { incrementalCache, tagCache, queue, cachePurge, routePreloadingBehavior = "none" } = config;

return {
default: {
Expand All @@ -91,10 +77,6 @@ export function defineCloudflareConfig(config: CloudflareOverrides = {}): OpenNe
cloudflare: {
useWorkerdCondition: true,
},
dangerous: {
enableCacheInterception,
useAdapterOutputs: true,
},
middleware: {
external: true,
override: {
Expand Down
65 changes: 2 additions & 63 deletions packages/cloudflare/src/cli/build/build.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,10 @@
import { buildNextjsApp, setStandaloneBuildMode } from "@opennextjs/aws/build/buildNextApp.js";
import { compileCache } from "@opennextjs/aws/build/compileCache.js";
import { createCacheAssets, createStaticAssets } from "@opennextjs/aws/build/createAssets.js";
import { createMiddleware } from "@opennextjs/aws/build/createMiddleware.js";
import * as buildHelper from "@opennextjs/aws/build/helper.js";
import { patchOriginalNextConfig } from "@opennextjs/aws/build/patch/patches/index.js";
import { printHeader } from "@opennextjs/aws/build/utils.js";
import logger from "@opennextjs/aws/logger.js";
import type { Unstable_Config } from "wrangler";

import { OpenNextConfig } from "../../api/config.js";
import type { ProjectOptions } from "../project-options.js";

import { bundleServer } from "./bundle-server.js";
import { compileCacheAssetsManifestSqlFile } from "./open-next/compile-cache-assets-manifest.js";
import { compileEnvFiles } from "./open-next/compile-env-files.js";
import { compileImages } from "./open-next/compile-images.js";
import { compileInit } from "./open-next/compile-init.js";
import { compileSkewProtection } from "./open-next/compile-skew-protection.js";
import { compileDurableObjects } from "./open-next/compileDurableObjects.js";
import { createServerBundle } from "./open-next/createServerBundle.js";
import { useNodeMiddleware } from "./utils/middleware.js";
import { getVersion } from "./utils/version.js";

/**
Expand All @@ -31,12 +16,7 @@ import { getVersion } from "./utils/version.js";
* @param config The OpenNext config
* @param projectOpts The options for the project
*/
export async function build(
options: buildHelper.BuildOptions,
config: OpenNextConfig,
projectOpts: ProjectOptions,
wranglerConfig: Unstable_Config
): Promise<void> {
export async function build(options: buildHelper.BuildOptions, projectOpts: ProjectOptions): Promise<void> {
// Do not minify the code so that we can apply string replacement patch.
options.minify = false;

Expand All @@ -61,48 +41,7 @@ export async function build(
buildNextjsApp(options);
}

if (config.dangerous?.useAdapterOutputs) {
logger.info("Using adapter outputs for building OpenNext bundle.");
return;
}

// Make sure no Node.js middleware is used
if (useNodeMiddleware(options)) {
logger.error("Node.js middleware is not currently supported. Consider switching to Edge Middleware.");
process.exit(1);
}

// Generate deployable bundle
printHeader("Generating bundle");

await patchOriginalNextConfig(options);

compileCache(options);
compileEnvFiles(options);
await compileInit(options, wranglerConfig);
await compileImages(options);
await compileSkewProtection(options, config);

// Compile middleware
await createMiddleware(options, { forceOnlyBuildOnce: true });

createStaticAssets(options, { useBasePath: true });

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

if (useTagCache) {
compileCacheAssetsManifestSqlFile(options, metaFiles);
}
}

await createServerBundle(options);

await compileDurableObjects(options);

await bundleServer(options, projectOpts);

logger.info("OpenNext build complete.");
logger.info("Using adapter outputs for building OpenNext bundle.");
}
Comment thread
conico974 marked this conversation as resolved.

async function ensureNextjsVersionSupported({ nextVersion }: buildHelper.BuildOptions) {
Expand Down
21 changes: 4 additions & 17 deletions packages/cloudflare/src/cli/build/open-next/createServerBundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import path from "node:path";
import { loadMiddlewareManifest } from "@opennextjs/aws/adapters/config/util.js";
import { compileCache } from "@opennextjs/aws/build/compileCache.js";
import { copyAdapterFiles } from "@opennextjs/aws/build/copyAdapterFiles.js";
import { copyTracedFiles } from "@opennextjs/aws/build/copyTracedFiles.js";
import { copyMiddlewareResources, generateEdgeBundle } from "@opennextjs/aws/build/edge/createEdgeBundle.js";
import * as buildHelper from "@opennextjs/aws/build/helper.js";
import { installDependencies } from "@opennextjs/aws/build/installDeps.js";
Expand Down Expand Up @@ -187,23 +186,11 @@ async function generateBundle(
let manifests: any = {};

// Copy all necessary traced files
if (config.dangerous?.useAdapterOutputs) {
if (!buildCtx) {
throw new Error("should not happen");
}
tracedFiles = await copyAdapterFiles(options, name, packagePath, buildCtx.outputs);
//TODO: we should load manifests here
} else {
const oldTracedFileOutput = await copyTracedFiles({
buildOutputPath: appBuildOutputPath,
packagePath,
outputDir: outputPath,
routes: fnOptions.routes ?? ["app/page.tsx"],
skipServerFiles: options.config.dangerous?.useAdapterOutputs === true,
});
tracedFiles = oldTracedFileOutput.tracedFiles;
manifests = oldTracedFileOutput.manifests;
if (!buildCtx) {
throw new Error("should not happen");
}
tracedFiles = await copyAdapterFiles(options, name, packagePath, buildCtx.outputs);
//TODO: we should load manifests here

// TODO(vicb): what should `nodePackages` be for the adapter
// if (getOpenNextConfig(options).cloudflare?.useWorkerdCondition !== false) {
Expand Down
12 changes: 3 additions & 9 deletions packages/cloudflare/src/cli/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
getNormalizedOptions,
nextAppDir,
printHeaders,
readWranglerConfig,
withWranglerOptions,
withWranglerPassthroughArgs,
} from "./utils.js";
Expand All @@ -36,11 +35,8 @@ async function buildCommand(

const projectOpts = { ...args, minify: !args.noMinify, sourceDir: nextAppDir };

if (config.dangerous?.useAdapterOutputs) {
console.log("Using adapter outputs for building OpenNext bundle.");
const require = createRequire(import.meta.url);
process.env.NEXT_ADAPTER_PATH = require.resolve("../adapter.js");
}
const require = createRequire(import.meta.url);
process.env.NEXT_ADAPTER_PATH = require.resolve("../adapter.js");

// Ask whether a `wrangler.jsonc` should be created when no config file exists.
// Note: We don't ask when a custom config file is specified via `--config`
Expand All @@ -49,9 +45,7 @@ async function buildCommand(
await createWranglerConfigIfNotExistent(projectOpts);
}

const wranglerConfig = await readWranglerConfig(args);

await buildImpl(options, config, projectOpts, wranglerConfig);
await buildImpl(options, projectOpts);
}

/**
Expand Down
2 changes: 0 additions & 2 deletions packages/open-next/src/adapters/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,11 @@ const defaultHandler = async (

const assetResolver = await resolveAssetResolver(middlewareConfig?.assetResolver);

//#override includeCacheInMiddleware
globalThis.tagCache = await resolveTagCache(middlewareConfig?.override?.tagCache);

globalThis.queue = await resolveQueue(middlewareConfig?.override?.queue);

globalThis.incrementalCache = await resolveIncrementalCache(middlewareConfig?.override?.incrementalCache);
//#endOverride

const requestId = Math.random().toString(36);

Expand Down
39 changes: 3 additions & 36 deletions packages/open-next/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,43 +47,10 @@ export async function build(openNextConfigPath?: string, nodeExternals?: string)
// Build Next.js app
printHeader("Building Next.js app");
setStandaloneBuildMode(options);
if (config.dangerous?.useAdapterOutputs) {
logger.info("Using adapter outputs for building OpenNext bundle.");
process.env.NEXT_ADAPTER_PATH = require.resolve("./adapter.js");
}
logger.info("Using adapter outputs for building OpenNext bundle.");
process.env.NEXT_ADAPTER_PATH = require.resolve("./adapter.js");
buildHelper.initOutputDir(options);
buildNextjsApp(options);

if (config.dangerous?.useAdapterOutputs) {
logger.info("Using adapter outputs for building OpenNext bundle.");
return;
}

// Generate deployable bundle
printHeader("Generating bundle");

// Patch the original Next.js config
await patchOriginalNextConfig(options);

// Compile cache.ts
compileCache(options);

// Compile middleware
await createMiddleware(options);

createStaticAssets(options);

if (config.dangerous?.disableIncrementalCache !== true) {
const { useTagCache } = createCacheAssets(options);
if (useTagCache) {
await compileTagCacheProvider(options);
}
}

await createServerBundle(options);
await createRevalidationBundle(options);
await createImageOptimizationBundle(options);
await createWarmerBundle(options);
await generateOutput(options);
logger.info("OpenNext build complete.");
return;
Comment thread
conico974 marked this conversation as resolved.
}
4 changes: 1 addition & 3 deletions packages/open-next/src/build/createAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@ export function createCacheAssets(options: buildHelper.BuildOptions) {
const buildId = buildHelper.getBuildId(options);
let useTagCache = false;

const dotNextPath = options.config.dangerous?.useAdapterOutputs
? appBuildOutputPath
: path.join(appBuildOutputPath, ".next/standalone", packagePath);
const dotNextPath = appBuildOutputPath;

const outputCachePath = path.join(outputDir, "cache", buildId);
fs.mkdirSync(outputCachePath, { recursive: true });
Expand Down
1 change: 0 additions & 1 deletion packages/open-next/src/build/createMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ export async function createMiddleware(
originResolver: config.middleware.originResolver,
},
defaultConverter: "aws-cloudfront",
includeCache: config.dangerous?.enableCacheInterception,
additionalExternals: config.edgeExternals,
onlyBuildOnce: forceOnlyBuildOnce === true,
name: "middleware",
Expand Down
24 changes: 9 additions & 15 deletions packages/open-next/src/build/createServerBundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { getCrossPlatformPathRegex } from "../utils/regex.js";

import { compileCache } from "./compileCache.js";
import { copyAdapterFiles } from "./copyAdapterFiles.js";
import { copyTracedFiles, getManifests } from "./copyTracedFiles.js";
import { getManifests } from "./copyTracedFiles.js";
import { copyMiddlewareResources, generateEdgeBundle } from "./edge/createEdgeBundle.js";
import * as buildHelper from "./helper.js";
import { installDependencies } from "./installDeps.js";
Expand Down Expand Up @@ -174,23 +174,17 @@ async function generateBundle(
buildHelper.copyEnvFile(appBuildOutputPath, packagePath, outputPath);

let tracedFiles: string[] = [];
let manifests: ReturnType<typeof getManifests> | Record<string, never> = {};
let manifests: ReturnType<typeof getManifests> = {} as ReturnType<typeof getManifests>;

// Copy all necessary traced files
if (config.dangerous?.useAdapterOutputs) {
tracedFiles = await copyAdapterFiles(options, name, packagePath, nextOutputs!);
//TODO: we should load manifests here
} else {
const oldTracedFileOutput = await copyTracedFiles({
buildOutputPath: appBuildOutputPath,
packagePath,
outputDir: outputPath,
routes: fnOptions.routes ?? ["app/page.tsx"],
skipServerFiles: options.config.dangerous?.useAdapterOutputs === true,
});
tracedFiles = oldTracedFileOutput.tracedFiles;
manifests = oldTracedFileOutput.manifests;
if (!nextOutputs) {
throw new Error(
"createServerBundle was called without adapter outputs. " +
"Please ensure NextAdapterOutputs is provided to createServerBundle."
);
}
tracedFiles = await copyAdapterFiles(options, name, packagePath, nextOutputs);
//TODO: we should load manifests here

const additionalCodePatches = codeCustomization?.additionalCodePatches ?? [];

Expand Down
Loading
Loading