Use <base href> tag to load from persistent storage.

This lets us use the correct path for assets in the `public` folder.
This commit is contained in:
Matthew Arbesfeld
2014-08-27 15:33:00 -07:00
parent 124b0d869e
commit bd86ed3450
3 changed files with 21 additions and 4 deletions

View File

@@ -90,17 +90,20 @@ var onNewVersion = function () {
_.each(manifest, function (item) {
if (! item.url) return;
var uri = encodeURI(urlPrefix + item.url);
// Add a cache buster to ensure that we don't cache an old asset.
var uri = encodeURI(urlPrefix + item.url + '?' + Random.id());
// Try to dowload the file a few times.
var tries = 0;
var tryDownload = function () {
console.log('downloading', uri)
ft.download(uri, versionPrefix + item.url, function (entry) {
if (entry) {
afterAllFilesDownloaded();
}
}, function (err) {
// It failed, try again if we have tries less than 5 times.
// It failed, try again if we have tried less than 5 times.
if (tries++ < 5) {
tryDownload();
} else {

View File

@@ -10,7 +10,7 @@ Cordova.depends({
Package.on_use(function (api) {
api.use('webapp', 'server');
api.use(['deps', 'retry'], 'client');
api.use(['deps', 'retry', 'random'], 'client');
api.use(['livedata', 'mongo-livedata', 'underscore'], ['client', 'server']);
api.use('deps', 'client');
api.use('reload', 'client', {weak: true});

View File

@@ -93,9 +93,23 @@
document.getElementsByTagName('head')[0].appendChild(styleTag);
};
var stripLeadingSlash = function (p) {
if (p.charAt(0) !== '/')
throw new Error("bad path: " + p);
return p.slice(1);
};
var loadAssetsFromManifest = function (manifest, urlPrefix) {
// Set the base href so that relative paths point to the correct version
// of the app.
var newBase = document.createElement("base");
newBase.setAttribute("href", urlPrefix);
document.getElementsByTagName("head")[0].appendChild(newBase);
each(manifest, function (item) {
var url = urlPrefix + (item.url ? item.url.substring(1) : '');
// We want to use relative paths so that our base href is taken into
// account.
var url = item.url ? stripLeadingSlash(item.url) : '';
if (item.type === 'js')
queue.push(function () {
loadScript(url);