Run build error

Hi could someone hop in a call with me and help me out with a getstaticprops problem. trying to get my portfolio done so i can apply for internships. this is the error when i npm run build, Error occurred prerendering page "/". Read more: https://nextjs.org/docs/messages/prerender-error TypeError: fetch failed at Object.fetch (node:internal/deps/undici/undici:14062:11) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async fetchPageInfo (C:\Users\Nando\Documents\Fernando_Portfolio\portfolio\my-portfolio.next\server\pages\index.js:949:17) at async getStaticProps (C:\Users\Nando\Documents\Fernando_Portfolio\portfolio\my-portfolio.next\server\pages\index.js:922:22)
24 Replies
ATOMowy_grzyb
ATOMowy_grzyb14mo ago
Well,
TypeError: fetch failed
the fetch has failed, pretty clear message. Check the URL and whatnot or add a catch clause to see why.
Tacoboi99
Tacoboi99OP14mo ago
hahaha yeah. thing is its fetching all the data everything works fine in dev but on build it says that what i am fetching is not json. But i turn the res to json in my api
ATOMowy_grzyb
ATOMowy_grzyb14mo ago
then check what it is, typically it's a case where you're getting a HTTP page for 404 error as a response and that, indeed, is not JSON
Tacoboi99
Tacoboi99OP14mo ago
its not trpc i am sadly using plain nextjs haha
ATOMowy_grzyb
ATOMowy_grzyb14mo ago
I think Josh's point is that there's no info
Josh
Josh14mo ago
Correct. If you would have even bothered to look at the first 2 messages, there is no context here And I really don't feel like recreating that thread with you here
Tacoboi99
Tacoboi99OP14mo ago
oh okay give me a sec! my bad hah this is my api, import type { NextApiRequest, NextApiResponse } from "next"; import {groq} from "next-sanity"; import { client } from "../../../sanity.config"; import { PageInfo } from "../../../typings"; const query = groq*[_type == "pageInfo"][0] type Data = { pageInfo: PageInfo } export default async function handler(req:NextApiRequest, res: NextApiResponse<Data>) { const pageInfo : PageInfo = await client.fetch(query); res.status(200).json({pageInfo}); }
Josh
Josh14mo ago
Im amazed you didn't even attempt to look at what I sent. What if your problem was actually solved in there? Don't be lazy. If you're asking for help and people offer it, don't blow it off Any time you send code you should format it I'm not reading anything until you have read that thread I posted
Tacoboi99
Tacoboi99OP14mo ago
alright give me a longer second have to figure out how to format
Josh
Josh14mo ago
We still have no context Read this thread
Tacoboi99
Tacoboi99OP14mo ago
yes will do Alright so i am coding a portfolio with next.js, typescript and sanity. I have connected sanity (took way too much time for me) and created apis to get the data and then utils functions to fetch from the api and ran the utils function in a getStaticProps. In my dev environment everything work but when i run build i get this error on vercel:
Error occurred prerendering page "/". Read more: https://nextjs.org/docs/messages/prerender-error
SyntaxError: Unexpected token T in JSON at position 0
at JSON.parse (<anonymous>)
at parseJSONFromBytes (node:internal/deps/undici/undici:6662:19)
at successSteps (node:internal/deps/undici/undici:6636:27)
at node:internal/deps/undici/undici:1236:60
at node:internal/process/task_queues:140:7
at AsyncResource.runInAsyncScope (node:async_hooks:203:9)
at AsyncResource.runMicrotask (node:internal/process/task_queues:137:8)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Error occurred prerendering page "/". Read more: https://nextjs.org/docs/messages/prerender-error
SyntaxError: Unexpected token T in JSON at position 0
at JSON.parse (<anonymous>)
at parseJSONFromBytes (node:internal/deps/undici/undici:6662:19)
at successSteps (node:internal/deps/undici/undici:6636:27)
at node:internal/deps/undici/undici:1236:60
at node:internal/process/task_queues:140:7
at AsyncResource.runInAsyncScope (node:async_hooks:203:9)
at AsyncResource.runMicrotask (node:internal/process/task_queues:137:8)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
As i understand from googling a little is that either im fetching something that isnt json from sanity or its not fetching and recieveing 404 html. I am very new to solving errors that are more api or backend related so im am a complete 0 at it. My api looks like this:
import type { PageInfo } from "../typings";

