Skip to content

Commit

Permalink
Add ServiceWorker.updateViaCache and ServiceWorker.forceInstall options
Browse files Browse the repository at this point in the history
  • Loading branch information
NekR committed Apr 8, 2018
1 parent be4d744 commit 39acf1f
Show file tree
Hide file tree
Showing 29 changed files with 193 additions and 114 deletions.
2 changes: 1 addition & 1 deletion lib/app-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ var AppCache = (function () {
}, {
key: 'getPageTemplate',
value: function getPageTemplate(name, content) {
return ('\n <!doctype html>\n <html manifest="' + name + '.appcache">' + (content || '') + '</html>\n ').trim().replace(/^ */gm, '');
return ('\n <!doctype html>\n <html manifest="' + name + '.appcache"><meta charset="utf-8">' + (content || '') + '</html>\n ').trim().replace(/^ */gm, '');
}
}, {
key: 'getPageContent',
Expand Down
6 changes: 6 additions & 0 deletions lib/default-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ var _path2 = _interopRequireDefault(_path);

var _miscUtils = require('./misc/utils');

var DEFAULT_AUTO_UPDATE_INTERVAL = 3600000;

exports.DEFAULT_AUTO_UPDATE_INTERVAL = DEFAULT_AUTO_UPDATE_INTERVAL;
var AppCacheOptions = {
NETWORK: '*',
FALLBACK: null,
Expand Down Expand Up @@ -53,6 +56,9 @@ exports['default'] = {
scope: null,
events: false,
minify: null,
forceInstall: false,

updateViaCache: 'imports',

prefetchRequest: {
credentials: 'same-origin',
Expand Down
4 changes: 1 addition & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ var _require = require('../package.json');

var pluginVersion = _require.version;

var AUTO_UPDATE_INTERVAL = 3600000;

var hasOwn = ({}).hasOwnProperty;
var updateStrategies = ['all', 'hash', 'changed'];

Expand Down Expand Up @@ -98,7 +96,7 @@ var OfflinePlugin = (function () {
var autoUpdate = this.options.autoUpdate;

if (autoUpdate === true) {
this.autoUpdate = AUTO_UPDATE_INTERVAL;
this.autoUpdate = _defaultOptions.DEFAULT_AUTO_UPDATE_INTERVAL;
} else if (typeof autoUpdate === 'number' && autoUpdate) {
this.autoUpdate = autoUpdate;
}
Expand Down
6 changes: 5 additions & 1 deletion lib/service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ var ServiceWorker = (function () {
this.navigateFallbackURL = options.navigateFallbackURL;
this.navigateFallbackForRedirects = options.navigateFallbackForRedirects;
this.prefetchRequest = this.validatePrefetch(options.prefetchRequest);
this.updateViaCache = (options.updateViaCache || '') + '';
this.navigationPreload = options.navigationPreload;
this.forceInstall = !!options.forceInstall;

var cacheNameQualifier = '';

Expand Down Expand Up @@ -251,7 +253,9 @@ var ServiceWorker = (function () {
return {
location: this.location,
scope: this.scope,
events: this.events
updateViaCache: this.updateViaCache,
events: this.events,
force: this.forceInstall
};
}
}, {
Expand Down
5 changes: 5 additions & 0 deletions src/default-options.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import path from 'path';
import { isAbsoluteURL } from './misc/utils';

export const DEFAULT_AUTO_UPDATE_INTERVAL = 3600000;

export const AppCacheOptions = {
NETWORK: '*',
FALLBACK: null,
Expand Down Expand Up @@ -41,6 +43,9 @@ export default {
scope: null,
events: false,
minify: null,
forceInstall: false,

updateViaCache: 'imports',

prefetchRequest: {
credentials: 'same-origin',
Expand Down
8 changes: 5 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import AppCache from './app-cache';
import ServiceWorker from './service-worker';
import defaultOptions,
{ AppCacheOptions as defaultAppCacheOptions } from './default-options';
{
AppCacheOptions as defaultAppCacheOptions,
DEFAULT_AUTO_UPDATE_INTERVAL
} from './default-options';

import {
hasMagic, interpolateString,
Expand All @@ -17,7 +20,6 @@ import loaderUtils from 'loader-utils';
import slash from 'slash';

const { version: pluginVersion } = require('../package.json');
const AUTO_UPDATE_INTERVAL = 3600000;

const hasOwn = {}.hasOwnProperty;
const updateStrategies = ['all', 'hash', 'changed'];
Expand Down Expand Up @@ -59,7 +61,7 @@ export default class OfflinePlugin {
const autoUpdate = this.options.autoUpdate;

if (autoUpdate === true) {
this.autoUpdate = AUTO_UPDATE_INTERVAL;
this.autoUpdate = DEFAULT_AUTO_UPDATE_INTERVAL;
} else if (typeof autoUpdate === 'number' && autoUpdate) {
this.autoUpdate = autoUpdate;
}
Expand Down
6 changes: 5 additions & 1 deletion src/service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ export default class ServiceWorker {
this.navigateFallbackURL = options.navigateFallbackURL;
this.navigateFallbackForRedirects = options.navigateFallbackForRedirects;
this.prefetchRequest = this.validatePrefetch(options.prefetchRequest);
this.updateViaCache = (options.updateViaCache || '') + '';
this.navigationPreload = options.navigationPreload;
this.forceInstall = !!options.forceInstall;

let cacheNameQualifier = '';

Expand Down Expand Up @@ -234,7 +236,9 @@ export default class ServiceWorker {
return {
location: this.location,
scope: this.scope,
events: this.events
updateViaCache: this.updateViaCache,
events: this.events,
force: this.forceInstall
};
}

Expand Down
2 changes: 1 addition & 1 deletion tests/legacy/compare.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var log = console.log;

var options = {
compareContent: true,
excludeFilter: '.DS_Store'
excludeFilter: '.DS_Store,manifest.html'
};

function logFile(file, state) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,13 @@
var appCacheIframe;

function hasSW() {
return 'serviceWorker' in navigator &&
// This is how I block Chrome 40 and detect Chrome 41, because first has
// bugs with history.pustState and/or hashchange
(window.fetch || 'imageRendering' in document.documentElement.style) &&
(window.location.protocol === 'https:' || window.location.hostname === 'localhost' || window.location.hostname.indexOf('127.') === 0)

return 'serviceWorker' in navigator && (
window.location.protocol === 'https:' ||
window.location.hostname === 'localhost' ||
window.location.hostname.indexOf('127.') === 0
);

}

function install(options) {
Expand All @@ -87,8 +89,10 @@ function install(options) {
if (hasSW()) {
var registration = navigator.serviceWorker
.register(
"sw.js"

"sw.js", {


}
);


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,13 @@ __webpack_require__(1);
var appCacheIframe;

function hasSW() {
return 'serviceWorker' in navigator &&
// This is how I block Chrome 40 and detect Chrome 41, because first has
// bugs with history.pustState and/or hashchange
(window.fetch || 'imageRendering' in document.documentElement.style) &&
(window.location.protocol === 'https:' || window.location.hostname === 'localhost' || window.location.hostname.indexOf('127.') === 0)

return 'serviceWorker' in navigator && (
window.location.protocol === 'https:' ||
window.location.hostname === 'localhost' ||
window.location.hostname.indexOf('127.') === 0
);

}

function install(options) {
Expand All @@ -90,8 +92,10 @@ function install(options) {
if (hasSW()) {
var registration = navigator.serviceWorker
.register(
"sw.js"

"sw.js", {


}
);


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,13 @@ __webpack_require__(1);
var appCacheIframe;

function hasSW() {
return 'serviceWorker' in navigator &&
// This is how I block Chrome 40 and detect Chrome 41, because first has
// bugs with history.pustState and/or hashchange
(window.fetch || 'imageRendering' in document.documentElement.style) &&
(window.location.protocol === 'https:' || window.location.hostname === 'localhost' || window.location.hostname.indexOf('127.') === 0)

return 'serviceWorker' in navigator && (
window.location.protocol === 'https:' ||
window.location.hostname === 'localhost' ||
window.location.hostname.indexOf('127.') === 0
);

}

function install(options) {
Expand All @@ -96,8 +98,10 @@ function install(options) {
if (hasSW()) {
var registration = navigator.serviceWorker
.register(
"sw.js"

"sw.js", {


}
);


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,13 @@
var appCacheIframe;

function hasSW() {
return 'serviceWorker' in navigator &&
// This is how I block Chrome 40 and detect Chrome 41, because first has
// bugs with history.pustState and/or hashchange
(window.fetch || 'imageRendering' in document.documentElement.style) &&
(window.location.protocol === 'https:' || window.location.hostname === 'localhost' || window.location.hostname.indexOf('127.') === 0)

return 'serviceWorker' in navigator && (
window.location.protocol === 'https:' ||
window.location.hostname === 'localhost' ||
window.location.hostname.indexOf('127.') === 0
);

}

function install(options) {
Expand All @@ -87,8 +89,10 @@ function install(options) {
if (hasSW()) {
var registration = navigator.serviceWorker
.register(
"sw.js"

"sw.js", {


}
);


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,13 @@ __webpack_require__(1);
var appCacheIframe;

function hasSW() {
return 'serviceWorker' in navigator &&
// This is how I block Chrome 40 and detect Chrome 41, because first has
// bugs with history.pustState and/or hashchange
(window.fetch || 'imageRendering' in document.documentElement.style) &&
(window.location.protocol === 'https:' || window.location.hostname === 'localhost' || window.location.hostname.indexOf('127.') === 0)

return 'serviceWorker' in navigator && (
window.location.protocol === 'https:' ||
window.location.hostname === 'localhost' ||
window.location.hostname.indexOf('127.') === 0
);

}

function install(options) {
Expand All @@ -90,8 +92,10 @@ function install(options) {
if (hasSW()) {
var registration = navigator.serviceWorker
.register(
"sw.js"

"sw.js", {


}
);


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,13 @@ __webpack_require__(1);
var appCacheIframe;

function hasSW() {
return 'serviceWorker' in navigator &&
// This is how I block Chrome 40 and detect Chrome 41, because first has
// bugs with history.pustState and/or hashchange
(window.fetch || 'imageRendering' in document.documentElement.style) &&
(window.location.protocol === 'https:' || window.location.hostname === 'localhost' || window.location.hostname.indexOf('127.') === 0)

return 'serviceWorker' in navigator && (
window.location.protocol === 'https:' ||
window.location.hostname === 'localhost' ||
window.location.hostname.indexOf('127.') === 0
);

}

function install(options) {
Expand All @@ -96,8 +98,10 @@ function install(options) {
if (hasSW()) {
var registration = navigator.serviceWorker
.register(
"sw.js"

"sw.js", {


}
);


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,13 @@
var appCacheIframe;

function hasSW() {
return 'serviceWorker' in navigator &&
// This is how I block Chrome 40 and detect Chrome 41, because first has
// bugs with history.pustState and/or hashchange
(window.fetch || 'imageRendering' in document.documentElement.style) &&
(window.location.protocol === 'https:' || window.location.hostname === 'localhost' || window.location.hostname.indexOf('127.') === 0)

return 'serviceWorker' in navigator && (
window.location.protocol === 'https:' ||
window.location.hostname === 'localhost' ||
window.location.hostname.indexOf('127.') === 0
);

}

function install(options) {
Expand All @@ -87,8 +89,10 @@ function install(options) {
if (hasSW()) {
var registration = navigator.serviceWorker
.register(
"sw.js"

"sw.js", {


}
);


Expand Down
Loading

0 comments on commit 39acf1f

Please sign in to comment.