Jens
Jens

Reputation: 205

How can I use Next.js incremental static regeneration (ISR) when deploying on Docker?

I wonder how best to deploy a Next.js app on Docker. The Docker image, once built, should be deployed on two different environments: first on TEST then - using the very same image - on PROD. Each environment has its own database.

It doesn't make sense to use SSG in this case: there are two databases and none of them is available when the Docker image is built (i.e. when "npm run build" is executed). However, it would make sense to generate pages when the Next.js server starts up ("npm start") and from thereon let ISR regenerate pages whenever a revalidate interval is reached. Is that possible ?

For example, I could imagine some boolean flag (e.g. "revalidateOnStartup") that forces initial page generation in getStaticProps():

  return {
    props: {
      //...
    },
    revalidate: 3600,
    revalidateOnStartup: true
  }

I did already try to use an empty data set during build in getStaticProps() and setting a revalidate interval of e.g. 3600 (one hour). However, in that case, the application starts up with an empty page. And it then takes one hour for the first revalidate to kick in and data to show up on the page. I somehow need to tell Next.js to not initially wait for that interval.

An alternative to ISR (if that is not possible here) might be to use SSR (server-side-rendering) and a CDN for caching.

Upvotes: 4

Views: 1617

Answers (1)

craigw
craigw

Reputation: 715

You can achieve it by including "npm run build" as the Docker ENTRYPOINT, before it runs the start command.

Eg ENTRYPOINT "npm run build && npm run start"

The container startup time will obviously be slower, but if you're using something like Kubernetes you can add a readiness check, so upgrades see no delay.

This way you can ingest the environment variables of the deployment target and can communicate with the content APIs of that environment.

You should still do the initial build (with mocked/empty static props) to warm up the Next build cache.

UPDATE: I've written a blog post that summarises this and some other alternatives for people who find themselves solving this issue: http://www.craigwardman.com/Blogging/BlogEntry/ssg-isr-and-environment-variables-in-next-js-and-docker

Upvotes: 4

Related Questions