I fetch data in my Next JS app from Sanity to create dynamic routes. Like this:
export const getStaticPaths = async () => {
const res = await client.fetch(`*[_type in ["work"] ]`);
const data = await res;
const paths = data.map((e) => {
return {
params: { slug: e.slug.current },
};
});
return {
paths,
fallback: false,
};
};
export const getStaticProps = async (context) => {
const slug = context.params.slug;
const res = await client.fetch(`*[_type == "work" && slug.current == "${slug}"]
`);
const data = await res;
const resAll = await client.fetch(`*[_type == "work"] | order(order asc)`);
const dataAll = await resAll;
return {
props: {
post: data[0],
dataAll,
},
revalidate: 1, // 10 seconds
};
};
on localhost everything works fine and quick, on Netlify i am getting a 404 error for every newly generated route. only after a redeploy the page shows up.
My directory looks like this:
-works
----[slug].jsx
----index.jsx
Why doesnt netlify recognize the new path right away? Also, every change to existing content on the site via sanity takes rather long to show up on netlify.
I tried creating a build hook in netlify and listen to the changes in content, to trigger a build on the server every time new content is added.
This seems like a hacky workaround, though. There must be a simpler way to get this to work.