2

I am building an e-commerce web app in Next.js and I got stuck on product filtering. Let's say there are 3 possible filtering options:

  1. by category - dioptric / sunglasses
  2. by sex - men / women
  3. by brand - rayban / gucci

Now, the filters should work as subpages. Let's say the user chose sunglasses for men, so the URL should look like this: /sunglasses/men. Now, here are 4 possible example URLs on my website:

  1. /sunglasses/men
  2. /sunglasses/rayban
  3. /sunglasses/men/rayban
  4. /sunglasses/rayban/men

I cannot figure out how the router should be able to distinguish between the first two URLs and figure out that the second parameter in the first URL is a sex filter, but the second parameter in the second URL is a brand filter.

I am using Next.js and its server side rendering (getServerSideProps).

Thank you for any help.

2
  • 1
    Why don't you use query params? /products?category=x&sex=y&brand=z Commented Jul 1, 2021 at 16:12
  • I am already using query params and it works, but I want to change it this way Commented Jul 4, 2021 at 15:27

3 Answers 3

2

You can use the spread operator inside the route name to solve this problem.

#  pages/[...product].ts

Example: pages/sunglasses/men/gucci

Your query object will be like this

{"product": ["sunglasses","men","gucci"]}

More information check Nextjs Docs

Sign up to request clarification or add additional context in comments.

Comments

2

If you don't want to create separate routes , you can use below implementation of gerServerSideProps

export async function gerServerSideProps({ params, ...props }) {
  const products = await fetchProducts();
  if (params.slugs && params.slugs instanceof Array) {
    if (params.slugs.length > 2) {
      const filteredProducts = products.filter(
        (p) =>
          p.category === params.slugs[0] &&
          (p.sex === params.slugs[1] || p.sex === params.slugs[2])&&
          (p.brand === params.slugs[1] || p.brand === params.slugs[2])
      );
      return {
        props: {
          products:filteredProducts
        },
      };
    } else if (params.slugs.length > 1) {
        const filteredProducts = products.filter(
            (p) =>
              p.category === params.slugs[0] &&
              (p.sex === params.slugs[1] || 
              p.brand === params.slugs[1])
          );
          return {
            props: {
              products:filteredProducts
            },
          };
      
    } else if(params.slugs.length > 0){
        const filteredProducts = products.filter(
            (p) =>
              p.category === params.slugs[0])
          return {
            props: {
              products:filteredProducts
            },
          };
  }
  else{
    return {
        props: {
          products:[]
        },
    };

}
}

1 Comment

Thanks! This seems like the solution I am looking for. I'm going to try this in the next few days and will let you know if it works.
0

You can create a separate folder for routes that have same params.

e.g

1./sunglasses/men

1./pages/sunglasses/gender/[gender].js

2./sunglasses/rayban

2./pages/sunglasses/type/[type].js

3./sunglasses/men/rayban

3./pages/sunglasses/item/[gender]/[item].js

4./sunglasses/rayban/men

4./pages/sunglasses/gender/[item]/[gender].js

1 Comment

Yes, I also came up with this solution but in my app, there are hundreds of variations, so the solution must be dynamic

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.