Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unstable_revalidate throws in production mode locally #34874

Closed
1 task done
alii opened this issue Feb 28, 2022 · 7 comments
Closed
1 task done

unstable_revalidate throws in production mode locally #34874

alii opened this issue Feb 28, 2022 · 7 comments
Labels
bug Issue was opened via the bug report template.

Comments

@alii
Copy link

alii commented Feb 28, 2022

Verify canary release

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

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 21.3.0: Wed Jan  5 21:37:58 PST 2022; root:xnu-8019.80.24~20/RELEASE_ARM64_T8101
Binaries:
  Node: 17.4.0
  npm: 8.3.1
  Yarn: 1.22.17
  pnpm: N/A
Relevant packages:
  next: 12.1.1-canary.4
  react: 17.0.2
  react-dom: 17.0.2

What browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

Describe the Bug

Was able to figure out that the client could not request itself, perhaps because it's not bound to 0.0.0.0? But the error message was extremely poor and I had to edit the console.log in the following file to figure out what had happened
image

image

Expected Behavior

No error

To Reproduce

Build a Next.js app that has a dynamic statically rendered route with ISR, build it locally and run it locally and call unstable_revalidate to see it error.

@alii alii added the bug Issue was opened via the bug report template. label Feb 28, 2022
@alii
Copy link
Author

alii commented Feb 28, 2022

Temp workaround was forcing next to bind to localhost:

yarn next start -H localhost

@balazsorban44
Copy link
Member

balazsorban44 commented Feb 28, 2022

I created the following reproduction:

pages/[...page].js

import { useRouter } from "next/router"

export default function Home(props) {
  const { query: { page } } = useRouter()
  return (
    <button
      onClick={async () => {
        await fetch(`/api/revalidate?page=${page}`)
        window.location.reload()
      }}
    >
      Revalidated {page}: {props.time}
    </button>
  )
}

export function getStaticPaths() {
  return { paths: [], fallback: true }
}

export function getStaticProps() {
  return { props: { time: new Date().toISOString() }, revalidate: 3600 }
}

pages/api/revalidate.js

export default async function handler(req, res) {
  await res.unstable_revalidate(`/${req.query.page}`)
  return res.json({ revalidated: true })
}

And calling http://localhost:3000/api/revalidate either through fetch or visiting directly works correctly on localhost.

@ijjk
Copy link
Member

ijjk commented Mar 3, 2022

re-opening as this sounds like a bug with the host value we are using internally on some setups

@thefinnomenon
Copy link

thefinnomenon commented Apr 8, 2022

Ended up here because of a similar error but in my case the error message is a 404 (I went in and added the for path part)

Error: Error: Invalid response 404 for http://localhost:3000/posts/my-first-post
    at unstable_revalidate (.../node_modules/next/dist/server/api-utils/node.js:228:15)

The 404 for this path makes sense because it really should have a subdomain (e.g. http://chris.localhost:3000/posts/my-first-post). Digging further into this node.js file, the problem is here,

async function unstable_revalidate(urlPath, req, context) {
    ...
    const baseUrl = context.trustHostHeader ? `https://${req.headers.host}` : `http://${context.hostname}:${context.port}`;
   ...
}

If context.trustHostHeader is false, like in this case, it creates a url that omits a possible subdomain. When deployed, everything works correctly because it evaluates to true and uses the first url template.

@ijjk
Copy link
Member

ijjk commented Apr 8, 2022

Closing as this should be fixed in the latest version of Next.js v12.1.4 specifically this PR #35386

@ijjk ijjk closed this as completed Apr 8, 2022
@alii
Copy link
Author

alii commented Apr 8, 2022

awesome thx :) ❤️

@github-actions
Copy link
Contributor

github-actions bot commented May 9, 2022

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 9, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template.
Projects
None yet
Development

No branches or pull requests

4 participants