[react-packager] Cache based on options, not url

Summary:
@public

We cached based on url, which wasn't unique becuase some options would be defaulted. This was obvious when starting the server via fbrnios which tries to warmup the bundle.
And then when the device woke up it will send a request (that is identical in reality) but would miss the cache.

This changes the cache key into a JSON stringification of the options.

Test Plan:
* ./runJestTests.sh
* ./fbrnios.sh run
This commit is contained in:
Amjad Masad 2015-06-19 15:14:39 -07:00
parent 85ac235691
commit 0405c82798
1 changed files with 13 additions and 11 deletions

View File

@ -131,13 +131,14 @@ Server.prototype._onFileChange = function(type, filepath, root) {
Server.prototype._rebuildPackages = function() { Server.prototype._rebuildPackages = function() {
var buildPackage = this.buildPackage.bind(this); var buildPackage = this.buildPackage.bind(this);
var packages = this._packages; var packages = this._packages;
Object.keys(packages).forEach(function(key) {
var options = getOptionsFromUrl(key); Object.keys(packages).forEach(function(optionsJson) {
var options = JSON.parse(optionsJson);
// Wait for a previous build (if exists) to finish. // Wait for a previous build (if exists) to finish.
packages[key] = (packages[key] || Promise.resolve()).finally(function() { packages[optionsJson] = (packages[optionsJson] || Promise.resolve()).finally(function() {
// With finally promise callback we can't change the state of the promise // With finally promise callback we can't change the state of the promise
// so we need to reassign the promise. // so we need to reassign the promise.
packages[key] = buildPackage(options).then(function(p) { packages[optionsJson] = buildPackage(options).then(function(p) {
// Make a throwaway call to getSource to cache the source string. // Make a throwaway call to getSource to cache the source string.
p.getSource({ p.getSource({
inlineSourceMap: options.inlineSourceMap, inlineSourceMap: options.inlineSourceMap,
@ -146,7 +147,7 @@ Server.prototype._rebuildPackages = function() {
return p; return p;
}); });
}); });
return packages[key]; return packages[optionsJson];
}); });
}; };
@ -228,9 +229,9 @@ Server.prototype._processDebugRequest = function(reqUrl, res) {
res.end(ret); res.end(ret);
} else if (parts[1] === 'packages') { } else if (parts[1] === 'packages') {
ret += '<h1> Cached Packages </h1>'; ret += '<h1> Cached Packages </h1>';
Promise.all(Object.keys(this._packages).map(function(url) { Promise.all(Object.keys(this._packages).map(function(optionsJson) {
return this._packages[url].then(function(p) { return this._packages[optionsJson].then(function(p) {
ret += '<div><h2>' + url + '</h2>'; ret += '<div><h2>' + optionsJson + '</h2>';
ret += p.getDebugInfo(); ret += p.getDebugInfo();
}); });
}, this)).then( }, this)).then(
@ -350,9 +351,10 @@ Server.prototype.processRequest = function(req, res, next) {
var startReqEventId = Activity.startEvent('request:' + req.url); var startReqEventId = Activity.startEvent('request:' + req.url);
var options = getOptionsFromUrl(req.url); var options = getOptionsFromUrl(req.url);
var building = this._packages[req.url] || this.buildPackage(options); var optionsJson = JSON.stringify(options);
var building = this._packages[optionsJson] || this.buildPackage(options);
this._packages[req.url] = building; this._packages[optionsJson] = building;
building.then( building.then(
function(p) { function(p) {
if (requestType === 'bundle') { if (requestType === 'bundle') {