@opennextjs/aws 3.5.3 → 3.5.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,10 +6,14 @@ import type { OpenNextConfig } from "../types/open-next.js";
6
6
  *
7
7
  * @param baseDir Directory where to look for the configuration.
8
8
  * @param openNextConfigPath Override the default configuration when provided. Relative to baseDir.
9
- * @param nodeExternals Externals for the Node.js compilation.
9
+ * @param nodeExternals Coma separated list of Externals for the Node.js compilation.
10
+ * @param compileEdge Force compiling for the edge runtime when true
10
11
  * @return The configuration and the build directory.
11
12
  */
12
- export declare function compileOpenNextConfig(baseDir: string, openNextConfigPath?: string, nodeExternals?: string): Promise<{
13
+ export declare function compileOpenNextConfig(baseDir: string, openNextConfigPath?: string, { nodeExternals, compileEdge }?: {
14
+ nodeExternals?: string | undefined;
15
+ compileEdge?: boolean | undefined;
16
+ }): Promise<{
13
17
  config: OpenNextConfig;
14
18
  buildDir: string;
15
19
  }>;
@@ -11,13 +11,14 @@ import { validateConfig } from "./validateConfig.js";
11
11
  *
12
12
  * @param baseDir Directory where to look for the configuration.
13
13
  * @param openNextConfigPath Override the default configuration when provided. Relative to baseDir.
14
- * @param nodeExternals Externals for the Node.js compilation.
14
+ * @param nodeExternals Coma separated list of Externals for the Node.js compilation.
15
+ * @param compileEdge Force compiling for the edge runtime when true
15
16
  * @return The configuration and the build directory.
16
17
  */
17
- export async function compileOpenNextConfig(baseDir, openNextConfigPath, nodeExternals) {
18
+ export async function compileOpenNextConfig(baseDir, openNextConfigPath, { nodeExternals = "", compileEdge = false } = {}) {
18
19
  const sourcePath = path.join(baseDir, openNextConfigPath ?? "open-next.config.ts");
19
20
  const buildDir = fs.mkdtempSync(path.join(os.tmpdir(), "open-next-tmp"));
20
- let configPath = compileOpenNextConfigNode(sourcePath, buildDir, nodeExternals ? nodeExternals.split(",") : []);
21
+ let configPath = compileOpenNextConfigNode(sourcePath, buildDir, nodeExternals.split(","));
21
22
  // On Windows, we need to use file:// protocol to load the config file using import()
22
23
  if (process.platform === "win32")
23
24
  configPath = `file://${configPath}`;
@@ -31,12 +32,12 @@ export async function compileOpenNextConfig(baseDir, openNextConfigPath, nodeExt
31
32
  // If it does, we need to compile it with the edge runtime
32
33
  const usesEdgeRuntime = (config.middleware?.external && config.middleware.runtime !== "node") ||
33
34
  Object.values(config.functions || {}).some((fn) => fn.runtime === "edge");
34
- if (!usesEdgeRuntime) {
35
- logger.debug("No edge runtime found in the open-next.config.ts. Using default config.");
36
- //Nothing to do here
35
+ if (usesEdgeRuntime || compileEdge) {
36
+ compileOpenNextConfigEdge(sourcePath, buildDir, config.edgeExternals ?? []);
37
37
  }
38
38
  else {
39
- compileOpenNextConfigEdge(sourcePath, buildDir, config.edgeExternals ?? []);
39
+ // Skip compiling for the edge runtime.
40
+ logger.debug("No edge runtime found in the open-next.config.ts. Using default config.");
40
41
  }
41
42
  return { config, buildDir };
42
43
  }
@@ -55,12 +55,12 @@ export async function copyTracedFiles({ buildOutputPath, packagePath, outputDir,
55
55
  });
56
56
  }
57
57
  // create directory for pages
58
- if (existsSync(path.join(standaloneServerDir, "pages"))) {
58
+ if (existsSync(path.join(standaloneNextDir, "server/pages"))) {
59
59
  mkdirSync(path.join(outputNextDir, "server/pages"), {
60
60
  recursive: true,
61
61
  });
62
62
  }
63
- if (existsSync(path.join(standaloneServerDir, "app"))) {
63
+ if (existsSync(path.join(standaloneNextDir, "server/app"))) {
64
64
  mkdirSync(path.join(outputNextDir, "server/app"), {
65
65
  recursive: true,
66
66
  });
@@ -210,7 +210,7 @@ File ${fullFilePath} does not exist
210
210
  // Maybe even move this to s3 directly
211
211
  if (hasPageDir) {
212
212
  // First we get truly static files - i.e. pages without getStaticProps
213
- const staticFiles = Object.values(JSON.parse(readFileSync(path.join(standaloneServerDir, "pages-manifest.json"), "utf8")));
213
+ const staticFiles = Object.values(loadPagesManifest(standaloneNextDir));
214
214
  // Then we need to get all fallback: true dynamic routes html
215
215
  const prerenderManifest = loadPrerenderManifest(standaloneNextDir);
216
216
  const config = loadConfig(standaloneNextDir);
@@ -58,6 +58,7 @@ export async function createMiddleware(options, { forceOnlyBuildOnce = false } =
58
58
  outfile: path.join(options.buildDir, "middleware.mjs"),
59
59
  middlewareInfo: edgeMiddlewareInfo,
60
60
  options,
61
+ overrides: config.default.override,
61
62
  onlyBuildOnce: true,
62
63
  name: "middleware",
63
64
  });
@@ -1,5 +1,6 @@
1
1
  import fs from "node:fs";
2
2
  import path from "node:path";
3
+ import { loadMiddlewareManifest } from "../adapters/config/util.js";
3
4
  import logger from "../logger.js";
4
5
  import { minifyAll } from "../minimize-js.js";
5
6
  import { ContentUpdater } from "../plugins/content-updater.js";
@@ -9,7 +10,7 @@ import { getCrossPlatformPathRegex } from "../utils/regex.js";
9
10
  import { bundleNextServer } from "./bundleNextServer.js";
10
11
  import { compileCache } from "./compileCache.js";
11
12
  import { copyTracedFiles } from "./copyTracedFiles.js";
12
- import { generateEdgeBundle } from "./edge/createEdgeBundle.js";
13
+ import { copyMiddlewareResources, generateEdgeBundle, } from "./edge/createEdgeBundle.js";
13
14
  import * as buildHelper from "./helper.js";
14
15
  import { installDependencies } from "./installDeps.js";
15
16
  import { applyCodePatches } from "./patch/codePatcher.js";
@@ -82,25 +83,28 @@ async function generateBundle(name, options, fnOptions, codeCustomization) {
82
83
  // `.next/standalone/package/path` (ie. `.next`, `server.js`).
83
84
  // We need to output the handler file inside the package path.
84
85
  const packagePath = buildHelper.getPackagePath(options);
85
- fs.mkdirSync(path.join(outputPath, packagePath), { recursive: true });
86
+ const outPackagePath = path.join(outputPath, packagePath);
87
+ fs.mkdirSync(outPackagePath, { recursive: true });
86
88
  const ext = fnOptions.runtime === "deno" ? "mjs" : "cjs";
87
- fs.copyFileSync(path.join(options.buildDir, `cache.${ext}`), path.join(outputPath, packagePath, "cache.cjs"));
89
+ fs.copyFileSync(path.join(options.buildDir, `cache.${ext}`), path.join(outPackagePath, "cache.cjs"));
88
90
  if (fnOptions.runtime === "deno") {
89
91
  addDenoJson(outputPath, packagePath);
90
92
  }
91
93
  // Bundle next server if necessary
92
94
  const isBundled = fnOptions.experimentalBundledNextServer ?? false;
93
95
  if (isBundled) {
94
- await bundleNextServer(path.join(outputPath, packagePath), appPath, {
96
+ await bundleNextServer(outPackagePath, appPath, {
95
97
  minify: options.minify,
96
98
  });
97
99
  }
98
100
  // Copy middleware
99
101
  if (!config.middleware?.external) {
100
- fs.copyFileSync(path.join(options.buildDir, "middleware.mjs"), path.join(outputPath, packagePath, "middleware.mjs"));
102
+ fs.copyFileSync(path.join(options.buildDir, "middleware.mjs"), path.join(outPackagePath, "middleware.mjs"));
103
+ const middlewareManifest = loadMiddlewareManifest(path.join(options.appBuildOutputPath, ".next"));
104
+ copyMiddlewareResources(options, middlewareManifest.middleware["/"], outPackagePath);
101
105
  }
102
106
  // Copy open-next.config.mjs
103
- buildHelper.copyOpenNextConfig(options.buildDir, path.join(outputPath, packagePath));
107
+ buildHelper.copyOpenNextConfig(options.buildDir, outPackagePath);
104
108
  // Copy env files
105
109
  buildHelper.copyEnvFile(appBuildOutputPath, packagePath, outputPath);
106
110
  // Copy all necessary traced files
@@ -233,7 +237,7 @@ function addMonorepoEntrypoint(outputPath, packagePath) {
233
237
  // that re-exports the real handler.
234
238
  // Always use posix path for import path
235
239
  const packagePosixPath = packagePath.split(path.sep).join(path.posix.sep);
236
- fs.writeFileSync(path.join(outputPath, "index.mjs"), `export * from "./${packagePosixPath}/index.mjs";`);
240
+ fs.writeFileSync(path.join(outputPath, "index.mjs"), `export { handler } from "./${packagePosixPath}/index.mjs";`);
237
241
  }
238
242
  async function minifyServerBundle(outputDir) {
239
243
  logger.info("Minimizing server function...");
@@ -23,4 +23,8 @@ interface BuildEdgeBundleOptions {
23
23
  }
24
24
  export declare function buildEdgeBundle({ middlewareInfo, entrypoint, outfile, options, defaultConverter, overrides, additionalInject, includeCache, additionalExternals, onlyBuildOnce, name, additionalPlugins: additionalPluginsFn, }: BuildEdgeBundleOptions): Promise<void>;
25
25
  export declare function generateEdgeBundle(name: string, options: BuildOptions, fnOptions: SplittedFunctionOptions, additionalPlugins?: (contentUpdater: ContentUpdater) => Plugin[]): Promise<void>;
26
+ /**
27
+ * Copy wasm files and assets into the destDir.
28
+ */
29
+ export declare function copyMiddlewareResources(options: BuildOptions, middlewareInfo: MiddlewareInfo | undefined, destDir: string): void;
26
30
  export {};
@@ -13,7 +13,7 @@ import { getCrossPlatformPathRegex } from "../../utils/regex.js";
13
13
  import { isEdgeRuntime } from "../helper.js";
14
14
  import { copyOpenNextConfig, esbuildAsync } from "../helper.js";
15
15
  export async function buildEdgeBundle({ middlewareInfo, entrypoint, outfile, options, defaultConverter, overrides, additionalInject, includeCache, additionalExternals, onlyBuildOnce, name, additionalPlugins: additionalPluginsFn, }) {
16
- const isInCloudfare = await isEdgeRuntime(overrides);
16
+ const isInCloudflare = await isEdgeRuntime(overrides);
17
17
  function override(target) {
18
18
  return typeof overrides?.[target] === "string"
19
19
  ? overrides[target]
@@ -56,7 +56,7 @@ export async function buildEdgeBundle({ middlewareInfo, entrypoint, outfile, opt
56
56
  openNextEdgePlugins({
57
57
  middlewareInfo,
58
58
  nextDir: path.join(options.appBuildOutputPath, ".next"),
59
- isInCloudfare,
59
+ isInCloudflare,
60
60
  }),
61
61
  ...additionalPlugins,
62
62
  // The content updater plugin must be the last plugin
@@ -97,7 +97,7 @@ Object.defineProperty = function(o, p, a) {
97
97
  return defaultDefineProperty(o, p, a);
98
98
  };
99
99
 
100
- ${isInCloudfare
100
+ ${isInCloudflare
101
101
  ? ""
102
102
  : `
103
103
  const require = (await import("node:module")).createRequire(import.meta.url);
@@ -141,18 +141,7 @@ export async function generateEdgeBundle(name, options, fnOptions, additionalPlu
141
141
  throw new Error("Only one function is supported for now");
142
142
  }
143
143
  const middlewareInfo = functions[0];
144
- //Copy wasm files
145
- const wasmFiles = middlewareInfo.wasm;
146
- mkdirSync(path.join(outputDir, "wasm"), { recursive: true });
147
- for (const wasmFile of wasmFiles) {
148
- fs.copyFileSync(path.join(buildOutputDotNextDir, wasmFile.filePath), path.join(outputDir, `wasm/${wasmFile.name}.wasm`));
149
- }
150
- // Copy assets
151
- const assets = middlewareInfo.assets;
152
- mkdirSync(path.join(outputDir, "assets"), { recursive: true });
153
- for (const asset of assets) {
154
- fs.copyFileSync(path.join(buildOutputDotNextDir, asset.filePath), path.join(outputDir, `assets/${asset.name}`));
155
- }
144
+ copyMiddlewareResources(options, middlewareInfo, outputDir);
156
145
  await buildEdgeBundle({
157
146
  middlewareInfo,
158
147
  entrypoint: path.join(options.openNextDistDir, "adapters/edge-adapter.js"),
@@ -164,3 +153,16 @@ export async function generateEdgeBundle(name, options, fnOptions, additionalPlu
164
153
  additionalPlugins,
165
154
  });
166
155
  }
156
+ /**
157
+ * Copy wasm files and assets into the destDir.
158
+ */
159
+ export function copyMiddlewareResources(options, middlewareInfo, destDir) {
160
+ mkdirSync(path.join(destDir, "wasm"), { recursive: true });
161
+ for (const file of middlewareInfo?.wasm ?? []) {
162
+ fs.copyFileSync(path.join(options.appBuildOutputPath, ".next", file.filePath), path.join(destDir, `wasm/${file.name}.wasm`));
163
+ }
164
+ mkdirSync(path.join(destDir, "assets"), { recursive: true });
165
+ for (const file of middlewareInfo?.assets ?? []) {
166
+ fs.copyFileSync(path.join(options.appBuildOutputPath, ".next", file.filePath), path.join(destDir, `assets/${file.name}`));
167
+ }
168
+ }
@@ -317,7 +317,7 @@ export async function isEdgeRuntime(overrides) {
317
317
  return false;
318
318
  }
319
319
  if (typeof overrides.wrapper === "string") {
320
- return ["cloudflare-edge", "cloudflare"].includes(overrides.wrapper);
320
+ return ["cloudflare-edge", "cloudflare", "cloudflare-node"].includes(overrides.wrapper);
321
321
  }
322
322
  return (await overrides?.wrapper?.())?.edgeRuntime;
323
323
  }
package/dist/build.js CHANGED
@@ -17,7 +17,7 @@ export async function build(openNextConfigPath, nodeExternals) {
17
17
  showWarningOnWindows();
18
18
  const baseDir = process.cwd();
19
19
  const openNextDistDir = url.fileURLToPath(new URL(".", import.meta.url));
20
- const { config, buildDir } = await compileOpenNextConfig(baseDir, openNextConfigPath, nodeExternals);
20
+ const { config, buildDir } = await compileOpenNextConfig(baseDir, openNextConfigPath, { nodeExternals });
21
21
  // Initialize options
22
22
  const options = buildHelper.normalizeOptions(config, openNextDistDir, buildDir);
23
23
  logger.setLevel(options.debug ? "debug" : "info");
@@ -1,4 +1,4 @@
1
- import { FunctionsConfigManifest, MiddlewareManifest, NextConfig, } from "../../adapters/config/index.js";
1
+ import { FunctionsConfigManifest, MiddlewareManifest, NextConfig, PrerenderManifest, } from "../../adapters/config/index.js";
2
2
  import { emptyReadableStream } from "../../utils/stream.js";
3
3
  import { localizePath } from "./i18n/index.js";
4
4
  //NOTE: we should try to avoid importing stuff from next as much as possible
@@ -24,7 +24,11 @@ function defaultMiddlewareLoader() {
24
24
  export async function handleMiddleware(internalEvent, middlewareLoader = defaultMiddlewareLoader) {
25
25
  const headers = internalEvent.headers;
26
26
  // We bypass the middleware if the request is internal
27
- if (headers["x-isr"])
27
+ // We should only do that if the request has the correct `x-prerender-revalidate` header
28
+ // The `x-prerender-revalidate` header is set at build time and should be safe to trust
29
+ if (headers["x-isr"] &&
30
+ headers["x-prerender-revalidate"] ===
31
+ PrerenderManifest.preview.previewModeId)
28
32
  return internalEvent;
29
33
  // We only need the normalizedPath to check if the middleware should run
30
34
  const normalizedPath = localizePath(internalEvent);
@@ -8,7 +8,6 @@ const wrapper = async (handler, converter) => {
8
8
  res.setHeader("Set-Cookie", prelude.cookies);
9
9
  res.writeHead(prelude.statusCode, prelude.headers);
10
10
  res.flushHeaders();
11
- res.uncork();
12
11
  return res;
13
12
  },
14
13
  };
@@ -3,12 +3,12 @@ import type { MiddlewareInfo } from "../types/next-types.js";
3
3
  export interface IPluginSettings {
4
4
  nextDir: string;
5
5
  middlewareInfo?: MiddlewareInfo;
6
- isInCloudfare?: boolean;
6
+ isInCloudflare?: boolean;
7
7
  }
8
8
  /**
9
9
  * @param opts.nextDir - The path to the .next directory
10
10
  * @param opts.middlewareInfo - Information about the middleware
11
- * @param opts.isInCloudfare - Whether the code runs on the cloudflare runtime
11
+ * @param opts.isInCloudflare - Whether the code runs on the cloudflare runtime
12
12
  * @returns
13
13
  */
14
- export declare function openNextEdgePlugins({ nextDir, middlewareInfo, isInCloudfare, }: IPluginSettings): Plugin;
14
+ export declare function openNextEdgePlugins({ nextDir, middlewareInfo, isInCloudflare, }: IPluginSettings): Plugin;
@@ -7,10 +7,10 @@ import { getCrossPlatformPathRegex } from "../utils/regex.js";
7
7
  /**
8
8
  * @param opts.nextDir - The path to the .next directory
9
9
  * @param opts.middlewareInfo - Information about the middleware
10
- * @param opts.isInCloudfare - Whether the code runs on the cloudflare runtime
10
+ * @param opts.isInCloudflare - Whether the code runs on the cloudflare runtime
11
11
  * @returns
12
12
  */
13
- export function openNextEdgePlugins({ nextDir, middlewareInfo, isInCloudfare, }) {
13
+ export function openNextEdgePlugins({ nextDir, middlewareInfo, isInCloudflare, }) {
14
14
  const entryFiles = middlewareInfo?.files.map((file) => path.join(nextDir, file)) ?? [];
15
15
  const routes = middlewareInfo
16
16
  ? [
@@ -54,7 +54,7 @@ globalThis._ENTRIES = {};
54
54
  globalThis.self = globalThis;
55
55
  globalThis._ROUTES = ${JSON.stringify(routes)};
56
56
 
57
- ${isInCloudfare
57
+ ${isInCloudflare
58
58
  ? ""
59
59
  : `
60
60
  import {readFileSync} from "node:fs";
@@ -97,11 +97,7 @@ if (!globalThis.URLPattern) {
97
97
  await import("urlpattern-polyfill");
98
98
  }
99
99
  `}
100
- ${wasmFiles
101
- .map((file) => isInCloudfare
102
- ? `import ${file.name} from './wasm/${file.name}.wasm';`
103
- : `const ${file.name} = readFileSync(path.join(__dirname,'/wasm/${file.name}.wasm'));`)
104
- .join("\n")}
100
+ ${importWasm(wasmFiles, { isInCloudflare })}
105
101
  ${entryFiles.map((file) => `require("${file}");`).join("\n")}
106
102
  ${contents}
107
103
  `;
@@ -153,3 +149,17 @@ ${contents}
153
149
  },
154
150
  };
155
151
  }
152
+ function importWasm(files, { isInCloudflare }) {
153
+ return files
154
+ .map(({ name }) => {
155
+ if (isInCloudflare) {
156
+ // As `.next/server/src/middleware.js` references the name,
157
+ // using `import ${name} from '...'` would cause ESBuild to rename the import.
158
+ // We use `globalThis.${name}` to make sure `middleware.js` reference name will match.
159
+ return `import __onw_${name}__ from './wasm/${name}.wasm'
160
+ globalThis.${name} = __onw_${name}__`;
161
+ }
162
+ return `const ${name} = readFileSync(path.join(__dirname,'/wasm/${name}.wasm'));`;
163
+ })
164
+ .join("\n");
165
+ }
@@ -152,6 +152,9 @@ export interface PrerenderManifest {
152
152
  dataRouteRegex: string;
153
153
  };
154
154
  };
155
+ preview: {
156
+ previewModeId: string;
157
+ };
155
158
  }
156
159
  export type Options = {
157
160
  internalEvent: InternalEvent;
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "access": "public"
4
4
  },
5
5
  "name": "@opennextjs/aws",
6
- "version": "3.5.3",
6
+ "version": "3.5.4",
7
7
  "bin": {
8
8
  "open-next": "./dist/index.js"
9
9
  },