I faced a problem while creating static pages in Next JS with GraphQL requsts using fetch.
I've created page where I render list with over 100 items and also created static dynamic pages for those items using generateStaticParams function provided by Next.
Local build was done without any issues, but when I deployed it to Vercel, build crashed. I am wondering if the problem is somewhere in WordPress.
To check it, I replaced those static pages with data from https://jsonplaceholder.typicode.com/ - free fake API. And with that API everything works fine.
Error that appeared during vbercel build:
I would be grateful if someone could help me with this. Thanks :)
I tried different API to check if this error will also happen, but everything worked fine.
My code inside [slug]/page.tsx:
import { getTeachingBySlug, getTeachings } from "@/services"
import { WPSerializer } from "@/utils"
export const generateStaticParams = async () => {
const teachings = await getTeachings()
const { teachingList } = WPSerializer(teachings)
return teachingList.map((teaching) => ({
slug: teaching.slug,
}))
}
const SingleTeaching = async ({ params }: { params: { slug: string } }) => {
const teachingSingle = await getTeachingBySlug(params.slug)
return (
<div>
<h1>
Single Teaching
</h1>
<h2>{teachingSingle.documentBy.title}</h2>
</div>
)
}
export default SingleTeaching
getTeachings function look like that:
const getTeachings = async (restOptions?: {}): Promise<WPDocuments> => {
const response = await fetch("https://some-wp-site-base-url/graphql", {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
query: `
query GetTeachings {
documents(first: 1000) {
edges {
node {
title
slug
link
nauczania {
pdfFile
mediaUrlLink
file
description
}
}
}
}
}
`,
variables: {},
...restOptions
})
})
const data = await response.json()
return data.data
}
export default getTeachings
And also getTeachingBySlug:
import { WPDocumentBySlug } from "@/types"
const getTeachingBySlug = async (slug: string, restOptions?: {}): Promise<WPDocumentBySlug> => {
const response = await fetch("https://some-wp-site-base-url/graphql", {
method: "POST",
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
query: `
query TeachingBySlug ($slug: String = "") {
documentBy(slug: $slug) {
title
nauczania {
pdfFile
mediaUrlLink
file
}
link
}
}
`,
variables: { slug }
}),
...restOptions
})
const data = await response.json()
return data.data
}
export default getTeachingBySlug
My operating system: Windows 11 Node version: 18.12 Next version: 13.1.6