cloudflare / next-on-pages

CLI to build and develop Next.js apps for Cloudflare Pages
https://www.npmjs.com/package/@cloudflare/next-on-pages
MIT License
1.28k stars 125 forks source link

[⚡ Feature]: Allow split deployments with latent nodejs runtime functions #845

Open nickbabcock opened 3 months ago

nickbabcock commented 3 months ago

Description

I'm currently running a next.js app on vercel. The next.js app contains a mix of edge and nodejs APIs, however the nodejs endpoints do not run on vercel (due to a body size limit), and are deployed on a separate server. Vercel will proxy to this server with the help of a conditional rewrite defined in the next.config.js

{
  rewrites: async () => ({
    beforeFiles: process.env.PROXY_NODE_URL
      ? [
          "/api/my-node-js-endpoint",
          // ...
        ].map((source) => ({
          source,
          destination: `${process.env.PROXY_NODE_URL}${source}`,
        }))
      : undefined,
    })
}

This allows the best of both worlds: the entire API can be defined in next.js and ran locally, and then divvied up for a production deployment.

I want to try out cloudflare's Next on pages but am receiving the following error:

⚡️ ERROR: Failed to produce a Cloudflare Pages build from the project. ⚡️ ⚡️ The following routes were not configured to run with the Edge Runtime: ⚡️ - /api/my-node-js-endpoint

As far as I can tell, the invalid function check is a heuristic that is eagerly throwing an error for functions that can never be invoked.

My request:

Additional Information

No response

Would you like to help?

nickbabcock commented 3 months ago

I tested a build that removed the check:

diff --git a/packages/next-on-pages/src/buildApplication/processVercelFunctions/invalidFunctions.ts b/packages/next-on-pages/src/buildApplication/processVercelFunctions/invalidFunctions.ts
index d6d92d7..b3a0ff9 100644
--- a/packages/next-on-pages/src/buildApplication/processVercelFunctions/invalidFunctions.ts
+++ b/packages/next-on-pages/src/buildApplication/processVercelFunctions/invalidFunctions.ts
@@ -45,13 +45,6 @@ export async function checkInvalidFunctions(

    await tryToFixInvalidFuncsWithValidIndexAlternative(collectedFunctions);
    await tryToFixInvalidDynamicISRFuncs(collectedFunctions);
-
-   if (collectedFunctions.invalidFunctions.size > 0) {
-       await printInvalidFunctionsErrorMessage(
-           collectedFunctions.invalidFunctions,
-       );
-       process.exit(1);
-   }
 }

 /**

And I was able to successfully able to port the application to cloudflage pages, and everything works as expected.

This confirms that the check is not exhaustive and can reject good deployments.

Not sure how this should be incorporated (I think I'm still leaning towards --force), but I'd love to not need to maintain a patched version of next-on-pages.