Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid a race between reading from and writing to the importScripts array #352

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions build-custom-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const webpack = require('webpack')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const TerserPlugin = require('terser-webpack-plugin')

const buildCustomWorker = ({ id, basedir, customWorkerDir, destdir, plugins, success, minify }) => {
const buildCustomWorker = ({ id, basedir, customWorkerDir, destdir, plugins, minify }) => {
let workerDir = undefined

if (fs.existsSync(path.join(basedir, customWorkerDir))) {
Expand All @@ -15,14 +15,19 @@ const buildCustomWorker = ({ id, basedir, customWorkerDir, destdir, plugins, suc
workerDir = path.join(basedir, 'src', customWorkerDir)
}

if (!workerDir) return
if (!workerDir) return null

const name = `worker-${id}.js`
const customWorkerEntries = ['ts', 'js']
.map(ext => path.join(workerDir, `index.${ext}`))
.filter(entry => fs.existsSync(entry))

if (customWorkerEntries.length !== 1) return
if (customWorkerEntries.length === 0) return null

if (customWorkerEntries.length > 1) {
console.warn(`> [PWA] WARNING: More than one custom worker found (${customWorkerEntries.join(",")}), not building a custom worker`)
return null
}

const customWorkerEntry = customWorkerEntries[0]
console.log(`> [PWA] Custom worker found: ${customWorkerEntry}`)
Expand Down Expand Up @@ -102,10 +107,10 @@ const buildCustomWorker = ({ id, basedir, customWorkerDir, destdir, plugins, suc
console.error(`> [PWA] Failed to build custom worker`)
console.error(status.toString({ colors: true }))
process.exit(-1)
} else {
success({ name })
}
})

return name
}

module.exports = buildCustomWorker
7 changes: 5 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,19 @@ module.exports = (nextConfig = {}) => ({

if (!options.isServer) {
const _dest = path.join(options.dir, dest)
buildCustomWorker({
const customWorkerScriptName = buildCustomWorker({
id: buildId,
basedir: options.dir,
customWorkerDir,
destdir: _dest,
plugins: config.plugins.filter(plugin => plugin instanceof webpack.DefinePlugin),
success: ({ name }) => importScripts.unshift(name),
minify: !dev
})

if (customWorkerScriptName !== null) {
importScripts.unshift(customWorkerScriptName)
}

if (register) {
console.log(`> [PWA] Auto register service worker with: ${path.resolve(registerJs)}`)
} else {
Expand Down