1

I am testing my application - for which I have written backend using node and express and frontend using React.

I am not simulating a slow-network - but I am actually sitting in a slow network.

Sometimes, an xhr request using axios - which I have written in my code doesn't actually work - because of network request caching.

When I open the chrome developer console in a separate window and check the "disable cache" button- it works perfectly.

How can I enforce this "disable cache" at all times in chrome without opening the developer console.

Is there any other browser which has this feature?

1
  • add a Cache-Control: no-cache header to the response? Commented Oct 31, 2017 at 13:27

1 Answer 1

2

The best way is to forbid request caching directly from the server. Most probably you don't want the API calls to be cached anyway.

const router = require('express').Router();

// Set no cache headers to ensure browsers (especially IE) don't cache the API requests
router.use((req, res, next) => {
    res.setHeader('Expires', '-1');
    res.setHeader('Cache-Control', 'no-cache');
    next();
});

This should go on the top of your express app, s.t. all requests pass to this filter. Additionally, if you want some requests to be cached just write them above this handler.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.