question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ISR unstable_revalidate -- TypeError: res.hasHeader is not a function

See original GitHub issue

Verify canary release

  • I verified that the issue exists in Next.js canary release

Provide environment information

Operating System:
     Platform: linux
      Arch: x64
      Version: #1 SMP Tue Jun 23 12:58:10 UTC 2020
    Binaries:
      Node: 14.17.3
      npm: 6.14.13
      Yarn: 1.22.17
      pnpm: N/A
    Relevant packages:
      next: 12.1.5
      react: 18.0.0
      react-dom: 18.0.0

What browser are you using? (if relevant)

Chrome 102.0.5005.61

How are you deploying your application? (if relevant)

Vercel

Describe the Bug

I am attempting to use res.unstable_revalidate to revalidate an ISR generated page. When invoking the function, the following error is returned ->

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse
) {
  if (req.query.secret !== process.env.ISR_REVALIDATION) {
    return res.status(401).json({ message: 'Invalid token' });
  }

  if (!req.body.path) {
    return res
      .status(400)
      .json({ message: 'Please provide a path to revalidate' });
  }

  try {
    await res.unstable_revalidate(req.body.path);
    return res.json({
      message: `${req.body.path} Revalidated`,
      revalidated: true,
    });
  } catch (err) {
    // If there was an error, Next.js will continue
    // to show the last successfully generated page
    return res.status(500).send('Error revalidating');
  }
}
TypeError: res.hasHeader is not a function

The error will not be returned if I pass just the index route, /. However, any other path that is passed in from a ISR page will return TypeError: res.hasHeader is not a function

Full stack trace

at handler (webpack-internal:///(api)/./src/pages/api/revalidate.ts:30:21)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
at async Object.apiResolver (/node_modules/next/dist/server/api-utils/node.js:185:9)
at async DevServer.runApi (/node_modules/next/dist/server/next-server.js:404:9)
at async Object.fn (/node_modules/next/dist/server/base-server.js:507:37)
at async Router.execute (/node_modules/next/dist/server/router.js:212:36)
at async DevServer.run (/node_modules/next/dist/server/base-server.js:642:29)
at async DevServer.run (/node_modules/next/dist/server/dev/next-dev-server.js:514:20)
at async DevServer.handleRequest (/node_modules/next/dist/server/base-server.js:334:20)

Expected Behavior

To run the function and trigger a revalidation

To Reproduce

Trigger res.unstable_revalidate and pass whatever path you want to revalidate.

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
bejarano-techcommented, Oct 3, 2022

Ok, i’m going to prepare a reproduction reference app. I will send you the link soon.

0reactions
dpnoltecommented, Dec 12, 2022

This seems to happen if the provided url path points to a route that is a server side rendered page. And looking at the comment here of @bejarano-tech, it probably also happens when the route no fallback defined getStaticPaths . If this is the case, it would be nice to have better error handling for it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

res.revalidate() is not a function in NextResponse of Nextjs for ...
i'm using v12.2.5 but it is there from v12.2.0 . i want to use on-demand ISR in typescript. https://nextjs.org ...
Read more >
Blog - Next.js 12.1
On-demand Incremental Static Regeneration (Beta). Next.js now exposes a function unstable_revalidate() allowing you to revalidate individual ...
Read more >
Build a Restaurant Website with Next.js and Cosmic
On-demand Incremental Static Regeneration ISR. Next.js now exposes a function unstable_revalidate() allowing you to revalidate individual pages ...
Read more >
Next.js On-Demand ISR // Full tutorial - YouTube
Next.js On-Demand ISR is was published in beta for Next.js version 12.1. In this video you will learn what On Demand ISR is,...
Read more >
res.hasHeader is not a function - TechInPlanet
I've run into the error below when trying to use getServerSideProps to fetch data from an endpoint. Error: ERROR TypeError: res.hasHeader is ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found