0

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.

juliomalves
  • 42,130
  • 20
  • 150
  • 146
  • Does this answer your question? [How to add new pages without rebuilding an app with +150k static pages?](https://stackoverflow.com/questions/66036558/how-to-add-new-pages-without-rebuilding-an-app-with-150k-static-pages) – juliomalves May 19 '23 at 17:28

1 Answers1

-1

Possible Solution:

I change fallback from false to true, and it works on Netlify without getting a 404. It breaks on localhost though, but works on the live server.