next.js: [Next 14] Next.js with custom cache handler fails to serve pre-rendered pages

Link to the code that reproduces this issue

https://github.com/better-salmon/custom-cache-404-for-ssg-pages

To Reproduce

  1. Build and start the app with npm run build && npm run start.
  2. Open http://localhost:3000/should-work in the browser.

Current vs. Expected behavior

Expected behavior:

The page is loaded with content and status code 200.

Actual behavior:

404 page is shown.

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.1.0: Mon Oct  9 21:28:31 PDT 2023; root:xnu-10002.41.9~6/RELEASE_ARM64_T8112
Binaries:
  Node: 20.9.0
  npm: 10.1.0
  Yarn: N/A
  pnpm: 8.10.0
Relevant Packages:
  next: 14.0.2-canary.13
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.1.3
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Not sure

Additional context

In the examples, there is a lack of reading static HTML from the disk, and the cache is empty; the app fails to serve such pages and shows 404 page.

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 7
  • Comments: 16 (9 by maintainers)

Most upvoted comments

Simply set fallback: true or fallback: 'blocking' to fix this issue. The problem here is that your cache is in memory, next sets the cache during build but it gets reset when you start the server.

This issue should be closed.

The cache is used for SSG/ISR pages in Pages and App Router, fetch call results, and unstable_cache callback calls in App Router. Server-side rendered pages, in general, are not affected by the cache in any way — such pages are marked as dynamic. Next.js doesn’t cache them.

@better-salmon I would love to brainstorm about a caching implementation for SSR pages. SSG is not a viable option for us, since our build step is isolated and can’t access backend. We do have SSR pages that could be cached, as they have a TTL of > 1h with infrequent changes to data. I never understood next’s approach in only allowing static pages at build time. (I know there is workarounds but they are not really feasible for a big app, like generating with mock data at build and revalidating post deploy)

The cache is used for SSG/ISR pages in Pages and App Router, fetch call results, and unstable_cache callback calls in App Router. Server-side rendered pages, in general, are not affected by the cache in any way — such pages are marked as dynamic. Next.js doesn’t cache them.

Hello @MauriceArikoglu! By default, the useFileSystem option is set to true in @neshca/cache-handler. This option allows the pre-rendered HTML file to be read from the file system, which helps overcome a bug. However, the bug occurs when using a custom cache handler that doesn’t read from the file system, such as the example from the Next.js docs or @neshca/cache-handler with useFileSystem set to false. To fix this issue, the Next.js server must implement a one-time server-side rendering of the page, then serve it to the client and put it in the custom cache. I hope this explanation makes things more straightforward!

@iscekic hello! Even the latest Next.js 14.1.1-canary.7 fails to serve pre-rendered pages generated within the Pages Router using a custom cache handler from the current docs example. You may use App Router because it works in this case. Or, if you have to use Pages Router, look at the @neshca/cache-handler library. It overcomes this issue when utilizing the lib’s default useFileSystem option.