Reload a page after res.redirect('back') in route

你说的曾经没有我的故事 提交于 2019-12-01 02:01:12

You should be able to do res.redirect('/album') instead of back to force a full reload but get the same type of feedback.

'back' is an alias for req.get('Referrer') so if '/albums' is your referrer you might still experience issues with the browser returning a 304 (http not modified) http status or a browser cached page (common). If you experience that issue you can do a couple of things:

Send some cache clearing headers:

res.header('Cache-Control', 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0');
res.redirect('back');

Or modify the url:

 var url = require('url');
 var u = url.parse(req.get('Referrer'), true, false);
 u.query['v'] = +new Date(); // add versioning to bust cache 
 delete u.search;
 res.redirect(url.format(u));  

Of course if you know the url such as '/albums' you don't have to go though all that rigamarole to add some versioning - just append current timestamp to the url string.

The first way is cleaner and works better imo. I've seen cases when a page doesn't have a referrer even though I clearly came from another page due to caching.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!