Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Cache Support for [email protected] #356

Merged
merged 5 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
25 changes: 25 additions & 0 deletions packages/open-next/src/adapters/plugins/14.1/serverHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*eslint-disable simple-import-sort/imports */
Copy link
Contributor

Choose a reason for hiding this comment

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

You don't need to create this file, just use the 13.5/serverHandler.js

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 thought that the module resolution could find an error because the file isn't in the same directory so the relative path was supposed to be different

Copy link
Contributor

Choose a reason for hiding this comment

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

That's not how the plugin works https://open-next.js.org/inner_workings/plugin
Long story short, the only things that happens is that everything between //#override and //#endOverride is replaced in the files you provide

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
30 changes: 30 additions & 0 deletions packages/open-next/src/adapters/plugins/14.1/util.ts
Original file line number Diff line number Diff line change
@@ -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`,
Manuel-Antunes marked this conversation as resolved.
Show resolved Hide resolved
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
20 changes: 20 additions & 0 deletions packages/open-next/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Copy link
Contributor

Choose a reason for hiding this comment

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

The only things that change between 13.5.1+ and 14.1 is the ./14.1/utils.js
Could you refactor this to something like that

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",
        target: /plugins\/serverHandler\.js/g,
        replacements: ["./13.5/serverHandler.js"],
      }),
      openNextPlugin({
        name: "opennext-13.5-util",
        target: /plugins\/util\.js/g,
        replacements: [utilReplacement, "./util.replacement.js"],
      }),
      openNextPlugin({
        name: "opennext-13.5-default",
        target: /plugins\/routing\/default\.js/g,
        replacements: ["./default.replacement.js"],
      }),
    ];
  }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

LGTM!

}),
openNextPlugin({
name: "opennext-14.1-default",
target: /plugins\/routing\/default\.js/g,
replacements: ["./default.replacement.js"],
}),
];
}

if (streaming) {
const streamingPlugin = openNextPlugin({
name: "opennext-streaming",
Expand Down