Skip to content
This repository has been archived by the owner on Nov 20, 2019. It is now read-only.

Commit

Permalink
Only allow single slash at start of scriptUrl (#57)
Browse files Browse the repository at this point in the history
* Only allow single slash at start of scriptUrl.

Fixes default public path colliding with absolute filename.

* Use path.join() instead of string concat + regex replace.

* Add testcase for stripping extra slashes
  • Loading branch information
woutervanvliet committed Mar 16, 2018
1 parent e3d91ef commit 626c8f9
Show file tree
Hide file tree
Showing 9 changed files with 1,883 additions and 1,067 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules
/lib
/docs/dist
/flow/interfaces
/tmp-build
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
/node_modules
/lib
/docs/dist
.idea/
tmp-build/
4 changes: 3 additions & 1 deletion docs/src/sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ self.addEventListener('fetch', event => {
if (!responseNetwork || !responseNetwork.ok) {
if (DEBUG) {
console.log(
`[SW] URL [${requestUrl.toString()}] wrong responseNetwork: ${responseNetwork.status} ${responseNetwork.type}`
`[SW] URL [${requestUrl.toString()}] wrong responseNetwork: ${
responseNetwork.status
} ${responseNetwork.type}`
)
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"react": "^15.6.1",
"react-dom": "^15.6.1",
"react-hot-loader": "^3.0.0-beta.6",
"rimraf": "^2.6.2",
"sass-loader": "^6.0.6",
"style-loader": "^0.18.2",
"webpack": "^3.5.5",
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export default class ServiceWorkerPlugin {
// Hijack the original module
if (result.resource === runtimePath) {
const data = {
scriptURL: this.options.publicPath + this.options.filename,
scriptURL: path.join(this.options.publicPath, this.options.filename),
}

result.loaders.push(`${path.join(__dirname, 'runtimeLoader.js')}?${JSON.stringify(data)}`)
Expand Down
45 changes: 44 additions & 1 deletion src/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,41 @@
// @flow weak
/* eslint-env mocha */

import { assert } from 'chai'
import webpack from 'webpack'
import path from 'path'
import fs from 'fs'
import rimraf from 'rimraf'
import { assert, expect } from 'chai'
import ServiceWorkerPlugin from './index'

function trim(str) {
return str.replace(/^\s+|\s+$/, '')
}

const filename = 'sw.js'
const webpackOutputPath = path.resolve('./tmp-build')
const makeWebpackConfig = options => ({
entry: path.join(__dirname, '../test/test-build-entry'),
plugins: [
new ServiceWorkerPlugin({
entry: path.join(__dirname, '../test/test-build-sw'),
...options,
}),
],
resolve: {
alias: {
'serviceworker-webpack-plugin/lib/runtime': path.join(__dirname, 'runtime.js'),
},
},
output: {
path: webpackOutputPath,
},
})

describe('ServiceWorkerPlugin', () => {
beforeEach(done => {
return rimraf(webpackOutputPath, done)
})
describe('options: filename', () => {
it('should throw if trying to hash the filename', () => {
assert.throws(() => {
Expand All @@ -20,6 +45,24 @@ describe('ServiceWorkerPlugin', () => {
})
}, /The name of the/)
})
it('should strip double slashes', done => {
const options = makeWebpackConfig({
filename: '//sw.js',
})
return webpack(options, (err, stats) => {
expect(err).to.equal(null)
const { assetsByChunkName, errors, warnings } = stats.toJson()
expect(errors).to.have.length(0)
expect(warnings).to.have.length(0)

const mainFile = fs.readFileSync(
path.join(webpackOutputPath, assetsByChunkName.main),
'utf8'
)
expect(mainFile).to.include('var serviceWorkerOption = {"scriptURL":"/sw.js"}')
done()
})
})
})

describe('options: includes', () => {
Expand Down
3 changes: 3 additions & 0 deletions test/test-build-entry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import runtime from 'serviceworker-webpack-plugin/lib/runtime'

runtime.register({ scope: '/' })
1 change: 1 addition & 0 deletions test/test-build-sw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// intentionally left empty
Loading

0 comments on commit 626c8f9

Please sign in to comment.