Kikobeats/cacheable-response

404s get cached and provided as 200

iankleist opened this issue · 11 comments

Hi!

I followed the next.js ssr-caching example, so I've got a route like this:

server.get('/product/:handle', (req, res) => { const queryParams = { handle: req.params.handle }; const pagePath = '/product/' + req.params.handle'; return ssrCache({ req, res, pagePath, queryParams }); });

When a non-existent handle gets hit, the server responds with 404 but the second time it's 200. How can I prevent this?

Hello,

I'm not using the Next.js, so I guess you need to open an issue or request for advice at Next.js repository 🙂

Hi! Thanks for your fast reply! So this is a Next.js problem? It appears that cachable-response caches requests with a 404 status code in my dynamic routes and provides them with a 200 status code.

that's a thing depend on your code logic; If you call get all the time, a new fresh cache copy will create independently of the http status code:

https://github.com/Kikobeats/cacheable-response#get

otherwise, if you want to prevent cache 404 status code, then don't call get under those scenarios. makes sense?

Yes thanks, I tried to do that. Somehow I can't find a way to catch the 404's and prevent calling get before that. I'll figure it out eventually.

In the current implementation, you can avoid store a value into the cache if you return undefined or null: https://github.com/Kikobeats/cacheable-response/blob/master/index.js#L85

so, if in your code workflow you return one of these values under your 404 error, then it should be enough to don't store it into the cache.

Can you confirm me that workflow works?

Thank you @Kikobeats ... that works!

@iankleist Thanks for confirming!

I'm going to extend docs to clarify about that 🙂

done at 649de67

@iankleist

謝謝@Kikobeats ...有效!

How do you do it
I also use nextJS
In my Case
SSR get Data Error in .getInitalproos , it will cache error page
how to pass this case

@Kikobeats any method focue remove cache

@lamb-mei See this comment here. Next.js will update their ssr-caching example to include the if-statement in said comment.

Thank you @iankleist