Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I disable cache on a specific page only in Next.JS

I'm running a job aggregator and I wish to avoid cache on my post feed, since its showing outdated results to my users. We're running on Next.JS.

I've searched through their docs, but didn't find a way about how to do it...

Does someone have an idea?

I assume its on next.config.js, but I don't know exactly how.

like image 383
Dr. Joao Paulo Avatar asked Jun 08 '20 08:06

Dr. Joao Paulo


People also ask

How do I disable a specific website cache?

Here's how... When you're in Google Chrome, click on View, then select Developer, then Developer Tools. Alternatively, you can right click on a page in Chrome, then click Inspect. Click on the Network tab, then check the box to Disable cache.

Is it possible to disable ETAG generation in next JS?

Next. js will generate etags for every page by default. You may want to disable etag generation for HTML pages depending on your cache strategy.

Does Next JS automatically cache?

Caching. Caching improves response times and reduces the number of requests to external services. Next. js automatically adds caching headers to immutable assets served from /_next/static including JavaScript, CSS, static images, and other media.


Video Answer


1 Answers

This seems work if server-side rendering

Page.getInitialProps = async ({ store, res }) => {
    if (res) {
        // res available only at server
        // no-store disable bfCache for any browser. So your HTML will not be cached
        res.setHeader('Cache-Control', 'no-store');
    }

    await store.dispatch(action());
    return {};
};
like image 72
Vinnie Avatar answered Nov 15 '22 04:11

Vinnie