Deploying a Next.js application with ISR support

Hi all,

We have been trying to set up ISR (incremental static regeneration) support in our Next.js applications. In short, Next.js builds static files based on dynamic data on build. By setting up ISR, Next.js will periodically update statically built files during runtime based on a revalidation time. For example: setting revalidation time to 30 seconds will revalidate, rebuild a page and store the page on disk after every 30 seconds.

The initially built pages (during build hook) and dynamically built pages are both stored under the .next folder, which is the default build folder that Next.js uses.

We are having trouble setting this up nicely. The ideal situation would be for the .next build folder to be writeable during the build hook and after, but setting up a mount for this is not supported, because mounts aren’t available during build hooks.

We have worked around this by building to a different location during the build hook and moving the build to the .next folder (which we mounted) in the deploy hook. Though, this solution is subpar.

Any ideas that might make our setup less cluttered or is this the only solution?

2 Likes

Hi,

As of today, this is the only solution.
We are aware this feels hacky and are working on bringing better ways to handle such use-cases.
If you encounter any issue or limitation with the current setup, let us know – we’ll be happy to help.

In the Company we are looking for a platform to use NextJS + Drupal.
The Applications we develop make extensive use of NextJS ISR. For this reason, it is imperative for us that you resolve this issue before migrating our sites to your platform. Are you interested and planned when to solve this problem?

The Platform.sh deployment methodology is based around the idea that the code you committed to the repository and built and tested is the exact code that runs in production. Self-modifying code that updates itself on the fly will have a bit of a conflict with that philosophy.
However, the solution described above has been found to be adequate for many uses, there are a number of static site builders that use that method.

  • Pre-build the generated files in one step,
  • then copy them into the desired location during deploy.

A number of deployment pipelines are somewhat more intricate than that. See gatsby-strapi eg
It may be possible to supply a new template example that includes the required additional lines for NextJS ISR in the deploy hook, but that has not been contributed yet.

My issue I am having is that next start is run before the deploy hook runs. This means that the .next directory is out of sync when the server starts as the files are copied over later. How would you resolve that issue? Can you move the next start command into the deploy hook so it runs after the mount is updated?

I don’t completely follow what the steps are that you are needing, but you’ve got 3 hooks to work with : build , deploy, and post-deploy. App reference · Platform.sh Documentation
In general I wouldn’t think there would be a problem with running a start command in the deploy hook. That’s usually when the app is most “live” anyway, with access to all its mounts and services, so that’s where last minute pre-launch tasks go.

thanks my issue has been fixed.

@alexsunny123 I’m running into the same issue. Could you please show your application yaml file?

Edit: @dman I think I’ve got a workaround for the issue, here’s the PR: https://github.com/platformsh-templates/nextjs/pull/82