Zero-downtime + Next.js not working


I have a Next.js app hosted on render and every time I deploy the app is down for a minute or so while the app is building.

  • The only customization I’ve done is that I have mounted the disk /opt/render/project/src/.next/cache/images in order to cache image resizes between deploys
  • I’ve also implemented an /api/healthz but the app is unavailable before it gets called.

It might be the mounted disk that’s the issue - there’s nothing else in the logs around the time of the deploys just getting a blank page and a 5XX error.

Might be the mounted disk. Any other way of caching the images between deploys?

I can’t just static paths and prerender as some of the paths comes dynamically from an external service.

You’re correct. Attaching a persistent disk currently prevents zero-downtime deploys (Disks for Persistent File Storage | Render). We plan to remove this limitation, but in the meantime I think we can find a workaround.

Am I correct in assuming that the image resizing happens as part of the build process? If so, you may be able to leverage the build cache. Anything you store in XDG_CACHE_HOME (the cache directory) will persist between builds, unless you deliberately clear the cache with a “Clear build cache & deploy” or the total cache size exceeds 7GB.

You may also want to consider creating a Render static site that rewrites to your Next.js web service (see URL Redirects and Rewrites | Render). This would let you leverage our free global CDN and would prevent downtime, at least for static assets that are cached in the CDN.

I’m happy to elaborate on any of this if you have questions!

Hey David - they are not part of the build as most of them are dynamic paths + I don’t use next.js SSG as my app user SSR-auth so the pages that have static images doesn’t seem to be picked up. I also use api/-routes heavily, so Render’s static option is not an option.

However, there might indeed be a workaround –

Is it possible to write to XDG_CACHE_HOME / /opt/render/.cache from my app? If that’s properly persistent we could maybe do a symlink from /opt/render/project/src/.next/cache/images to /opt/render/.cache/next-images as part of the build?

Hi @KATT ,

XDG_CACHE_HOME is writable, but is only persisted between builds to help with caching, for example, package compilation. Whatever you put there at runtime will not be persisted.

To clarify @david 's suggestion to use a static site, you can use the rewrites/redirects feature so things like /api requests are rewritten to go to your dynamic site. In this solution, your static site acts as a proxy of sorts for your dynamic site.

1 Like

However it is not a static site, it’s a server rendered site where content may differ depending on the logged in user.

Yes, that makes sense. You can create a new static site in conjunction with your existing dynamic site if you’d like to follow @david’s suggestion. If you would prefer not to follow his suggestion, then you can avoid creating a static site.