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

前端 未结 2 1974
一向
一向 2021-01-07 10:19

I\'m working on an application which allows you to upload images, and create albums. Everything works fine accept that after an album is created the new album isn\'t shown i

2条回答
  •  迷失自我
    2021-01-07 10:58

    '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.

提交回复
热议问题