From 1ebb2816a5d0334fc4b09f385090ac5f82cb9b09 Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Wed, 12 Mar 2025 17:23:52 +0100 Subject: [PATCH 1/2] fix: Pages router API routes with Next 14 --- examples/playground14/e2e/base.spec.ts | 5 ++ examples/playground14/pages/api/pages.ts | 9 ++ packages/cloudflare/package.json | 2 +- .../build/patches/plugins/dynamic-requires.ts | 7 +- pnpm-lock.yaml | 82 +++++++++---------- 5 files changed, 59 insertions(+), 46 deletions(-) create mode 100644 examples/playground14/pages/api/pages.ts diff --git a/examples/playground14/e2e/base.spec.ts b/examples/playground14/e2e/base.spec.ts index e36987d05..1513e801c 100644 --- a/examples/playground14/e2e/base.spec.ts +++ b/examples/playground14/e2e/base.spec.ts @@ -52,6 +52,11 @@ test.describe("playground/base", () => { await expect(res.json()).resolves.toEqual({ nextUrl: expectedURL, url: expectedURL }); }); + test("Pages router API routes", async ({ page }) => { + const res = await page.request.get("/api/pages"); + expect(await res.json()).toEqual({ hello: "world" }); + }); + test("generates an og image successfully", async ({ page }) => { const res = await page.request.get("/og"); expect(res.status()).toEqual(200); diff --git a/examples/playground14/pages/api/pages.ts b/examples/playground14/pages/api/pages.ts new file mode 100644 index 000000000..32dfa5a3d --- /dev/null +++ b/examples/playground14/pages/api/pages.ts @@ -0,0 +1,9 @@ +import type { NextApiRequest, NextApiResponse } from "next"; + +type Data = { + hello: string; +}; + +export default function handler(req: NextApiRequest, res: NextApiResponse) { + res.status(200).json({ hello: "world" }); +} diff --git a/packages/cloudflare/package.json b/packages/cloudflare/package.json index 520e57b11..1c3fe156f 100644 --- a/packages/cloudflare/package.json +++ b/packages/cloudflare/package.json @@ -71,7 +71,7 @@ "vitest": "catalog:" }, "dependencies": { - "@ast-grep/napi": "^0.34.1", + "@ast-grep/napi": "^0.36.1", "@dotenvx/dotenvx": "catalog:", "@opennextjs/aws": "https://pkg.pr.new/@opennextjs/aws@756", "enquirer": "^2.4.1", diff --git a/packages/cloudflare/src/cli/build/patches/plugins/dynamic-requires.ts b/packages/cloudflare/src/cli/build/patches/plugins/dynamic-requires.ts index 436ee54b8..a5489d3b9 100644 --- a/packages/cloudflare/src/cli/build/patches/plugins/dynamic-requires.ts +++ b/packages/cloudflare/src/cli/build/patches/plugins/dynamic-requires.ts @@ -47,10 +47,9 @@ export function inlineDynamicRequires(updater: ContentUpdater, buildOpts: BuildO updater.updateContent( "inline-node-module-loader", { - filter: getCrossPlatformPathRegex( - String.raw`/next/dist/server/lib/module-loader/node-module-loader\.js$`, - { escape: false } - ), + filter: getCrossPlatformPathRegex(String.raw`/module-loader/node-module-loader\.js$`, { + escape: false, + }), contentFilter: /class NodeModuleLoader {/, }, async ({ contents }) => patchCode(contents, await getNodeModuleLoaderRule(buildOpts)) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index baceed6d4..3099e27cd 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -892,8 +892,8 @@ importers: packages/cloudflare: dependencies: '@ast-grep/napi': - specifier: ^0.34.1 - version: 0.34.1 + specifier: ^0.36.1 + version: 0.36.1 '@dotenvx/dotenvx': specifier: 'catalog:' version: 1.31.0 @@ -971,62 +971,62 @@ packages: resolution: {integrity: sha512-UrcABB+4bUrFABwbluTIBErXwvbsU/V7TZWfmbgJfbkwiBuziS9gxdODUyuiecfdGQ85jglMW6juS3+z5TsKLw==} engines: {node: '>=10'} - '@ast-grep/napi-darwin-arm64@0.34.1': - resolution: {integrity: sha512-SzXXx2ya6xDKBaHZv20P9Qza5FaWE6lMBu00yegJVYRs7+0feBJ+10dvbv9fm+7RJ91Tw0V0G92Sgdg9lysBwA==} + '@ast-grep/napi-darwin-arm64@0.36.1': + resolution: {integrity: sha512-hLceB0+nTMzmp+85ffJdmwUYcH9n+9ca/dj4nBxXjWBEPhcsA4FOlxCn/dltxhIsdb9eVL8ffATyu7yO96Xjcw==} engines: {node: '>= 10'} cpu: [arm64] os: [darwin] - '@ast-grep/napi-darwin-x64@0.34.1': - resolution: {integrity: sha512-LIeTaB0IgNGyyU5sMW2cUHjWWW/sNjEqseMmpHLft7+LEXK7ojr4NDUN33cKtrRgEfwpvVXjXh5sgLqm343ecg==} + '@ast-grep/napi-darwin-x64@0.36.1': + resolution: {integrity: sha512-bt5FZNIRE/FmwD29t1LMMsbWxxqgAGabCOSWxbFKBPWqYPW48sG0FfEP2x3P6vIMmNrNNaEy0YwiwaZVR8M0dQ==} engines: {node: '>= 10'} cpu: [x64] os: [darwin] - '@ast-grep/napi-linux-arm64-gnu@0.34.1': - resolution: {integrity: sha512-zi/JKIYDsucSfMHhb2W33AVw8Q6bF9MBV6FrYF98UWpJJoWXUPzPqVM09NANyWM0lypGiWuHk//JCwHndeCiSA==} + '@ast-grep/napi-linux-arm64-gnu@0.36.1': + resolution: {integrity: sha512-6xAs1AGqll9cZdinQIdEo8WA6dEgOSKGfk0HLmXO0R6Z9q/i17oRtLdud+iVNUVBjyFxrOrTYUGxzwwbfrh2Mg==} engines: {node: '>= 10'} cpu: [arm64] os: [linux] - '@ast-grep/napi-linux-arm64-musl@0.34.1': - resolution: {integrity: sha512-IXdqwTbkdqHrcuQb448Qzd82QdTqVFe/f0sSkFYQTic8P2qNzmiHsVnxgEFsQPPbe09BVAoZ885j3OnaNfcDYA==} + '@ast-grep/napi-linux-arm64-musl@0.36.1': + resolution: {integrity: sha512-qBg0id6C138OkD68zuaa37hrNE01NKJVaV+VZ+7lYIdM7ovwq4D3cgvKsxz1iWUrXsQIX+RPQfU4MoYJdRKYRQ==} engines: {node: '>= 10'} cpu: [arm64] os: [linux] - '@ast-grep/napi-linux-x64-gnu@0.34.1': - resolution: {integrity: sha512-on4LyIeN/zN7SIh8zr5v+NTzVu3kXm2mG28ib1Qe9GVcf35dz52ckf7bilulayKSa2MHZWAXMjuc6NYMiNEw+w==} + '@ast-grep/napi-linux-x64-gnu@0.36.1': + resolution: {integrity: sha512-DJnOF0nETsFCPlwRSPwiddja/Pe6yyQmgvQMZRWR3jBcqNeytL6MwVBs7bLyiGPeRNgB/Fk/V4lfVjPV8OYi6w==} engines: {node: '>= 10'} cpu: [x64] os: [linux] - '@ast-grep/napi-linux-x64-musl@0.34.1': - resolution: {integrity: sha512-l1R5L9LOp0jTPjs8C+LUndZOA8cRw7PFlvoVxVbi2jCfcns00dqatSYc4yA/ke6ng2K0LSxjoV/jS8tefve0sA==} + '@ast-grep/napi-linux-x64-musl@0.36.1': + resolution: {integrity: sha512-vXrqWq9KZssTrJa47mDv8uPk7+ZLjQZScQjpbzB/icYj+VdhDAMxRC2ZrCTaSP+FQzibtZQdlEe3lGm4DGvuKA==} engines: {node: '>= 10'} cpu: [x64] os: [linux] - '@ast-grep/napi-win32-arm64-msvc@0.34.1': - resolution: {integrity: sha512-eVsdMtnY7jmN2xQjYY9gaqIxRHA44+QYivlP1uLbg8w3P4YlZWTFgOJ7aa357Hg/257mjeQCpodCkr0lGRsSYQ==} + '@ast-grep/napi-win32-arm64-msvc@0.36.1': + resolution: {integrity: sha512-y02toJitm47Zv6zeCZnvzU/YyACfim8xcZx7+V9dJr5H8DSd6bJhnCIEhyPqsDJBhgh0i+saRmFr12rb6Fmjtw==} engines: {node: '>= 10'} cpu: [arm64] os: [win32] - '@ast-grep/napi-win32-ia32-msvc@0.34.1': - resolution: {integrity: sha512-jDW6jfoxEeYMM8hrPSiOz4iUIN+O7VTTlqU5TWDdlFX8HJYLF4SrNFEZl+Gz5B0Ph5LTjKPZFk6sd+DC1zt9+A==} + '@ast-grep/napi-win32-ia32-msvc@0.36.1': + resolution: {integrity: sha512-I4oVIFX9dhzEI3fz7GYlj1Khw8w6Kk+2r8cuyqI9Z+U+FUZZFHr3ZWyXhJdrsfuJ9AmUeJLlydu6zAyCUlEvow==} engines: {node: '>= 10'} cpu: [ia32] os: [win32] - '@ast-grep/napi-win32-x64-msvc@0.34.1': - resolution: {integrity: sha512-ttBFcTugjzEbYRPBo400NfPU+3ukBxeOxpeYLBZ1yIYqbgekPnD4WItiowsX5/hd0wHcX3T50FdthlJRmOXubQ==} + '@ast-grep/napi-win32-x64-msvc@0.36.1': + resolution: {integrity: sha512-ofuHLFdaVMsPoxJsl1cgIlrOUYoh3kF6fiSKSbCbL80PRCpJXo8J+EwxsF033hK7bwMy40FohG4guz5a4B2tYA==} engines: {node: '>= 10'} cpu: [x64] os: [win32] - '@ast-grep/napi@0.34.1': - resolution: {integrity: sha512-SZAXFjgSqr7TfdW+ZJZ5LMkC226vJCAHAU5VvaluG2maT/LdkaUN3RwV+emdcEXIrUOqKB98+WSobhqOsKYuNw==} + '@ast-grep/napi@0.36.1': + resolution: {integrity: sha512-Ptfwhmlh9Xc5PunM1/X9tcmudxTAOIXxsDdvkSM4h9z/o+OrqL6DYgXOyPxo3mWFln/+oHHIB65jtNQCoo6Niw==} engines: {node: '>= 10'} '@aws-crypto/crc32@5.2.0': @@ -9628,44 +9628,44 @@ snapshots: '@alloc/quick-lru@5.2.0': {} - '@ast-grep/napi-darwin-arm64@0.34.1': + '@ast-grep/napi-darwin-arm64@0.36.1': optional: true - '@ast-grep/napi-darwin-x64@0.34.1': + '@ast-grep/napi-darwin-x64@0.36.1': optional: true - '@ast-grep/napi-linux-arm64-gnu@0.34.1': + '@ast-grep/napi-linux-arm64-gnu@0.36.1': optional: true - '@ast-grep/napi-linux-arm64-musl@0.34.1': + '@ast-grep/napi-linux-arm64-musl@0.36.1': optional: true - '@ast-grep/napi-linux-x64-gnu@0.34.1': + '@ast-grep/napi-linux-x64-gnu@0.36.1': optional: true - '@ast-grep/napi-linux-x64-musl@0.34.1': + '@ast-grep/napi-linux-x64-musl@0.36.1': optional: true - '@ast-grep/napi-win32-arm64-msvc@0.34.1': + '@ast-grep/napi-win32-arm64-msvc@0.36.1': optional: true - '@ast-grep/napi-win32-ia32-msvc@0.34.1': + '@ast-grep/napi-win32-ia32-msvc@0.36.1': optional: true - '@ast-grep/napi-win32-x64-msvc@0.34.1': + '@ast-grep/napi-win32-x64-msvc@0.36.1': optional: true - '@ast-grep/napi@0.34.1': + '@ast-grep/napi@0.36.1': optionalDependencies: - '@ast-grep/napi-darwin-arm64': 0.34.1 - '@ast-grep/napi-darwin-x64': 0.34.1 - '@ast-grep/napi-linux-arm64-gnu': 0.34.1 - '@ast-grep/napi-linux-arm64-musl': 0.34.1 - '@ast-grep/napi-linux-x64-gnu': 0.34.1 - '@ast-grep/napi-linux-x64-musl': 0.34.1 - '@ast-grep/napi-win32-arm64-msvc': 0.34.1 - '@ast-grep/napi-win32-ia32-msvc': 0.34.1 - '@ast-grep/napi-win32-x64-msvc': 0.34.1 + '@ast-grep/napi-darwin-arm64': 0.36.1 + '@ast-grep/napi-darwin-x64': 0.36.1 + '@ast-grep/napi-linux-arm64-gnu': 0.36.1 + '@ast-grep/napi-linux-arm64-musl': 0.36.1 + '@ast-grep/napi-linux-x64-gnu': 0.36.1 + '@ast-grep/napi-linux-x64-musl': 0.36.1 + '@ast-grep/napi-win32-arm64-msvc': 0.36.1 + '@ast-grep/napi-win32-ia32-msvc': 0.36.1 + '@ast-grep/napi-win32-x64-msvc': 0.36.1 '@aws-crypto/crc32@5.2.0': dependencies: From 451c41c10a170f1e04cb28e78cfd45ec577c690d Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Wed, 12 Mar 2025 17:42:12 +0100 Subject: [PATCH 2/2] fixup! add changeset --- .changeset/pretty-lamps-press.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/pretty-lamps-press.md diff --git a/.changeset/pretty-lamps-press.md b/.changeset/pretty-lamps-press.md new file mode 100644 index 000000000..02defda20 --- /dev/null +++ b/.changeset/pretty-lamps-press.md @@ -0,0 +1,5 @@ +--- +"@opennextjs/cloudflare": patch +--- + +fix: Pages router API routes with Next 14