export const fetchPageInfo = async() =>{
const res = await fetch(`${process.env.NEXT_PUBLIC_BASE_URL}/api/getPageInfo`)

const data = await res.json();

const pageInfo: PageInfo = data.pageInfo;
return pageInfo
}
import type { PageInfo } from "../typings";

export const fetchPageInfo = async() =>{
const res = await fetch(`${process.env.NEXT_PUBLIC_BASE_URL}/api/getPageInfo`)

const data = await res.json();

const pageInfo: PageInfo = data.pageInfo;
return pageInfo
}
My getStaticProps:
export const getStaticProps: GetStaticProps = async () => {
const pageInfo: PageInfo = await fetchPageInfo();
const skills: Technology[] = await fetchSkills();
const projects: Project[] = await fetchProjects();
const socials: Social[] = await fetchSocials();

return {
props: {
pageInfo,
skills,
socials,
projects,
},
revalidate: 3,
};
};
export const getStaticProps: GetStaticProps = async () => {
const pageInfo: PageInfo = await fetchPageInfo();
const skills: Technology[] = await fetchSkills();
const projects: Project[] = await fetchProjects();
const socials: Social[] = await fetchSocials();

return {
props: {
pageInfo,
skills,
socials,
projects,
},
revalidate: 3,
};
};
I dont know if this could be the cause of the problem since it was what fixed the whole api problem i had with sanity in my dev environment. In my .env.local i added
NODE_TLS_REJECT_UNAUTHORIZED=0
NODE_TLS_REJECT_UNAUTHORIZED=0
This is what i get from my api:
{"pageInfo":{"phoneNumber":"+46703541899","socials":[{"_ref":"2f5d84e2-884c-4a68-810f-a7e6f36f159d","_type":"reference","_key":"808e4c0f14d0"},{"_ref":"54bcf48f-7dfe-48b5-8a5c-34ab795fdb11","_type":"reference","_key":"9236d70f4530"},{"_ref":"1039eb82-2954-48f9-9c2b-b64952efbb85","_type":"reference","_key":"36fabf31a408"}],"_rev":"9G1ke557B3leRtFPJs94eL","heroImage":{"_type":"image","asset":{"_type":"reference","_ref":"image-6a51b3aa4abda865ec8e0f486c1b4c51b1ed3410-1000x1000-jpg"}},"name":"Fernando Aleite","_id":"3a839b48-b366-4b84-b313-5fd94e719824","address":"Stockholm area","_type":"pageInfo","backgroundInformatio":"Hi there! I am a frontend student who will graduate summer 2024. As of right now i am looking for an internship starting asap. Apart from coding i enjoy playing video games but also play a lot of volleyball and soccer on my free time.","_createdAt":"2023-10-25T13:27:51Z","email":"[email protected]","role":"Frontend Developer","profilePic":{"_type":"image","asset":{"_ref":"image-fe948d9526afb0ff68278ccbca4291e0dd12eb1c-828x1104-jpg","_type":"reference"},"crop":{"left":0,"bottom":0,"_type":"sanity.imageCrop","right":0,"top":0},"hotspot":{"height":1,"_type":"sanity.imageHotspot","width":1,"x":0.5,"y":0.5}},"_updatedAt":"2023-10-27T14:38:01Z"}}
{"pageInfo":{"phoneNumber":"+46703541899","socials":[{"_ref":"2f5d84e2-884c-4a68-810f-a7e6f36f159d","_type":"reference","_key":"808e4c0f14d0"},{"_ref":"54bcf48f-7dfe-48b5-8a5c-34ab795fdb11","_type":"reference","_key":"9236d70f4530"},{"_ref":"1039eb82-2954-48f9-9c2b-b64952efbb85","_type":"reference","_key":"36fabf31a408"}],"_rev":"9G1ke557B3leRtFPJs94eL","heroImage":{"_type":"image","asset":{"_type":"reference","_ref":"image-6a51b3aa4abda865ec8e0f486c1b4c51b1ed3410-1000x1000-jpg"}},"name":"Fernando Aleite","_id":"3a839b48-b366-4b84-b313-5fd94e719824","address":"Stockholm area","_type":"pageInfo","backgroundInformatio":"Hi there! I am a frontend student who will graduate summer 2024. As of right now i am looking for an internship starting asap. Apart from coding i enjoy playing video games but also play a lot of volleyball and soccer on my free time.","_createdAt":"2023-10-25T13:27:51Z","email":"[email protected]","role":"Frontend Developer","profilePic":{"_type":"image","asset":{"_ref":"image-fe948d9526afb0ff68278ccbca4291e0dd12eb1c-828x1104-jpg","_type":"reference"},"crop":{"left":0,"bottom":0,"_type":"sanity.imageCrop","right":0,"top":0},"hotspot":{"height":1,"_type":"sanity.imageHotspot","width":1,"x":0.5,"y":0.5}},"_updatedAt":"2023-10-27T14:38:01Z"}}
ATOMowy_grzyb
ATOMowy_grzyb14mo ago
are you sure that process.env.NEXT_PUBLIC_BASE_URL is defined in the env where it doesn't work? I'd reiterate my initial suggestion - check what error fetch gives you
Tacoboi99
Tacoboi99OP14mo ago
right now my base url is just http//:localhost:3000/ , i googled my issue and a few were saying it was because of the url. I honestly dont have enough knowledge to know why but the env line fixed it. When i had uploaded it it vercel i was gonna take away the
NODE_TLS_REJECT_UNAUTHORIZED=0
NODE_TLS_REJECT_UNAUTHORIZED=0
line and put in the hosting url and see if it worked.
ATOMowy_grzyb
ATOMowy_grzyb14mo ago
your localhost is over HTTP, remote will be over HTTPS can you please just see what fetch doesn't like, the solution is probably right there in that error
Tacoboi99
Tacoboi99OP14mo ago
Sorry i am very bad at error handling. Do you want me to create a try catch on the api file or utils?
ATOMowy_grzyb
ATOMowy_grzyb14mo ago
you have a function (fetch) that's failing, you should therefore inspect the input and the output - input is the URL, which comes from process.env - this might be undefined - output in this case is an error you can console.log it, you can step into it with a debugger, whatever you can do to get to it you can add a .catch to that fetch call or a try..catch block
const res = await fetch(`${process.env.NEXT_PUBLIC_BASE_URL}/api/getPageInfo`).catch(e => console.error(e));
const res = await fetch(`${process.env.NEXT_PUBLIC_BASE_URL}/api/getPageInfo`).catch(e => console.error(e));
Tacoboi99
Tacoboi99OP14mo ago
i hope to god this is what you were wanting
cause: Error: connect ECONNREFUSED ::1:3000
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1487:16) {
errno: -4078,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '::1',
port: 3000
}
}
cause: Error: connect ECONNREFUSED ::1:3000
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1487:16) {
errno: -4078,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '::1',
port: 3000
}
}
thanks for the code i was doing something waaaay diffrent
ATOMowy_grzyb
ATOMowy_grzyb14mo ago
connection refused on localhost:3000 is what this says you got that during build? if so, where is this building?
Tacoboi99
Tacoboi99OP14mo ago
i just ran it on my terminal npm run build but its also getting error on vercel but on vercel i have added the url of the site as a env
ATOMowy_grzyb
ATOMowy_grzyb14mo ago
on Vercel obviosuly it will not be able to connect to localhost:3000, you have to set the URL in project settings env variables
Tacoboi99
Tacoboi99OP14mo ago
as a base url i mean
ATOMowy_grzyb
ATOMowy_grzyb14mo ago
during build on your local machine, I assume the app is not running so there is nothing serving that /api/getPageInfo endpoint that it's trying to reach during build it will only work if you run the app in one terminal, then build in another terminal, then the app that's running will serve that request on localhost:3000
but on vercel i have added the url of the site as a env
you added the URL of the site that you're trying to build, and you need it to serve you an API that's needed for t he build step (static props)? I'm not sure if that's a bulletproof setup you should be pointing that fetch to an external source of data for it to build the static props, ig you're building on localhost, don't point to localhost unless you have the app running in parallel, if you're building on a remote env, don't point to that very same env
Tacoboi99
Tacoboi99OP14mo ago
alright thank you i think i understand. will try to follow sanity docs instead of tutorial see if that helps. Otherwise ill just do everything manually. Thank you so much you gave me a lot more clarity of where and why things are going wrong. Sorry for not being specific about my problem in the beginning.
Want results from more Discord servers?
Add your server