Skip to content

Commit

Permalink
NekR#324, added tests, fixed implementation, do not set default shoul…
Browse files Browse the repository at this point in the history
…dServeFromNetwork
  • Loading branch information
BowlingX committed Dec 11, 2017
1 parent fe3be21 commit 4850042
Show file tree
Hide file tree
Showing 16 changed files with 177 additions and 14 deletions.
9 changes: 8 additions & 1 deletion lib/misc/sw-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,9 +358,16 @@ function WebpackServiceWorker(params, helpers) {
});
}

function shouldServeFromNetwork(response, urlString, cacheUrl) {
if (params.shouldServeFromNetwork) {
return params.shouldServeFromNetwork(response, urlString, cacheUrl);
}
return response.ok;
}

function networkFirstResponse(event, urlString, cacheUrl) {
return fetch(event.request).then(function (response) {
if (response.ok) {
if (shouldServeFromNetwork(response, urlString, cacheUrl)) {
if (DEBUG) {
console.log('[SW]:', 'URL [' + urlString + '] from network');
}
Expand Down
16 changes: 13 additions & 3 deletions lib/service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ var _deepExtend2 = _interopRequireDefault(_deepExtend);

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

var REGEX_MINIFY_FUNC = /(\r\n|\n|\r|\s+)/gm;

var ServiceWorker = (function () {
function ServiceWorker(options) {
_classCallCheck(this, ServiceWorker);
Expand All @@ -39,7 +41,7 @@ var ServiceWorker = (function () {
this.minify = options.minify;
this.output = options.output.replace(/^\.\/+/, '');
this.publicPath = options.publicPath;

this.shouldServeFromNetwork = options.shouldServeFromNetwork;
this.basePath = null;
this.location = null;
this.pathRewrite = null;
Expand Down Expand Up @@ -199,15 +201,17 @@ var ServiceWorker = (function () {

var loaders = Object.keys(plugin.loaders).length ? plugin.loaders : void 0;

return ('\n var ' + this.SW_DATA_VAR + ' = ' + JSON.stringify({
var shouldServeFromNetworkPlaceholder = this.shouldServeFromNetwork ? '__func_should_serve_from_network__' : undefined;
var functionResult = this.shouldServeFromNetwork && (minify ? this.shouldServeFromNetwork.toString().replace(REGEX_MINIFY_FUNC, '') : this.shouldServeFromNetwork.toString());
var result = ('\n var ' + this.SW_DATA_VAR + ' = ' + JSON.stringify({
assets: {
main: cache('main'),
additional: cache('additional'),
optional: cache('optional')
},

externals: externals,

shouldServeFromNetwork: shouldServeFromNetworkPlaceholder,
hashesMap: hashesMap,
navigateFallbackURL: this.navigateFallbackURL,
navigateFallbackForRedirects: this.navigateFallbackURL ? this.navigateFallbackForRedirects : void 0,
Expand All @@ -227,6 +231,12 @@ var ServiceWorker = (function () {
preferOnline: plugin.preferOnline,
ignoreSearch: plugin.ignoreSearch
}, null, minify ? void 0 : ' ') + ';\n ').trim();

if (functionResult) {
return result.replace('"' + shouldServeFromNetworkPlaceholder + '"', functionResult.trim());
}

return result;
}
}, {
key: 'getConfig',
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"author": "Arthur Stolyar <nekr.fabula@gmail.com>",
"license": "MIT",
"dependencies": {
"babel": "^5.8.38",
"deep-extend": "^0.4.0",
"ejs": "^2.3.4",
"loader-utils": "0.2.x",
Expand Down
1 change: 0 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ const defaultOptions = {
cache: void 0
},
minify: null,
shouldServeFromNetwork: (response) => response.ok,
navigateFallbackForRedirects: true
},

Expand Down
9 changes: 8 additions & 1 deletion src/misc/sw-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,10 +372,17 @@ function WebpackServiceWorker(params, helpers) {
});
}

function shouldServeFromNetwork(response, urlString, cacheUrl) {
if (params.shouldServeFromNetwork) {
return params.shouldServeFromNetwork(response, urlString, cacheUrl);
}
return response.ok;
}

function networkFirstResponse(event, urlString, cacheUrl) {
return fetch(event.request)
.then((response) => {
if (params.shouldServeFromNetwork(response, urlString, cacheUrl)) {
if (shouldServeFromNetwork(response, urlString, cacheUrl)) {
if (DEBUG) {
console.log('[SW]:', `URL [${ urlString }] from network`);
}
Expand Down
17 changes: 15 additions & 2 deletions src/service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import webpack from 'webpack';
import deepExtend from 'deep-extend';
import { getSource, pathToBase, isAbsoluteURL, isAbsolutePath } from './misc/utils';

const REGEX_MINIFY_FUNC = /(\r\n|\n|\r|\s+)/gm;

export default class ServiceWorker {
constructor(options) {
if (isAbsolutePath(options.output)) {
Expand Down Expand Up @@ -176,7 +178,12 @@ export default class ServiceWorker {
const loaders = Object.keys(plugin.loaders).length ?
plugin.loaders : void 0;

return `
const shouldServeFromNetworkPlaceholder =
this.shouldServeFromNetwork ? '__func_should_serve_from_network__' : undefined;
const functionResult = this.shouldServeFromNetwork &&
(minify ? this.shouldServeFromNetwork.toString().replace(REGEX_MINIFY_FUNC, '') :
this.shouldServeFromNetwork.toString());
const result = `
var ${ this.SW_DATA_VAR } = ${ JSON.stringify({
assets: {
main: cache('main'),
Expand All @@ -185,7 +192,7 @@ export default class ServiceWorker {
},
externals: externals,
shouldServeFromNetwork: this.shouldServeFromNetwork,
shouldServeFromNetwork: shouldServeFromNetworkPlaceholder,
hashesMap: hashesMap,
navigateFallbackURL: this.navigateFallbackURL,
navigateFallbackForRedirects: this.navigateFallbackURL ?
Expand All @@ -207,6 +214,12 @@ export default class ServiceWorker {
ignoreSearch: plugin.ignoreSearch,
}, null, minify ? void 0 : ' ') };
`.trim();

if (functionResult) {
return result.replace( `"${shouldServeFromNetworkPlaceholder}"`, functionResult.trim());
}

return result;
}

getConfig(plugin) {
Expand Down
9 changes: 8 additions & 1 deletion tests/legacy/fixtures/basic-wIth-sw-out/__expected/sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -465,9 +465,16 @@ var __wpo = {
});
}

function shouldServeFromNetwork(response, urlString, cacheUrl) {
if (params.shouldServeFromNetwork) {
return params.shouldServeFromNetwork(response, urlString, cacheUrl);
}
return response.ok;
}

function networkFirstResponse(event, urlString, cacheUrl) {
return fetch(event.request).then(function (response) {
if (response.ok) {
if (shouldServeFromNetwork(response, urlString, cacheUrl)) {
if (false) {
console.log('[SW]:', 'URL [' + urlString + '] from network');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -465,9 +465,16 @@ var __wpo = {
});
}

function shouldServeFromNetwork(response, urlString, cacheUrl) {
if (params.shouldServeFromNetwork) {
return params.shouldServeFromNetwork(response, urlString, cacheUrl);
}
return response.ok;
}

function networkFirstResponse(event, urlString, cacheUrl) {
return fetch(event.request).then(function (response) {
if (response.ok) {
if (shouldServeFromNetwork(response, urlString, cacheUrl)) {
if (false) {
console.log('[SW]:', 'URL [' + urlString + '] from network');
}
Expand Down
9 changes: 8 additions & 1 deletion tests/legacy/fixtures/cachemaps-basic/__expected/sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -461,9 +461,16 @@ var __wpo = {
});
}

function shouldServeFromNetwork(response, urlString, cacheUrl) {
if (params.shouldServeFromNetwork) {
return params.shouldServeFromNetwork(response, urlString, cacheUrl);
}
return response.ok;
}

function networkFirstResponse(event, urlString, cacheUrl) {
return fetch(event.request).then(function (response) {
if (response.ok) {
if (shouldServeFromNetwork(response, urlString, cacheUrl)) {
if (false) {
console.log('[SW]:', 'URL [' + urlString + '] from network');
}
Expand Down
9 changes: 8 additions & 1 deletion tests/legacy/fixtures/loaders-externals/__expected/sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -472,9 +472,16 @@ var __wpo = {
});
}

function shouldServeFromNetwork(response, urlString, cacheUrl) {
if (params.shouldServeFromNetwork) {
return params.shouldServeFromNetwork(response, urlString, cacheUrl);
}
return response.ok;
}

function networkFirstResponse(event, urlString, cacheUrl) {
return fetch(event.request).then(function (response) {
if (response.ok) {
if (shouldServeFromNetwork(response, urlString, cacheUrl)) {
if (false) {
console.log('[SW]:', 'URL [' + urlString + '] from network');
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};

/******/ // The require function
/******/ function __webpack_require__(moduleId) {

/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;

/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };

/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);

/******/ // Flag the module as loaded
/******/ module.loaded = true;

/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }


/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;

/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;

/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";

/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports) {



/***/ }
/******/ ]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var __wpo = {
"assets": {
"main": [
"./external.js"
],
"additional": [],
"optional": []
},
"externals": [
"./external.js"
],
"shouldServeFromNetwork": function (response, urlString, cacheUrl) {
if(urlString.match(/\//)) {
return true;
}
return response.ok;
},
"hashesMap": {},
"strategy": "changed",
"responseStrategy": "cache-first",
"version": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
"name": "webpack-offline",
"relativePaths": true
};
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = __CONFIG__({
caches: {
main: ['external.js', ':rest:']
},
externals: ['external.js'],
excludes: ['main.js'],
version: '[hash]',
AppCache: false,
ServiceWorker: {
shouldServeFromNetwork: function(response, urlString, cacheUrl) {
if(urlString.match(/\//)) {
return true;
}
return response.ok;
}
}
});
9 changes: 8 additions & 1 deletion tests/legacy/fixtures/sw-minify-false/__expected/sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -465,9 +465,16 @@ var __wpo = {
});
}

function shouldServeFromNetwork(response, urlString, cacheUrl) {
if (params.shouldServeFromNetwork) {
return params.shouldServeFromNetwork(response, urlString, cacheUrl);
}
return response.ok;
}

function networkFirstResponse(event, urlString, cacheUrl) {
return fetch(event.request).then(function (response) {
if (response.ok) {
if (shouldServeFromNetwork(response, urlString, cacheUrl)) {
if (false) {
console.log('[SW]:', 'URL [' + urlString + '] from network');
}
Expand Down
Loading

0 comments on commit 4850042

Please sign in to comment.