Use one entry per file in transpile cache

This avoids any risk of the cache key and contents getting out
of sync.
This commit is contained in:
Pierre Ossman 2017-05-22 14:01:44 +02:00
parent c23665dd82
commit 1433360ad9
3 changed files with 15 additions and 15 deletions

View File

@ -1390,12 +1390,12 @@ BrowserESModuleLoader.prototype[RegisterLoader$1.instantiate] = function(key, pr
}) })
.then(function(source) { .then(function(source) {
// check our cache first // check our cache first
const cacheEntryTrans = localStorage.getItem(key+'!transpiled'); var cacheEntry = localStorage.getItem(key);
if (cacheEntryTrans) { if (cacheEntry) {
const cacheEntryRaw = localStorage.getItem(key+'!raw'); cacheEntry = JSON.parse(cacheEntry);
// TODO: store a hash instead // TODO: store a hash instead
if (cacheEntryRaw === source) { if (cacheEntry.source === source) {
return Promise.resolve({key: key, code: cacheEntryTrans, source: source}); return Promise.resolve({key: key, code: cacheEntry.code, source: cacheEntry.source});
} }
} }
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
@ -1407,8 +1407,8 @@ BrowserESModuleLoader.prototype[RegisterLoader$1.instantiate] = function(key, pr
// we leave module in for now to allow module.require access // we leave module in for now to allow module.require access
if (data.key.slice(-8) !== '#nocache') { if (data.key.slice(-8) !== '#nocache') {
try { try {
localStorage.setItem(key+'!raw', data.source); var cacheEntry = JSON.stringify({source: data.source, code: data.code});
localStorage.setItem(data.key+'!transpiled', data.code); localStorage.setItem(key, cacheEntry);
} catch (e) { } catch (e) {
if (window.console) { if (window.console) {
window.console.warn('Unable to cache transpiled version of ' + key + ': ' + e); window.console.warn('Unable to cache transpiled version of ' + key + ': ' + e);

File diff suppressed because one or more lines are too long

View File

@ -230,12 +230,12 @@ BrowserESModuleLoader.prototype[RegisterLoader.instantiate] = function(key, proc
}) })
.then(function(source) { .then(function(source) {
// check our cache first // check our cache first
const cacheEntryTrans = localStorage.getItem(key+'!transpiled'); var cacheEntry = localStorage.getItem(key);
if (cacheEntryTrans) { if (cacheEntry) {
const cacheEntryRaw = localStorage.getItem(key+'!raw'); cacheEntry = JSON.parse(cacheEntry);
// TODO: store a hash instead // TODO: store a hash instead
if (cacheEntryRaw === source) { if (cacheEntry.source === source) {
return Promise.resolve({key: key, code: cacheEntryTrans, source: source}); return Promise.resolve({key: key, code: cacheEntry.code, source: cacheEntry.source});
} }
} }
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
@ -247,8 +247,8 @@ BrowserESModuleLoader.prototype[RegisterLoader.instantiate] = function(key, proc
// we leave module in for now to allow module.require access // we leave module in for now to allow module.require access
if (data.key.slice(-8) !== '#nocache') { if (data.key.slice(-8) !== '#nocache') {
try { try {
localStorage.setItem(key+'!raw', data.source); var cacheEntry = JSON.stringify({source: data.source, code: data.code});
localStorage.setItem(data.key+'!transpiled', data.code); localStorage.setItem(key, cacheEntry);
} catch (e) { } catch (e) {
if (window.console) { if (window.console) {
window.console.warn('Unable to cache transpiled version of ' + key + ': ' + e); window.console.warn('Unable to cache transpiled version of ' + key + ': ' + e);