Skip to content

Commit

Permalink
code format
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowwalker committed Jun 6, 2022
1 parent 86a4c18 commit 1801bcb
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 60 deletions.
6 changes: 5 additions & 1 deletion build-custom-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ const buildCustomWorker = ({ id, basedir, customWorkerDir, destdir, plugins, min
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`)
console.warn(
`> [PWA] WARNING: More than one custom worker found (${customWorkerEntries.join(
','
)}), not building a custom worker`
)
return null
}

Expand Down
51 changes: 29 additions & 22 deletions build-fallback-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 getFallbackEnvs = ({fallbacks, basedir, id, pageExtensions}) => {
const getFallbackEnvs = ({ fallbacks, basedir, id, pageExtensions }) => {
let { document, data } = fallbacks

if (!document) {
Expand All @@ -30,8 +30,8 @@ const getFallbackEnvs = ({fallbacks, basedir, id, pageExtensions}) => {

if (data && data.endsWith('.json')) {
data = path.posix.join('/_next/data', id, data)
}
}

const envs = {
__PWA_FALLBACK_DOCUMENT__: document || false,
__PWA_FALLBACK_IMAGE__: fallbacks.image || false,
Expand All @@ -50,12 +50,12 @@ const getFallbackEnvs = ({fallbacks, basedir, id, pageExtensions}) => {
if (envs.__PWA_FALLBACK_VIDEO__) console.log(`> [PWA] video: ${envs.__PWA_FALLBACK_VIDEO__}`)
if (envs.__PWA_FALLBACK_FONT__) console.log(`> [PWA] font: ${envs.__PWA_FALLBACK_FONT__}`)
if (envs.__PWA_FALLBACK_DATA__) console.log(`> [PWA] data (/_next/data/**/*.json): ${envs.__PWA_FALLBACK_DATA__}`)

return envs
}

const buildFallbackWorker = ({ id, fallbacks, basedir, destdir, success, minify, pageExtensions }) => {
const envs = getFallbackEnvs({fallbacks, basedir, id, pageExtensions})
const envs = getFallbackEnvs({ fallbacks, basedir, id, pageExtensions })
if (!envs) return false

const name = `fallback-${id}.js`
Expand Down Expand Up @@ -93,18 +93,23 @@ const buildFallbackWorker = ({ id, fallbacks, basedir, destdir, success, minify,
{
loader: 'babel-loader',
options: {
presets: [['next/babel', {
'transform-runtime': {
corejs: false,
helpers: true,
regenerator: false,
useESModules: true
},
'preset-env': {
modules: false,
targets: 'chrome >= 56'
}
}]]
presets: [
[
'next/babel',
{
'transform-runtime': {
corejs: false,
helpers: true,
regenerator: false,
useESModules: true
},
'preset-env': {
modules: false,
targets: 'chrome >= 56'
}
}
]
]
}
}
]
Expand All @@ -121,17 +126,19 @@ const buildFallbackWorker = ({ id, fallbacks, basedir, destdir, success, minify,
}),
new webpack.EnvironmentPlugin(envs)
],
optimization: minify ? {
minimize: true,
minimizer: [new TerserPlugin()]
} : undefined
optimization: minify
? {
minimize: true,
minimizer: [new TerserPlugin()]
}
: undefined
}).run((error, status) => {
if (error || status.hasErrors()) {
console.error(`> [PWA] Failed to build fallback worker`)
console.error(status.toString({ colors: true }))
process.exit(-1)
} else {
success({name, precaches: Object.values(envs).filter(v => !!v)})
success({ name, precaches: Object.values(envs).filter(v => !!v) })
}
})

Expand Down
36 changes: 18 additions & 18 deletions cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ module.exports = [
cacheName: 'google-fonts-webfonts',
expiration: {
maxEntries: 4,
maxAgeSeconds: 365 * 24 * 60 * 60, // 365 days
},
},
maxAgeSeconds: 365 * 24 * 60 * 60 // 365 days
}
}
},
{
urlPattern: /^https:\/\/fonts\.(?:googleapis)\.com\/.*/i,
Expand All @@ -20,9 +20,9 @@ module.exports = [
cacheName: 'google-fonts-stylesheets',
expiration: {
maxEntries: 4,
maxAgeSeconds: 7 * 24 * 60 * 60, // 7 days
},
},
maxAgeSeconds: 7 * 24 * 60 * 60 // 7 days
}
}
},
{
urlPattern: /\.(?:eot|otf|ttc|ttf|woff|woff2|font.css)$/i,
Expand All @@ -48,14 +48,14 @@ module.exports = [
},
{
urlPattern: /\/_next\/image\?url=.+$/i,
handler: "StaleWhileRevalidate",
handler: 'StaleWhileRevalidate',
options: {
cacheName: "next-image",
cacheName: 'next-image',
expiration: {
maxEntries: 64,
maxAgeSeconds: 24 * 60 * 60, // 24 hours
},
},
maxAgeSeconds: 24 * 60 * 60 // 24 hours
}
}
},
{
urlPattern: /\.(?:mp3|wav|ogg)$/i,
Expand Down Expand Up @@ -105,14 +105,14 @@ module.exports = [
},
{
urlPattern: /\/_next\/data\/.+\/.+\.json$/i,
handler: "StaleWhileRevalidate",
handler: 'StaleWhileRevalidate',
options: {
cacheName: "next-data",
cacheName: 'next-data',
expiration: {
maxEntries: 32,
maxAgeSeconds: 24 * 60 * 60, // 24 hours
maxAgeSeconds: 24 * 60 * 60 // 24 hours
}
},
}
},
{
urlPattern: /\.(?:json|xml|csv)$/i,
Expand All @@ -126,7 +126,7 @@ module.exports = [
}
},
{
urlPattern: ({url}) => {
urlPattern: ({ url }) => {
const isSameOrigin = self.origin === url.origin
if (!isSameOrigin) return false
const pathname = url.pathname
Expand All @@ -149,7 +149,7 @@ module.exports = [
}
},
{
urlPattern: ({url}) => {
urlPattern: ({ url }) => {
const isSameOrigin = self.origin === url.origin
if (!isSameOrigin) return false
const pathname = url.pathname
Expand All @@ -167,7 +167,7 @@ module.exports = [
}
},
{
urlPattern: ({url}) => {
urlPattern: ({ url }) => {
const isSameOrigin = self.origin === url.origin
return !isSameOrigin
},
Expand Down
44 changes: 25 additions & 19 deletions fallback.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
'use strict'

self.fallback = async (request) => {
// https://developer.mozilla.org/en-US/docs/Web/API/RequestDestination
switch (request.destination) {
case 'document':
if (process.env.__PWA_FALLBACK_DOCUMENT__) return caches.match(process.env.__PWA_FALLBACK_DOCUMENT__, {ignoreSearch: true})
case 'image':
if (process.env.__PWA_FALLBACK_IMAGE__) return caches.match(process.env.__PWA_FALLBACK_IMAGE__, {ignoreSearch: true})
case 'audio':
if (process.env.__PWA_FALLBACK_AUDIO__) return caches.match(process.env.__PWA_FALLBACK_AUDIO__, {ignoreSearch: true})
case 'video':
if (process.env.__PWA_FALLBACK_VIDEO__) return caches.match(process.env.__PWA_FALLBACK_VIDEO__, {ignoreSearch: true})
case 'font':
if (process.env.__PWA_FALLBACK_FONT__) return caches.match(process.env.__PWA_FALLBACK_FONT__, {ignoreSearch: true})
case '':
if (process.env.__PWA_FALLBACK_DATA__ && request.url.match(/\/_next\/data\/.+\/.+\.json$/i)) return caches.match(process.env.__PWA_FALLBACK_DATA__, {ignoreSearch: true})
default:
return Response.error()
};
}
self.fallback = async request => {
// https://developer.mozilla.org/en-US/docs/Web/API/RequestDestination
switch (request.destination) {
case 'document':
if (process.env.__PWA_FALLBACK_DOCUMENT__)
return caches.match(process.env.__PWA_FALLBACK_DOCUMENT__, { ignoreSearch: true })
case 'image':
if (process.env.__PWA_FALLBACK_IMAGE__)
return caches.match(process.env.__PWA_FALLBACK_IMAGE__, { ignoreSearch: true })
case 'audio':
if (process.env.__PWA_FALLBACK_AUDIO__)
return caches.match(process.env.__PWA_FALLBACK_AUDIO__, { ignoreSearch: true })
case 'video':
if (process.env.__PWA_FALLBACK_VIDEO__)
return caches.match(process.env.__PWA_FALLBACK_VIDEO__, { ignoreSearch: true })
case 'font':
if (process.env.__PWA_FALLBACK_FONT__)
return caches.match(process.env.__PWA_FALLBACK_FONT__, { ignoreSearch: true })
case '':
if (process.env.__PWA_FALLBACK_DATA__ && request.url.match(/\/_next\/data\/.+\/.+\.json$/i))
return caches.match(process.env.__PWA_FALLBACK_DATA__, { ignoreSearch: true })
default:
return Response.error()
}
}

0 comments on commit 1801bcb

Please sign in to comment.