After Upgrading from Next.js 14.0 to 14.2 NetlifyCacheHandler.get fails

Hi,

I deploy my Next.js 14.x app from GitHub. According to the build log (and my understanding) I only have static rendered routes. During the build I access the file system using Nodes fs module to generate static routes with content from my repository. That works both locally on my machine and on Netlify.

Upgrading to Next.js (from 14.0 to latest 14.2.3 version), my app now failes at runtime (" This function has crashed" from the Next.js Server Handler function) .

It seems that after the upgrade, the NetlifyCacheHandler executes the generateStaticParams in my page.tsx files. That generateStaticParams function fails because in the deployed version the folder with the static content (content) is not available at runtime.

Apr 26, 01:12:57 PM: cc243690 DEBUG  [NetlifyCacheHandler.get]: /index
Apr 26, 01:12:57 PM: cc243690 ERROR  Unhandled Promise Rejection 	{"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"Error: ENOENT: no such file or directory, scandir 'content'","reason":{"errorType":"Error","errorMessage":"ENOENT: no such file or directory, scandir 'content'","code":"ENOENT","errno":-2,"syscall":"scandir","path":"content","stack":["Error: ENOENT: no such file or directory, scandir 'content'"]},"promise":{},"stack":["Runtime.UnhandledPromiseRejection: Error: ENOENT: no such file or directory, scandir 'content'","    at process.<anonymous> (file:///var/runtime/index.mjs:1276:17)","    at process.emit (node:events:529:35)","    at emit (node:internal/process/promises:149:20)","    at processPromiseRejections (node:internal/process/promises:283:27)","    at process.processTicksAndRejections (node:internal/process/task_queues:96:32)"]}

Is there any way to disable the execution of NetlifyCacheHandler.get or any other way to make my application’s static routes work with Next.js 14.2? Thanks!

Failing deployment using Next.js 14.2.3: https://663caf1ea89dd900080096b4--nilshartmann-net.netlify.app/
Same source code, but with Next.js 14.0: https://663cb09a4c823b00089a34d1--nilshartmann-net.netlify.app/

Thank you for reporting this. The devs are looking into it. Meanwhile, could you see if adding the following to your netlify.toml helps:

[functions]
  included_files = ["path-to-content-folder"]

Docs on that: How to Include Files in Netlify Serverless Functions

Thanks for your reponse!

Adding the netlify.toml file makes the application work with Next.js 14.2 :partying_face:

(One minor thing: I had to write included_files = ["path-to-content-folder/**"]. Without /** the path-to-content-folder folder still hadn’t been added.)

But anyway, I’m not sure, if static routes should be rebuild at all automatically?

Currently, the Next.js Runtime v5 deletes all your Next.js-built HTML files from the deploy and uploads them to Netlify Blobs. Then, during the request time, the request is sent to the server handler where the HTML is downloaded from Blobs, processed by Next.js (checked for cache status) and a response is served.

I believe that’s what’s causing an issue in your case. On a surface level, this doesn’t appear to be related to the Next.js 14.2 upgrade, but we’ll keep checking.

I my case, I have a module that contains some code on top-level, that reads the file system:

// get-content.ts

const filesPromise = fs.readdir("content").then(...);

This module is imported from my app, so it is executed during the build process. But somehow the Next.js runtime than also require/imports it at runtime. Imho that should not be the case. Imagine there is a database connection and I only have access to the db at compile time. In that case I wouldn’t be able to run my app with the Next.js runtime. In my understanding, Next.js static routes are excactly for this case: execute the code only once at compile and not at runtime time?

There are no “truly static” routes in Next.js. Even those static files are revalidateable by Next.js so we have to let Next.js handle those assets and not serve those directly as static files. We have no means to know if a route would be revalidated in the future or not, and since Next.js allows revalidating static files, we have to follow that standard and allow revalidating static files on Netlify as well.

If you want a truly static outputI believe you’d have to use: Deploying: Static Exports | Next.js (nextjs.org)

True that it is possible to revalidate static routes in Next, but in my case I didn’t manually revalidate the route and thus expected the routes not to be re-rendered at runtime.