next-boost / next-boost

Add a cache layer for server-side-rendered pages with stale-while-revalidate. Can be considered as an implementation of next.js's Incremental Static Regeneration which works with getServerSideProps.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Custom cacheKey to cache on first dynamic url parameter should not cache second url prop to next router.

dan003400 opened this issue · comments

Here is an example of a config that will only cache on the first dynamic parameter:

/t/[tid]/c/[cid].js => /t/1/c/2

This will result in a cache miss on the first request with t/1/...... (tid=1) - But a hit on the second request no matter what cid parameter is.

This is done do use the first parameter as an id for dynamically created templates which should be be SSR Cached, but that template can be shared by multiple users, each with their own cid.

This is working, the only problem is that cid is also cached, meaning after the first request, the cid does not change.

What is the best way to access this cid in each request?

module.exports = {
  cacheKey: (req) => {
    if (req.url.split("/").splice(1, 1)[0] === "t") {
      return req.url.split("/").reduce((acc, s, index) => {
        return index <= 2 ? acc += s : acc
      }, "")
    } else {
      return req.url
    }
  },
  rules: [
    {
      regex: "^/t/*",
      ttl: 60, // 1 Minute
      tbd: 60 * 60 * 24 * 2 // 2 Days
    },
  ],
};

We have resulted into snagging this from location.pathname - But thing this would be a great way to some how whitelist a dynamic url parameter.

const cid = window.location.pathname.split("/")[4]

Seems problem solved. I'm closing it.

Was this solved in a PR? Because I am still seeing the issue and have the same hack above to get non-cached url parameters.