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
Server Components fetch() throws in page.tsx #46840
Comments
Please note. |
Can you check if this works in |
I still get the same error |
It seems to only be happening using Node 18.x Seems that had this issue before. |
|
I get the same error message when I use a relative path ( EDIT: Here's my
|
That helps me. It's quite sad because it can cause issues later on maxing it fixed with http://localhost:3000/ |
Signed-off-by: teobler <teobler@163.com>
Signed-off-by: teobler <teobler@163.com>
Any info to this? In development it is solved by using an absolute route (e.g. http://localhost:3000/api/hello) but if I want to deploy to Vercel, obviously this route would not work. |
Running into the same issue |
Same issue here, using |
Same issue. In the meantime I'm using this that works in all my environments: import { headers } from "next/headers";
// Inside the page component
const headersData = headers();
const protocol = headersData.get("x-forwarded-proto");
const host = headersData.get("host");
const data = await fetch(`${protocol}://${host}/api/hello`); |
import { headers } from "next/headers"; // Inside the page component |
I have the same issue but can't use next/headers because parent component is a client component so get this error: |
This works perfectly. |
Verify canary release
Provide environment information
Which example does this report relate to?
async/await in Server Components
What browser are you using? (if relevant)
Opera
How are you deploying your application? (if relevant)
No response
Describe the Bug
In your page.tsx file.
fetch() always throws : TypeError: Failed to parse URL from /api/getCards
It also throws if you using axios.
Expected Behavior
As per example on website https://beta.nextjs.org/docs/data-fetching/fetching#asyncawait-in-server-components
It uses the page.tsx file.
It should work as expected.
How it doesn't.
To Reproduce
Create a page.tsxx
And attempt the following code
import React from 'react';
async function HomePageChild() {
const data = await fetch('/api/getCards') // throws "TypeError: Failed to parse URL from /api/getCards"
return (
<> Child</>
)
};
export default HomePageChild
The text was updated successfully, but these errors were encountered: