Skip to content

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

@drewbietron

Description

@drewbietron

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIssue was opened via the bug report template.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions