Skip to content

Updated libraries #510

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2019 ShadowWalker [email protected] https://weiw.io
Copyright (c) 2024 Red-misst

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
130 changes: 30 additions & 100 deletions README.md

Large diffs are not rendered by default.

50 changes: 25 additions & 25 deletions build-custom-worker.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
'use strict'
'use strict';

const path = require('path')
const fs = require('fs')
const webpack = require('webpack')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const TerserPlugin = require('terser-webpack-plugin')
import path from 'path';
import fs from 'fs';
import webpack from 'webpack';
import { CleanWebpackPlugin } from 'clean-webpack-plugin';
import TerserPlugin from 'terser-webpack-plugin';

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

if (fs.existsSync(path.join(basedir, customWorkerDir))) {
workerDir = path.join(basedir, customWorkerDir)
workerDir = path.join(basedir, customWorkerDir);
} else if (fs.existsSync(path.join(basedir, 'src', customWorkerDir))) {
workerDir = path.join(basedir, 'src', customWorkerDir)
workerDir = path.join(basedir, 'src', customWorkerDir);
}

if (!workerDir) return
if (!workerDir) return;

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

if (customWorkerEntries.length === 0) return
if (customWorkerEntries.length === 0) return;

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

const customWorkerEntry = customWorkerEntries[0]
console.log(`> [PWA] Custom worker found: ${customWorkerEntry}`)
console.log(`> [PWA] Build custom worker: ${path.join(destdir, name)}`)
const customWorkerEntry = customWorkerEntries[0];
console.log(`> [PWA] Custom worker found: ${customWorkerEntry}`);
console.log(`> [PWA] Build custom worker: ${path.join(destdir, name)}`);
webpack({
mode: 'none',
target: 'webworker',
Expand Down Expand Up @@ -108,13 +108,13 @@ const buildCustomWorker = ({ id, basedir, customWorkerDir, destdir, plugins, min
: undefined
}).run((error, status) => {
if (error || status.hasErrors()) {
console.error(`> [PWA] Failed to build custom worker`)
console.error(status.toString({ colors: true }))
process.exit(-1)
console.error(`> [PWA] Failed to build custom worker`);
console.error(status.toString({ colors: true }));
process.exit(-1);
}
})
});

return name
}
return name;
};

module.exports = buildCustomWorker
export default buildCustomWorker;
112 changes: 56 additions & 56 deletions build-fallback-worker.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
'use strict'
'use strict';

const path = require('path')
const fs = require('fs')
const webpack = require('webpack')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const TerserPlugin = require('terser-webpack-plugin')
import path from 'path';
import fs from 'fs';
import webpack from 'webpack';
import { CleanWebpackPlugin } from 'clean-webpack-plugin';
import TerserPlugin from 'terser-webpack-plugin';

