Next Js Custom Routes and SSR Next Js Custom Routes and SSR express express

Next Js Custom Routes and SSR


I was able to figure out the problem. This is not really well documented but you need to prefetch the component. So for my case instead of prefetching /about-us I should have prefetched /about.

That's why there is as prop in the link component. Nextjs 9 just got released which fixes this issue.

https://nextjs.org/blog/next-9#dynamic-route-segments

For nextjs 9 you can save your file as [pid].js and it will catch all paths in a specific route. i.e for /products/test-product you have to create folder products and inside that add [pid].js.

I needed to query for product based on slug so I added this and voila, I have access to the slug inside my component.

Product.getInitialProps = async ({ query }) => {    return { slug: query.pid };};

These issues were pretty frustrating before next 9 but it's heavily simplified and it helped me fully remove server.js.