TL;DR This appears to be a browser problem that is fixed in newer (58+) versions.
I'm working on a Node.js/express-based web server and I'm running into a problem where my browser (Firefox 57.0.1 on Mac OS) loads a page twice. I've been googling for tons and tons of other answers and blog posts, but haven't found a solution yet.
Obviously, apart from the double network traffic, this is a problem in an application that performs database work under the hood.
Reproducing the problem
Here's a simple script to replicate the issue:
#!/usr/bin/env node
const express = require('express');
const app = express();
app.disable('etag');
app.listen(3000, () => console.log('\n\n\n\n\nListening on port 3000!'));
app.all('/', function (req, res, next) {
rn=Math.random();
console.log('\n'+req.method+' '+req.url);
console.log('rn='+rn);
res.send('rn='+rn);
next();
});
For every request to /
, the server generates a random number and returns it to the client. That way, I can see (using console.log()
) how many requests are being made and which ones are being rendered in the browser.
When I request http://localhost:3000/, I get the following output in my console:
GET /
rn=0.3196834037080407
GET /
rn=0.8270968825090677
Full HTTP headers
Using Firefox' developer tools, I can see that two identical requests are being made. First request:
Accept: text/html,application/xhtml+xm…plication/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip, deflate
Accept-Language: en-US,sv-SE;q=0.5
Cache-Control: max-age=0
Connection: keep-alive
DNT: 1
Host: localhost:3000
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Macintosh; Intel …) Gecko/20100101 Firefox/57.0
First response:
Connection: keep-alive
Content-Length: 21
Content-Type: text/html; charset=utf-8
Date: Mon, 18 Dec 2017 05:44:51 GMT
X-Powered-By: Express
Second request:
Accept: text/html,application/xhtml+xm…plication/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip, deflate
Accept-Language: en-US,sv-SE;q=0.5
Cache-Control: no-cache
Connection: keep-alive
DNT: 1
Host: localhost:3000
Pragma: no-cache
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Macintosh; Intel …) Gecko/20100101 Firefox/57.0
The only difference between the first and the second requests are the Cache-Control
and Pragma
headers.
Second response:
Connection: keep-alive
Content-Length: 21
Content-Type: text/html; charset=utf-8
Date: Mon, 18 Dec 2017 05:44:51 GMT
X-Powered-By: Express
I can actually see Firefox render the first number and then immediately replace it with the second, as if it actually clicks the "Reload" button.
In the developer tools, on the "Stack trace" tab seems to confirm this. Here's the first request:
loadURI/< chrome://global/content/browser-child.js:354:14
_wrapURIChangeCall chrome://global/content/browser-child.js:308:7
loadURI chrome://global/content/browser-child.js:353:5
receiveMessage chrome://global/content/browser-child.js:288:9
.. and the second request:
reload chrome://global/content/browser-child.js:366:5
receiveMessage chrome://global/content/browser-child.js:297:9
Other things I've checked
Any pointers would be greatly appreciated.