10000 [BLOCKED] Drop the patch for NextServer#getMiddlewareManifest by vicb · Pull Request #712 · opennextjs/opennextjs-cloudflare · GitHub
[go: up one dir, main page]

Skip to content

[BLOCKED] Drop the patch for NextServer#getMiddlewareManifest #712

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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/wild-readers-remain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opennextjs/cloudflare": patch
---

Drop the patch for NextServer#getMiddlewareManifest
2 changes: 1 addition & 1 deletion packages/cloudflare/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"homepage": "https://github.com/opennextjs/opennextjs-cloudflare",
"dependencies": {
"@dotenvx/dotenvx": "catalog:",
"@opennextjs/aws": "3.6.5",
"@opennextjs/aws": "https://pkg.pr.new/@opennextjs/aws@896",
"enquirer": "^2.4.1",
"glob": "catalog:",
"ts-tqdm": "^0.8.6"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ async function generateBundle(
awsPatches.patchNextServer,
awsPatches.patchEnvVars,
awsPatches.patchBackgroundRevalidation,
awsPatches.patchDropBabel,
// Cloudflare specific patches
patchResRevalidate,
patchUseCacheIO,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { patchCode } from "@opennextjs/aws/build/patch/astCodePatcher.js";
import { describe, expect, test } from "vitest";

import {
buildIdRule,
createCacheHandlerRule,
createComposableCacheHandlersRule,
createMiddlewareManifestRule,
} from "./next-server.js";
import { buildIdRule, createCacheHandlerRule, createComposableCacheHandlersRule } from "./next-server.js";

describe("Next Server", () => {
const nextServerCode = `
Expand Down Expand Up @@ -172,91 +167,6 @@ class NextNodeServer extends _baseserver.default {
`);
});

test("middleware manifest", () => {
expect(patchCode(nextServerCode, createMiddlewareManifestRule("manifest"))).toMatchInlineSnapshot(`
"class NextNodeServer extends _baseserver.default {
constructor(options){
// Initialize super class
super(options);
this.handleNextImageRequest = async (req, res, parsedUrl) => { /* ... */ };
}
async handleUpgrade() {
// The web server does not support web sockets, it's only used for HMR in
// development.
}
loadEnvConfig({ dev, forceReload, silent }) {
(0, _env.loadEnvConfig)(this.dir, dev, silent ? {
info: ()=>{},
error: ()=>{}
} : _log, forceReload);
}
async hasPage(pathname) {
var _this_nextConfig_i18n;
return !!(0, _require.getMaybePagePath)(pathname, this.distDir, (_this_nextConfig_i18n = this.nextConfig.i18n) == null ? void 0 : _this_nextConfig_i18n.locales, this.enabledDirectories.app);
}
getBuildId() {
const buildIdFile = (0, _path.join)(this.distDir, _constants.BUILD_ID_FILE);
try {
return _fs.default.readFileSync(buildIdFile, "utf8").trim();
} catch (err) {
if (err.code === "ENOENT") {
throw new Error(\`Could not find a production build in the '\${this.distDir}' directory. Try building your app with 'next build' before starting the production server. https://nextjs.org/docs/messages/production-start-no-build-id\`);
}
throw err;
}
}
getMiddlewareManifest() {
return "manifest";
}
async loadCustomCacheHandlers() {
const { cacheHandlers } = this.nextConfig.experimental;
if (!cacheHandlers) return;
// If we've already initialized the cache handlers interface, don't do it
// again.
if (!(0, _handlers.initializeCacheHandlers)()) return;
for (const [kind, handler] of Object.entries(cacheHandlers)){
if (!handler) continue;
(0, _handlers.setCacheHandler)(kind, (0, _interopdefault.interopDefault)(await dynamicImportEsmDefault((0, _formatdynamicimportpath.formatDynamicImportPath)(this.distDir, handler))));
}
}
async getIncrementalCache({ requestHeaders, requestProtocol }) {
const dev = !!this.renderOpts.dev;
let CacheHandler;
const { cacheHandler } = this.nextConfig;
if (cacheHandler) {
CacheHandler = (0, _interopdefault.interopDefault)(await dynamicImportEsmDefault((0, _formatdynamicimportpath.formatDynamicImportPath)(this.distDir, cacheHandler)));
}
await this.loadCustomCacheHandlers();
// incremental-cache is request specific
// although can have shared caches in module scope
// per-cache handler
return new _incrementalcache.IncrementalCache({
fs: this.getCacheFilesystem(),
dev,
requestHeaders,
requestProtocol,
allowedRevalidateHeaderKeys: this.nextConfig.experimental.allowedRevalidateHeaderKeys,
minimalMode: this.minimalMode,
serverDistDir: this.serverDistDir,
fetchCacheKeyPrefix: this.nextConfig.experimental.fetchCacheKeyPrefix,
maxMemoryCacheSize: this.nextConfig.cacheMaxMemorySize,
flushToDisk: !this.minimalMode && this.nextConfig.experimental.isrFlushToDisk,
getPrerenderManifest: ()=>this.getPrerenderManifest(),
CurCacheHandler: CacheHandler
});
}
getEnabledDirectories(dev) {
const dir = dev ? this.dir : this.serverDistDir;
return {
app: (0, _findpagesdir.findDir)(dir, "app") ? true : false,
pages: (0, _findpagesdir.findDir)(dir, "pages") ? true : false
};
}
// ...
}"
`);
});

test("cache handler", () => {
expect(patchCode(nextServerCode, createCacheHandlerRule("manifest"))).toMatchInlineSnapshot(`
"class NextNodeServer extends _baseserver.default {
Expand Down
28 changes: 0 additions & 28 deletions packages/cloudflare/src/cli/build/patches/plugins/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
* Note: we will probably need to revisit the patches when the Next adapter API lands
*
* - Inline `getBuildId` as it relies on `readFileSync` that is not supported by workerd
* - Inline the middleware manifest
* - Override the cache and composable cache handlers
*/

import { existsSync, readFileSync } from "node:fs";
import path from "node:path";

import { type BuildOptions, getPackagePath } from "@opennextjs/aws/build/helper.js";
Expand All @@ -31,19 +29,6 @@ export function patchNextServer(updater: ContentUpdater, buildOpts: BuildOptions

contents = patchCode(contents, buildIdRule);

const manifestPath = path.join(
outputDir,
"server-functions/default",
getPackagePath(buildOpts),
".next/server/middleware-manifest.json"
);

const manifest = existsSync(manifestPath)
? JSON.parse(await readFileSync(manifestPath, "utf-8"))
: {};

contents = patchCode(contents, createMiddlewareManifestRule(manifest));

const outputPath = path.join(outputDir, "server-functions/default");
const cacheHandler = path.join(outputPath, getPackagePath(buildOpts), "cache.cjs");
contents = patchCode(contents, createCacheHandlerRule(cacheHandler));
Expand Down Expand Up @@ -72,19 +57,6 @@ fix: |-
}
`;

export function createMiddlewareManifestRule(manifest: unknown) {
return `
rule:
pattern:
selector: method_definition
context: "class { getMiddlewareManifest($$$PARAMS) { $$$_ } }"
fix: |-
getMiddlewareManifest($$$PARAMS) {
return ${JSON.stringify(manifest)};
}
`;
}

/**
* The cache handler used by Next.js is normally defined in the config file as a path. At runtime,
* Next.js would then do a dynamic require on a transformed version of the path to retrieve the
Expand Down
11 changes: 6 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0