Parent Category / Sub Category caching in routes.ts

How do you recommend configuring caching in routes.ts for the following category and product URL structures?

category: “parent-category/category” and “/parent-cateogory”
product: “product-name.html?cgid=X123” where cgid = categoryID

1 Like

That’s tricky, but is sadly a very common use case for websites that have been around for a while and thus have legacy SEO to preserve. Most websites being designed today use a route structure like /categories/:id that indicates the page type. Frameworks like Next.js and Nuxt.js basically require this.

The solution, in most cases, is to add a catch-all route at the top of your router to cache everything, then remove caching for specific paths in subsequent routes. For example:

new Router()
  // cache everything by default
  .get("/:slug*", ({ cache }) => {
    cache({
      edge: {
        maxAgeSeconds: 60 * 60 * 24,
        staleWhileRevalidateSeconds: 60 * 60,
      },
    });
  })
  // don't cache specific routes like the cart and checkout
  .match("/cart", ({ cache }) => {
    cache({ edge: false });
  })
  .match("/checkout", ({ cache }) => {
    cache({ edge: false });
  });
1 Like