vendredi 25 septembre 2020

catch all URLs in [...slug] and only create static props for valid addresses in Next.js

I'm using dynamic routes and using fallback:true option to be able to accept newly created pages

first i check if parameters is true then i create related props and show the component with that props. In console i can also see that next.js create new json file for that related page to not go to server again for the next requests to the same page.

But even I type wrong address next create new json file for that path. It means that next.js create json file for every wrong path request.

How can avoid that vulnerable approach?

export const getStaticProps: GetStaticProps = async ({ params }) => {
  if (params.slug[0] === "validurl") {
    const { products } = await fetcher(xx);
    const { categories } = await fetcher(xx);
    return { props: { categories, products } };
  } else {
    return { props: {} };
  }
};

const Home = (props: any) => {
  if (!props) {
    return <div>404</div>;
  }
  return (
    <MainLayout {...props}>
      <FlowItems items={props.products} />
    </MainLayout>
  );
};



Aucun commentaire:

Enregistrer un commentaire