Skip to content

Instantly share code, notes, and snippets.

@bolasblack
Last active September 19, 2018 08:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bolasblack/737d7e36aada2e950c36ed88304597f7 to your computer and use it in GitHub Desktop.
Save bolasblack/737d7e36aada2e950c36ed88304597f7 to your computer and use it in GitHub Desktop.
how to prevent bfcache
// modified from https://mixmax.com/blog/chrome-back-button-cache-no-store
/**
* Run this by downloading this script to your computer, then:
* 1. $ npm install express
* 2. $ node thisscript.js
* 3. Open localhost:8030/nocache and then localhost:8030/nostore
*/
const app = require('express')()
app.get('/nocache', function(req, res) {
res.setHeader('Cache-Control', 'no-cache')
res.send(`
${new Date().toString()}<br />
<div id="api"></div><br />
<script>fetch(location.pathname + '/api').then(r => r.text()).then(r => document.getElementById('api').textContent = r)</script>
<a href="https://mixmax.com">Click to navigate away and then press Back. It will show the same timestamp.</a>
`)
})
app.get('/nocache/api', function(req, res) {
res.setHeader('Cache-Control', 'no-cache')
res.send(new Date().toString())
})
app.get('/nostore', function(req, res) {
res.setHeader('Cache-Control', 'no-cache, no-store')
res.send(`
${new Date().toString()}<br />
<div id="api"></div><br />
<script>fetch(location.pathname + '/api').then(r => r.text()).then(r => document.getElementById('api').textContent = r)</script>
<a href="https://mixmax.com">Click to navigate away and then press Back. It will show the same timestamp.</a>
`)
})
app.get('/nostore/api', function(req, res) {
res.setHeader('Cache-Control', 'no-cache, no-store')
res.send(new Date().toString())
})
app.listen('8030')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment