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/whole-bats-sip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opennextjs/aws": patch
---

Fix for Next 16.1 config
14 changes: 8 additions & 6 deletions packages/open-next/src/build/copyTracedFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,12 +303,13 @@ File ${serverPath} does not exist
(fileOrDir) =>
!statSync(path.join(standaloneNextDir, fileOrDir)).isDirectory(),
)
.forEach((file) =>
.forEach((file) => {
copyFileSync(
path.join(standaloneNextDir, file),
path.join(outputNextDir, file),
),
);
);
tracedFiles.push(path.join(outputNextDir, file));
});

// We then need to copy all the files at the root of server

Expand All @@ -320,12 +321,13 @@ File ${serverPath} does not exist
!statSync(path.join(standaloneServerDir, fileOrDir)).isDirectory(),
)
.filter((file) => file !== "server.js")
.forEach((file) =>
.forEach((file) => {
copyFileSync(
path.join(standaloneServerDir, file),
path.join(path.join(outputNextDir, "server"), file),
),
);
);
tracedFiles.push(path.join(outputNextDir, "server", file));
});

// Copy patch file
copyPatchFile(path.join(outputDir, packagePath));
Expand Down
1 change: 1 addition & 0 deletions packages/open-next/src/build/createServerBundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ async function generateBundle(
patches.patchBackgroundRevalidation,
patches.patchUseCacheForISR,
patches.patchNodeEnvironment,
patches.patchNextConfig,
...additionalCodePatches,
]);

Expand Down
5 changes: 5 additions & 0 deletions packages/open-next/src/build/patch/codePatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ export type PatchCodeFn = (args: {
* Next.js manifests that are used by Next at runtime
*/
manifests: ReturnType<typeof getManifests>;
/**
* OpenNext build options
*/
buildOptions: buildHelper.BuildOptions;
}) => Promise<string>;

interface IndividualPatch {
Expand Down Expand Up @@ -168,6 +172,7 @@ export async function applyCodePatches(
filePath,
tracedFiles,
manifests,
buildOptions,
});
}
await fs.writeFile(filePath, patchedContent);
Expand Down
1 change: 1 addition & 0 deletions packages/open-next/src/build/patch/patches/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export {
export { patchFetchCacheSetMissingWaitUntil } from "./patchFetchCacheWaitUntil.js";
export { patchBackgroundRevalidation } from "./patchBackgroundRevalidation.js";
export { patchNodeEnvironment } from "./patchNodeEnvironment.js";
export { patchNextConfig } from "./patchNextConfig.js";
63 changes: 63 additions & 0 deletions packages/open-next/src/build/patch/patches/patchNextConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import fs from "node:fs";
import path from "node:path";
import { buildSync } from "esbuild";
import type { CodePatcher } from "../codePatcher";

export const patchNextConfig: CodePatcher = {
name: "patch-next-config",
patches: [
{
pathFilter: /required\-server\-files\.json$/,
versions: ">=16.1.0",
patchCode: async ({ code, buildOptions }) => {
// Find the next.config file with any supported extension
const tsExtensions = [".ts", ".mts", ".cts"];
const possibleExtensions = [...tsExtensions, ".mjs", ".js", ".cjs"];
let configPath: string | undefined;
let configExtension: string | undefined;

for (const ext of possibleExtensions) {
const testPath = path.join(buildOptions.appPath, `next.config${ext}`);
if (fs.existsSync(testPath)) {
configPath = testPath;
configExtension = ext;
break;
}
}

if (!configPath || !configExtension) {
throw new Error("Could not find next.config file");
}

let configToImport: string;

// Only compile if the extension is a TypeScript extension
if (tsExtensions.includes(configExtension)) {
buildSync({
entryPoints: [configPath],
outfile: path.join(buildOptions.tempBuildDir, "next.config.mjs"),
bundle: true,
format: "esm",
platform: "node",
});
configToImport = path.join(
buildOptions.tempBuildDir,
"next.config.mjs",
);
} else {
// For .js, .mjs, .cjs, use the file directly
configToImport = configPath;
}

// In next 16.1+ we need to add `skipTrailingSlashRedirect` manually because next removes it from the config
Copy link
Contributor

Choose a reason for hiding this comment

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

Q: do you think this should be reported as an issue on the Next repo?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I guess we could, but I think they did it on purpose. Looks like they use define for that now, and I checked, it is defined in the adapter output

const originalConfig = (await import(configToImport)).default;
const config = JSON.parse(code);
if (config.config.skipTrailingSlashRedirect === undefined) {
config.config.skipTrailingSlashRedirect =
originalConfig.skipTrailingSlashRedirect ?? false;
}
return JSON.stringify(config, null, 2);
},
},
],
};
Loading
Loading