next-shared-cache: Don't Discard Page Contents Generated During Build

Brief Description of the Bug

When configured to use a Redis cache, the package currently discards the page contents generated during the NextJS build phase. As a result, initial page contents are generated twice in the common case when deployment follows shortly after build: (1) once during build (slow, developer facing) and (2) again during first page load (slow, user facing (!)).

Severity

Major

Frequency of Occurrence

Always

Steps to Reproduce Provide detailed steps to reproduce the behavior, including any specific conditions or configurations where the bug occurs:

  1. Build a NextJS project that uses ISR for some pages
  2. Deploy the project using this package configured with a single redis-strings cache handler
  3. Load one of the pages generated just seconds ago using ISR, experience a significant delay while the page is built again so that it can be loaded into the cache

Expected vs. Actual Behavior

Pages generated during the NextJS build phase should not be immediately rebuilt after deployment.

Environment:

  • OS: MacOS
  • Node.js version: 20.12.2
  • @neshca/cache-handler version: 1.3.1
  • next version: 14.2.3

Attempted Solutions or Workarounds

I’ve implemented a simple cache handler that loads initial data and HTML from the on-disk files included in the build output. But this should really be the default behavior and included in the handler, not something that a user has to implement—if not only because I probably didn’t get it quite right. Another alternative would be to provide a filesystem cache handler which could provide this functionality, but I think users will be surprised that this doesn’t work out of the box.

Additional context

It looks like a previous version of this package did implement this behavior, but it was removed right before 1.0.0 was released. (I reused some of the code from that version for my handler.) It wasn’t clear to me why that option was removed, and it should probably have fired a warning so users would notice that the package was no longer supporting loading initial values from the filesystem. Next builds are also fairly slow when many pages are being built, so it seems problematic to do all that work and then discard it immediately.

There’s language in the current documentation about the package being able to use (say) Redis during the build phase, which perhaps would address this? But that seems at odds with the Redis security model, which states that:

Redis is designed to be accessed by trusted clients inside trusted environments. This means that usually it is not a good idea to expose the Redis instance directly to the internet or, in general, to an environment where untrusted clients can directly access the Redis TCP port or UNIX socket.

At least in my deployment flow, I perform the builds on my local machine, push the Docker container, and deploy that to our Kubernetes cluster. Meaning that the build step does not run inside the trusted environment and so doesn’t have access to the Redis instance using during production. Perhaps this is odd? It doesn’t seem that way to me, necessarily. And it may be more common for users trying to deploy NextJS applications to non-Vercel environments, which AFAICT is essentially the intended audience for this package…

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 1
  • Comments: 16 (1 by maintainers)

Commits related to this issue

Most upvoted comments

so, we looked some more into this, and by adding some custom code it seems like we can achieve what we need:

when the client is instantiated for the first time:

  1. we create a list of the HTML files that were generated during build in the pages folder
  2. we iterate over the files, calling the handler to cache the contents according to the file modification date, and with the same attributes in terms of ttl and expiration that the handler would normally assign during runtime

this is visible here: https://github.com/wunderio/next-drupal-starterkit/pull/182/files#diff-e3c8a9ea9ddb0989e98b5e09063f02e372a49e57fbfb4a1bbd69ac55645d25c1

(feel free to take a look and tell us if your implementations are similar or we missed something 😃 )

@vermario @gchallen @lzw1926 hi,

I’m excited to share that the feature you’ve all been anticipating is now in progress!

I’ve just opened a PR that introduces pre-population of the cache with the initial data at application startup, which is expected to bring a noticeable performance improvement. This new functionality will be made possible via a registerInitialCache function, which leverages the CacheHandler class to populate the cache for all Handlers.

To achieve this, it is utilizing Next.js’s instrumentation hook to run just before the server starts. This way, you can efficiently pre-populate the cache without the need for any additional scripts right before the server startup.

It’s worth noting that this is currently an experimental feature and may evolve in future releases.

Feel free to check out the PR here: #743. I appreciate your feedback as I continue to refine this feature!

In addition to this, if you check https://github.com/caching-tools/next-shared-cache/issues/556 , it seems that also under normal operations the stale-while-revalidate mechanism can’t work, since the cached contents in redis are evicted straight away. This to my mind makes ISR not compatible at all with this.

I would hope that it doesn’t break later ISR page regeneration. I think what’s being discussed here is just the initial load. NextJS doing an initial pass on generating ISR pages during the build step, assuming that these will be used as the initial page contents. (Of course, this assumes that the built pages will be rapidly deployed, but that’s probably not a bad assumption. If they aren’t, the ISR interval could expire before the pages were used.)

The initial page build also ensures that the first page load can always return a stale page immediately, even if an ISR build is triggered due to the contents being stale. That’s part of what’s so frustrating about this limitation of this library. Here’s what’s supposed to happen:

  1. next build: creates page /foo
  2. (sometime later): /foo loaded, returns prebuilt contents immediately
  3. (if needed): ISR triggered if page is stale, rebuilt in background
  4. (next load): gets latest /foo contents
  5. and so on

Here’s what happens when you use this caching library:

  1. next build: creates page /foo
  2. (sometime later): prebuilt /foo has been discarded, so a full page build is done while the user waits!

Not good.

I am wondering if I just assumed that this library was meant to work with ISR, but maybe I have imagined it? 😃

While implementing this for our starterkit for drupal -next.js projects I have been wondering about this same thing.

We are in the same situation described above, and it seems that basically using this cache handler makes the build step in the normal ISR next.js workflow futile. The data that is built at build time is then never used, I think.

And, coupled with the issue described here: https://github.com/caching-tools/next-shared-cache/issues/556 it also seems to negate a lot of the benefits of ISR in general.

Maybe I am missing something though 😃