const getFallbackEnvs = ({ fallbacks, basedir, id, pageExtensions }) => {
let { document, data } = fallbacks
let { document, data } = fallbacks;

if (!document) {
let pagesDir = undefined
let pagesDir = undefined;

if (fs.existsSync(path.join(basedir, 'pages'))) {
pagesDir = path.join(basedir, 'pages')
pagesDir = path.join(basedir, 'pages');
} else if (fs.existsSync(path.join(basedir, 'src', 'pages'))) {
pagesDir = path.join(basedir, 'src', 'pages')
pagesDir = path.join(basedir, 'src', 'pages');
}

if (!pagesDir) return
if (!pagesDir) return;

const offlines = pageExtensions
.map(ext => path.join(pagesDir, `_offline.${ext}`))
.filter(entry => fs.existsSync(entry))
.filter(entry => fs.existsSync(entry));
if (offlines.length === 1) {
document = '/_offline'
document = '/_offline';
}
}

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

const envs = {
Expand All @@ -38,34 +38,34 @@ const getFallbackEnvs = ({ fallbacks, basedir, id, pageExtensions }) => {
__PWA_FALLBACK_AUDIO__: fallbacks.audio || false,
__PWA_FALLBACK_VIDEO__: fallbacks.video || false,
__PWA_FALLBACK_FONT__: fallbacks.font || false,
__PWA_FALLBACK_DATA__: data || false
}
__PWA_FALLBACK_DATA__: data || false,
};

if (Object.values(envs).filter(v => !!v).length === 0) return
if (Object.values(envs).filter(v => !!v).length === 0) return;

console.log('> [PWA] Fallback to precache routes when fetch failed from cache or network:')
if (envs.__PWA_FALLBACK_DOCUMENT__) console.log(`> [PWA] document (page): ${envs.__PWA_FALLBACK_DOCUMENT__}`)
if (envs.__PWA_FALLBACK_IMAGE__) console.log(`> [PWA] image: ${envs.__PWA_FALLBACK_IMAGE__}`)
if (envs.__PWA_FALLBACK_AUDIO__) console.log(`> [PWA] audio: ${envs.__PWA_FALLBACK_AUDIO__}`)
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__}`)
console.log('> [PWA] Fallback to precache routes when fetch failed from cache or network:');
if (envs.__PWA_FALLBACK_DOCUMENT__) console.log(`> [PWA] document (page): ${envs.__PWA_FALLBACK_DOCUMENT__}`);
if (envs.__PWA_FALLBACK_IMAGE__) console.log(`> [PWA] image: ${envs.__PWA_FALLBACK_IMAGE__}`);
if (envs.__PWA_FALLBACK_AUDIO__) console.log(`> [PWA] audio: ${envs.__PWA_FALLBACK_AUDIO__}`);
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
}
return envs;
};

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

const name = `fallback-${id}.js`
const fallbackJs = path.join(__dirname, `fallback.js`)
const name = `fallback-${id}.js`;
const fallbackJs = path.join(__dirname, `fallback.js`);

webpack({
mode: 'none',
target: 'webworker',
entry: {
main: fallbackJs
main: fallbackJs,
},
resolve: {
extensions: ['.js'],
Expand All @@ -82,8 +82,8 @@ const buildFallbackWorker = ({ id, fallbacks, basedir, destdir, minify, pageExte
net: false,
tls: false,
zlib: false,
child_process: false
}
child_process: false,
},
},
module: {
rules: [
Expand All @@ -101,46 +101,46 @@ const buildFallbackWorker = ({ id, fallbacks, basedir, destdir, minify, pageExte
corejs: false,
helpers: true,
regenerator: false,
useESModules: true
useESModules: true,
},
'preset-env': {
modules: false,
targets: 'chrome >= 56'
}
}
]
]
}
}
]
}
]
targets: 'chrome >= 56',
},
},
],
],
},
},
],
},
],
},
output: {
path: destdir,
filename: name
filename: name,
},
plugins: [
new CleanWebpackPlugin({
cleanOnceBeforeBuildPatterns: [path.join(destdir, 'fallback-*.js'), path.join(destdir, 'fallback-*.js.map')]
cleanOnceBeforeBuildPatterns: [path.join(destdir, 'fallback-*.js'), path.join(destdir, 'fallback-*.js.map')],
}),
new webpack.EnvironmentPlugin(envs)
new webpack.EnvironmentPlugin(envs),
],
optimization: minify
? {
minimize: true,
minimizer: [new TerserPlugin()]
minimizer: [new TerserPlugin()],
}
: undefined
: 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)
console.error(`> [PWA] Failed to build fallback worker`);
console.error(status.toString({ colors: true }));
process.exit(-1);
}
})
});

return { fallbacks, name, precaches: Object.values(envs).filter(v => !!v) }
}
return { fallbacks, name, precaches: Object.values(envs).filter(v => !!v) };
};

module.exports = buildFallbackWorker
export default buildFallbackWorker;
36 changes: 18 additions & 18 deletions cache.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict'
'use strict';

// Workbox RuntimeCaching config: https://developers.google.com/web/tools/workbox/reference-docs/latest/module-workbox-build#.RuntimeCachingEntry
module.exports = [
Expand Down Expand Up @@ -127,15 +127,15 @@ module.exports = [
},
{
urlPattern: ({ url }) => {
const isSameOrigin = self.origin === url.origin
if (!isSameOrigin) return false
const pathname = url.pathname
// Exclude /api/auth/callback/* to fix OAuth workflow in Safari without impact other environment
// Above route is default for next-auth, you may need to change it if your OAuth workflow has a different callback route
const isSameOrigin = self.origin === url.origin;
if (!isSameOrigin) return false;
const pathname = url.pathname;
// Exclude /api/auth/callback/* to fix OAuth workflow in Safari without impacting other environments
// Above route is the default for next-auth, you may need to change it if your OAuth workflow has a different callback route
// Issue: https://github.com/shadowwalker/next-pwa/issues/131#issuecomment-821894809
if (pathname.startsWith('/api/auth/')) return false
if (pathname.startsWith('/api/')) return true
return false
if (pathname.startsWith('/api/auth/')) return false;
if (pathname.startsWith('/api/')) return true;
return false;
},
handler: 'NetworkFirst',
method: 'GET',
Expand All @@ -145,16 +145,16 @@ module.exports = [
maxEntries: 16,
maxAgeSeconds: 24 * 60 * 60 // 24 hours
},
networkTimeoutSeconds: 10 // fall back to cache if api does not response within 10 seconds
networkTimeoutSeconds: 10 // fall back to cache if the API does not respond within 10 seconds
}
},
{
urlPattern: ({ url }) => {
const isSameOrigin = self.origin === url.origin
if (!isSameOrigin) return false
const pathname = url.pathname
if (pathname.startsWith('/api/')) return false
return true
const isSameOrigin = self.origin === url.origin;
if (!isSameOrigin) return false;
const pathname = url.pathname;
if (pathname.startsWith('/api/')) return false;
return true;
},
handler: 'NetworkFirst',
options: {
Expand All @@ -168,8 +168,8 @@ module.exports = [
},
{
urlPattern: ({ url }) => {
const isSameOrigin = self.origin === url.origin
return !isSameOrigin
const isSameOrigin = self.origin === url.origin;
return !isSameOrigin;
},
handler: 'NetworkFirst',
options: {
Expand All @@ -181,4 +181,4 @@ module.exports = [
networkTimeoutSeconds: 10
}
}
]
];
3 changes: 0 additions & 3 deletions examples/cache-on-front-end-nav/.eslintrc.json

This file was deleted.

41 changes: 0 additions & 41 deletions examples/cache-on-front-end-nav/README.md

This file was deleted.

5 changes: 0 additions & 5 deletions examples/cache-on-front-end-nav/next.config.js

This file was deleted.

Loading