From fa57895777101744ad03b2a05b3f2ec972aceb86 Mon Sep 17 00:00:00 2001 From: Manuel Antunes <57446204+Manuel-Antunes@users.noreply.github.com> Date: Thu, 1 Feb 2024 00:12:27 +0000 Subject: [PATCH 1/5] feat: add cache support for next@14.1.0 --- .../adapters/plugins/14.1/serverHandler.ts | 25 ++++++++++++++++ .../src/adapters/plugins/14.1/util.ts | 30 +++++++++++++++++++ packages/open-next/src/build.ts | 20 +++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 packages/open-next/src/adapters/plugins/14.1/serverHandler.ts create mode 100644 packages/open-next/src/adapters/plugins/14.1/util.ts diff --git a/packages/open-next/src/adapters/plugins/14.1/serverHandler.ts b/packages/open-next/src/adapters/plugins/14.1/serverHandler.ts new file mode 100644 index 00000000..dc7f9864 --- /dev/null +++ b/packages/open-next/src/adapters/plugins/14.1/serverHandler.ts @@ -0,0 +1,25 @@ +/*eslint-disable simple-import-sort/imports */ +import type { Options, PluginHandler } from "../../types/next-types.js"; +import type { IncomingMessage } from "../../http/request.js"; +import type { ServerlessResponse } from "../../http/response.js"; +//#override imports +//@ts-ignore +import { requestHandler } from "./util.js"; +//@ts-ignore +import { proxyRequest } from "./routing/util.js"; +//#endOverride + +//#override handler +export const handler: PluginHandler = async ( + req: IncomingMessage, + res: ServerlessResponse, + options: Options, +) => { + if (options.isExternalRewrite) { + return proxyRequest(req, res); + } else { + // Next Server + return requestHandler(req, res); + } +}; +//#endOverride diff --git a/packages/open-next/src/adapters/plugins/14.1/util.ts b/packages/open-next/src/adapters/plugins/14.1/util.ts new file mode 100644 index 00000000..e422240f --- /dev/null +++ b/packages/open-next/src/adapters/plugins/14.1/util.ts @@ -0,0 +1,30 @@ +import { NextConfig } from "../../config"; +import { debug } from "../../logger.js"; + +//#override requestHandler +// @ts-ignore +export const requestHandler = new NextServer.default({ + conf: { + ...NextConfig, + // Next.js compression should be disabled because of a bug in the bundled + // `compression` package — https://github.com/vercel/next.js/issues/11669 + compress: false, + // By default, Next.js uses local disk to store ISR cache. We will use + // our own cache handler to store the cache on S3. + cacheHandler: `${process.env.LAMBDA_TASK_ROOT}/cache.cjs`, + experimental: { + ...NextConfig.experimental, + // This uses the request.headers.host as the URL + // https://github.com/vercel/next.js/blob/canary/packages/next/src/server/next-server.ts#L1749-L1754 + trustHostHeader: true, + }, + }, + customServer: false, + dev: false, + dir: __dirname, +}).getRequestHandler(); +//#endOverride + +//#override requireHooks +debug("No need to override require hooks with next 13.4.20+"); +//#endOverride \ No newline at end of file diff --git a/packages/open-next/src/build.ts b/packages/open-next/src/build.ts index ccfbfe9d..27de1fe6 100755 --- a/packages/open-next/src/build.ts +++ b/packages/open-next/src/build.ts @@ -692,6 +692,26 @@ async function createServerBundle(monorepoRoot: string, streaming = false) { ]; } + if(compareSemver(options.nextVersion, "14.1.0") >= 0){ + plugins = [ + openNextPlugin({ + name: "opennext-14.1-serverHandler", + target: /plugins\/serverHandler\.js/g, + replacements: ["./14.1/serverHandler.js"], + }), + openNextPlugin({ + name: "opennext-14.1-util", + target: /plugins\/util\.js/g, + replacements: ["./14.1/util.js", "./util.replacement.js"], + }), + openNextPlugin({ + name: "opennext-14.1-default", + target: /plugins\/routing\/default\.js/g, + replacements: ["./default.replacement.js"], + }), + ]; + } + if (streaming) { const streamingPlugin = openNextPlugin({ name: "opennext-streaming", From 671dd86dc77e5f908bee4cdeb2d682beb00d7216 Mon Sep 17 00:00:00 2001 From: Manuel Antunes <57446204+Manuel-Antunes@users.noreply.github.com> Date: Thu, 1 Feb 2024 00:14:27 +0000 Subject: [PATCH 2/5] fix: lint files --- packages/open-next/src/adapters/plugins/14.1/util.ts | 2 +- packages/open-next/src/build.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/open-next/src/adapters/plugins/14.1/util.ts b/packages/open-next/src/adapters/plugins/14.1/util.ts index e422240f..7437f2a3 100644 --- a/packages/open-next/src/adapters/plugins/14.1/util.ts +++ b/packages/open-next/src/adapters/plugins/14.1/util.ts @@ -27,4 +27,4 @@ export const requestHandler = new NextServer.default({ //#override requireHooks debug("No need to override require hooks with next 13.4.20+"); -//#endOverride \ No newline at end of file +//#endOverride diff --git a/packages/open-next/src/build.ts b/packages/open-next/src/build.ts index 27de1fe6..4e45a15c 100755 --- a/packages/open-next/src/build.ts +++ b/packages/open-next/src/build.ts @@ -692,7 +692,7 @@ async function createServerBundle(monorepoRoot: string, streaming = false) { ]; } - if(compareSemver(options.nextVersion, "14.1.0") >= 0){ + if (compareSemver(options.nextVersion, "14.1.0") >= 0) { plugins = [ openNextPlugin({ name: "opennext-14.1-serverHandler", From d39e137b173ae1216891bb8ec82ce0cde8ab99a0 Mon Sep 17 00:00:00 2001 From: Manuel Antunes <57446204+Manuel-Antunes@users.noreply.github.com> Date: Thu, 1 Feb 2024 13:43:42 +0000 Subject: [PATCH 3/5] chore: apply the proposed changes --- .../adapters/plugins/14.1/serverHandler.ts | 25 ------------------ .../src/adapters/plugins/14.1/util.ts | 1 + packages/open-next/src/build.ts | 26 ++++--------------- 3 files changed, 6 insertions(+), 46 deletions(-) delete mode 100644 packages/open-next/src/adapters/plugins/14.1/serverHandler.ts diff --git a/packages/open-next/src/adapters/plugins/14.1/serverHandler.ts b/packages/open-next/src/adapters/plugins/14.1/serverHandler.ts deleted file mode 100644 index dc7f9864..00000000 --- a/packages/open-next/src/adapters/plugins/14.1/serverHandler.ts +++ /dev/null @@ -1,25 +0,0 @@ -/*eslint-disable simple-import-sort/imports */ -import type { Options, PluginHandler } from "../../types/next-types.js"; -import type { IncomingMessage } from "../../http/request.js"; -import type { ServerlessResponse } from "../../http/response.js"; -//#override imports -//@ts-ignore -import { requestHandler } from "./util.js"; -//@ts-ignore -import { proxyRequest } from "./routing/util.js"; -//#endOverride - -//#override handler -export const handler: PluginHandler = async ( - req: IncomingMessage, - res: ServerlessResponse, - options: Options, -) => { - if (options.isExternalRewrite) { - return proxyRequest(req, res); - } else { - // Next Server - return requestHandler(req, res); - } -}; -//#endOverride diff --git a/packages/open-next/src/adapters/plugins/14.1/util.ts b/packages/open-next/src/adapters/plugins/14.1/util.ts index 7437f2a3..684c1aab 100644 --- a/packages/open-next/src/adapters/plugins/14.1/util.ts +++ b/packages/open-next/src/adapters/plugins/14.1/util.ts @@ -12,6 +12,7 @@ export const requestHandler = new NextServer.default({ // By default, Next.js uses local disk to store ISR cache. We will use // our own cache handler to store the cache on S3. cacheHandler: `${process.env.LAMBDA_TASK_ROOT}/cache.cjs`, + cacheMaxMemorySize: 0, experimental: { ...NextConfig.experimental, // This uses the request.headers.host as the URL diff --git a/packages/open-next/src/build.ts b/packages/open-next/src/build.ts index 4e45a15c..d8a39518 100755 --- a/packages/open-next/src/build.ts +++ b/packages/open-next/src/build.ts @@ -673,6 +673,10 @@ async function createServerBundle(monorepoRoot: string, streaming = false) { : undefined; if (compareSemver(options.nextVersion, "13.5.1") >= 0) { + const utilReplacement = + compareSemver(options.nextVersion, "14.1.0") >= 0 + ? "./14.1.util.js" + : "./13.5/util.js"; plugins = [ openNextPlugin({ name: "opennext-13.5-serverHandler", @@ -682,7 +686,7 @@ async function createServerBundle(monorepoRoot: string, streaming = false) { openNextPlugin({ name: "opennext-13.5-util", target: /plugins\/util\.js/g, - replacements: ["./13.5/util.js", "./util.replacement.js"], + replacements: [utilReplacement, "./util.replacement.js"], }), openNextPlugin({ name: "opennext-13.5-default", @@ -692,26 +696,6 @@ async function createServerBundle(monorepoRoot: string, streaming = false) { ]; } - if (compareSemver(options.nextVersion, "14.1.0") >= 0) { - plugins = [ - openNextPlugin({ - name: "opennext-14.1-serverHandler", - target: /plugins\/serverHandler\.js/g, - replacements: ["./14.1/serverHandler.js"], - }), - openNextPlugin({ - name: "opennext-14.1-util", - target: /plugins\/util\.js/g, - replacements: ["./14.1/util.js", "./util.replacement.js"], - }), - openNextPlugin({ - name: "opennext-14.1-default", - target: /plugins\/routing\/default\.js/g, - replacements: ["./default.replacement.js"], - }), - ]; - } - if (streaming) { const streamingPlugin = openNextPlugin({ name: "opennext-streaming", From 980dc5fdb2c99548c5a1879be7c7e773448be7ca Mon Sep 17 00:00:00 2001 From: conico974 Date: Thu, 1 Feb 2024 23:37:23 +0100 Subject: [PATCH 4/5] Fix typo --- packages/open-next/src/build.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/open-next/src/build.ts b/packages/open-next/src/build.ts index d8a39518..7a8bb164 100755 --- a/packages/open-next/src/build.ts +++ b/packages/open-next/src/build.ts @@ -673,10 +673,8 @@ async function createServerBundle(monorepoRoot: string, streaming = false) { : undefined; if (compareSemver(options.nextVersion, "13.5.1") >= 0) { - const utilReplacement = - compareSemver(options.nextVersion, "14.1.0") >= 0 - ? "./14.1.util.js" - : "./13.5/util.js"; + const isAfter141 = compareSemver(options.nextVersion, "14.1.0") >= 0; + const utilReplacement = isAfter141 ? "./14.1/util.js" : "./13.5/util.js"; plugins = [ openNextPlugin({ name: "opennext-13.5-serverHandler", @@ -686,7 +684,10 @@ async function createServerBundle(monorepoRoot: string, streaming = false) { openNextPlugin({ name: "opennext-13.5-util", target: /plugins\/util\.js/g, - replacements: [utilReplacement, "./util.replacement.js"], + replacements: [ + utilReplacement, + ...(isAfter141 ? [] : ["./util.replacement.js"]), + ], }), openNextPlugin({ name: "opennext-13.5-default", From 4d4813ddb629618429b7956ade105724374b4cbe Mon Sep 17 00:00:00 2001 From: conico974 Date: Thu, 1 Feb 2024 23:38:43 +0100 Subject: [PATCH 5/5] changeset --- .changeset/selfish-suits-exercise.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/selfish-suits-exercise.md diff --git a/.changeset/selfish-suits-exercise.md b/.changeset/selfish-suits-exercise.md new file mode 100644 index 00000000..a152231f --- /dev/null +++ b/.changeset/selfish-suits-exercise.md @@ -0,0 +1,5 @@ +--- +"open-next": patch +--- + +Fix Cache Support for Next@14.1.0