Next.js Discord

Discord Forum

ISR and revalidation on demand at the same time?

Answered
Finnish Hound posted this in #help-forum
Open in Discord
Finnish HoundOP
Hello! is it possible to use isr and revalidation on demand at the same time? I tried that and revalidation on demand only worked when I set revalidate to false.
Answered by Z4NR34L
Here you can find my route for revalidating page router paths:

import { NextApiRequest, NextApiResponse } from "next"

export const config = {
  maxDuration: 300,
}

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse
) {
  const { headers, body } = req
  const token = headers["revalidate-token"]
  if (token !== process.env.REVALIDATE_TOKEN) {
    return res.status(401).json({ message: "Invalid token" })
  }

  try {
    const { path } = body
    await res.revalidate(path)
    return res.json({ revalidated: true, now: Date.now() })
  } catch (err) {
    console.error(err)
    return res.status(500).json({
      message: "Error revalidating",
      error: err,
    })
  }
}
View full answer

15 Replies

Hi, sure it's possible. I'm using on-demand revalidation with ISR in few projects - so far I didn't found any issues about that.
Finnish HoundOP
thanks for the quick answer! is there anything to consider? any special cache settings or something else?
I've just set export const revalidate = <time> and calling revalidatePath when needed, it only you would find any issues I would be glad to help and learn about them 😉
Finnish HoundOP
we use the old pages router and use revalidate: 3600 and a revalidation hook that only works if revalidate is set to false. we have updated nextjs to version 14.0.2. Fallback is on 'blocking'. We have no explanation for the error
OK, I will test that tomorrow as I have one project on pages router during incremental migration to app router
Finnish HoundOP
great, thank you!👍
Page router, ISR refreshed:
same route, after on-demand revalidate:
just working 😄
but remember that for pages router you need to use other revalidate methods then for app router
Here you can find my route for revalidating page router paths:

import { NextApiRequest, NextApiResponse } from "next"

export const config = {
  maxDuration: 300,
}

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse
) {
  const { headers, body } = req
  const token = headers["revalidate-token"]
  if (token !== process.env.REVALIDATE_TOKEN) {
    return res.status(401).json({ message: "Invalid token" })
  }

  try {
    const { path } = body
    await res.revalidate(path)
    return res.json({ revalidated: true, now: Date.now() })
  } catch (err) {
    console.error(err)
    return res.status(500).json({
      message: "Error revalidating",
      error: err,
    })
  }
}
Answer
secured with revalidate token in headers
If you will run into any problems with that feel free to ping me for help 😉
@Finnish Hound let me know if that solves your issues 😄
Finnish HoundOP
It seems to be a problem with the Storyblok API and the public token. With the preview token it works now. No idea why, but anyway thanks for your help!👍 👍