I'm trying to update my SailsJs application to fix the blank page wich I'm geting on Heroku, but I'm getting the status 304 not modified from browser.
I think I did everything I need to fix the blank page on my app, but I dont know why the page is not updating on Heroku
My Procfile:
web: node app.js
I appreciate if anyone can help.
Thanks!
I don't think that you need to fix anything. If you try to call the same url using POSTMAN or different browser you should get status 200.
You can add this to a policy instead and then just apply it to the specific pages/requests you want - if you do not want to set this for the entire application.
/api/policies/nocache.js:
/**
* Sets no-cache header in response.
*/
module.exports = function (req, res, next) {
sails.log.info("Applying disable cache policy");
res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate');
res.header('Expires', '-1');
res.header('Pragma', 'no-cache');
next();
};
Don't forget to include nocatche into config/policy.js
'*': 'nocatche'
Related
What is the best way to make sure all users get a fresh index.html instead of cached index.html?
So last week I tried to make the user cache the js and css bundles for a Single page application. I did it adding max age in the server.js file:
app.use(express.static('build', { maxAge: '365d' }));
The problem is that the index.html file is also in the build folder. Which means that it`s being cached client side for 365 days...
So even though I have cache busting on the js and css bundles, the index.html file is always the same and therefore the bundles stays the same because of the old hash.
I have now changed the server to use Etag instead:
app.use(express.static('build', { etag: true }));
This works great.
The problem is that i pushed this to production and now all the users have an stale index.html.
I use Node express and AWS Application Load balancer.
The endpoint which sends the index.html looks like this:
app.get('*', (request, response) => {
response.sendFile(path.join(__dirname, 'build/index.html'));
});
Does anyone have a smart solution?
I have looked into using a php file like this: https://github.com/facebook/create-react-app/issues/1910 . Is that the best solution?
I had a related problem where, after a deployment, users would need to hard refresh in certain browsers. It was due to blindly caching all build files, including the index.html.
Here's how we resolved it:
app
.use(express.static(BUILD, {
maxAge: '30 days',
setHeaders: (res, path) => {
if (express.static.mime.lookup(path) === 'text/html') {
// Skip cache on html to load new builds.
res.setHeader('Cache-Control', 'public, max-age=0');
}
}
}))
.get('/*', (req, res) => res.sendFile(HTML));
You can send header no-cache when using index.html route
For example:
app.get('/', (request, response) => {
response.header('Cache-Control', 'private, no-cache, no-store, must-revalidate');
response.header('Expires', '-1');
response.header('Pragma', 'no-cache');
response.sendFile(path.join(__dirname, 'build/index.html'));
});
I have a query regarding back button. As we know that after pressing logout button, if user presses back button than it must be redirect to the login page not to the authenticated page.
I tried to find the solution but can't fix it out in my case.
Thanks in Advance
Here is my routes.js screenshot:
When the user navigates back in the browser, the data is shown from the local browser cache, and not requested from your server.
Using the following setup when the browser back button, the page is reloaded and not cached.
// caching disabled for every route
server.use(function(req, res, next) {
res.set('Cache-Control', 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0');
next();
});
// otherwise put the res.set() call into the route-handler you want
You can use ensureLoggedIn
First install ensureLoggedIn by npm install
var ensureLoggedIn=require("connect-ensure-login").ensureLoggedIn
app.get('/profile', ensureLoggedIn('/login'),function(req,res){
res.sendFile(__dirname +'/public/selflocates.html');
})
I'm using Express and Node to build a web, where the client uses RESTful API to get Json response to show a list of objects.
After creating a new object, the app should request a new json response of the updated object list. Chrome works fine - new json response returned with status code 200 OK. However, things are not good in IE and Edge - it seems the browser just fetch the json response from cache (with status code 304), instead of making a new request. If I manually clear the browser cache data things will be fine.
I tried this solution: using a middleware to set max-age of cache-control res.header to be 0
function headerSet(req, res, next) {
res.header('Cache-Control', 'public, max-age=0');
return next();
}
And in the response header I can see accordant settings taking effect, however, IE and Edge still refuse to make a new request - I'm still getting the unupdated json response with 304.
What possibly have I done wrong?
Instead of setting max age try with the following.
res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate');
I'm working on a dynamic application where we don't want to cache HTML (i.e. cart contents can change from one page refresh to the next). To that end, I'm calling middleware that sets cache-control headers to avoid caching. However, said cache-control headers also apply when fetching static resources. For obvious performance reasons, this is undesired behavior. We def want to cache static resources. My question is this... Is there a way to set diff response headers for static resources vs rendered html? I tried passing the setHeaders option to the express.static middleware, but the thread hangs, presumably because we are trying to set the same response header twice. Any help is greatly appreciated!
Edit: adding environment information -
I'm on Express 4 and Node 4.4
Edit: adding example code. This is the relevant bit from app.js that aggressively avoids caching HTML in browser.
app.use(express.static(config.static.public));
// ...Stuff
app.use(function (req, res, next) {
// Don't cache html
res.set('Cache-Control', 'no-cache, private, no-store, must-revalidate, '
+ 'max-stale=0, post-check=0, pre-check=0');
res.set('Expires', 'Fri, 31 Dec 1998 12:00:00 GMT');
next();
});
app.use(express.static("static", {maxage : 0}))
more info
Maybe clear all ready cached files in the browser before testing.
I have an application on node.js using sails.js as a framework. For displaying the informations to the user I have implemented some .ejs files.
When navigating using links within the app menu, I receive a "304 - Nod Modified" response. Also, within headers in the response I see Etag, If-None-Match or Expires.
After reading some posts, I have added the "app.disable('etag')" statement within /config/express.js file in the customMiddleware function and etag and if-none-match are not sent anymore.
Anyway, now I do not see any request made to the server (just the first one is made) when accessing different pages (not even 304 response).
How can I tell sails.js to stop caching my pages?
You can add this to a policy instead and then just apply it to the specific pages/requests you want - if you do not want to set this for the entire application.
/api/policies/nocache.js:
/**
* Sets no-cache header in response.
*/
module.exports = function (req, res, next) {
sails.log.info("Applying disable cache policy");
res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate');
res.header('Expires', '-1');
res.header('Pragma', 'no-cache');
next();
};
It seems that adding the code below solved my problem:
res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate');
res.header('Expires', '-1');
res.header('Pragma', 'no-cache');
These line should be included in the customMiddleware function from the /config/express.js file.
If there is a cleaner way to achieve the same result, please reply.