Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'workbox-webpack-plugin' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
const manifest = originalManifest.filter(
(entry) => entry.url !== 'ignored.html');
// Optionally, set warning messages.
const warnings = ['warning'];
return {manifest, warnings: [] };
}
]
});
}
// InjectManifest
{
let plugin: InjectManifest;
// No options object
plugin = new InjectManifest();
// Minimal options object (swSrc is required)
plugin = new InjectManifest({
swSrc: 'service-worker.js'
});
// With all of the examples
plugin = new InjectManifest({
swDest: 'custom-sw-name.js',
importWorkboxFrom: 'local',
// *Only* include assets that belong to these chunks:
chunks: ['chunk-name-1', 'chunk-name-2'],
// Exclude assets that belong to these chunks:
excludeChunks: ['chunk-name-1', 'chunk-name-2'],
// Only include HTML and JS assets when precaching:
include: [/\.html$/, /\.js$/],
node: {
fs: 'empty'
},
optimization: {
namedModules: true,
splitChunks: {
minChunks: 2
}
},
performance: {
assetFilter: assetFilename => !mediaFilesRegex.test(assetFilename) &&
!assetFilename.endsWith('.map')
}
};
const serviceWorkerPlugin = new WorkboxPlugin.GenerateSW({
swDest: 'sw.js',
exclude: [
/\.map$/, // source maps
/^manifest.*\.js(?:on)?$/, // web app manifest
/icons-[a-z0-9]+\/[a-z0-9_-]+\.png$/, // icons
/icons-[a-z0-9]+\/\.cache$/, // favicons cache file
/node_modules\/standardized-audio-context\//,
mediaFilesRegex // media files
],
skipWaiting: true,
clientsClaim: true,
cleanupOutdatedCaches: true,
runtimeCaching: [{
urlPattern: mediaFilesRegex,
handler: 'CacheFirst',
options: {}
// to their corresponding output file so that tools can pick it up without
// having to parse `index.html`.
new ManifestPlugin({
fileName: 'asset-manifest.json',
publicPath: publicPath,
}),
// Moment.js is an extremely popular library that bundles large locale files
// by default due to how Webpack interprets its code. This is a practical
// solution that requires the user to opt into importing specific locales.
// https://github.com/jmblog/how-to-optimize-momentjs-with-webpack
// You can remove this if you don't use Moment.js:
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
// Generate a service worker script that will precache, and keep up to date,
// the HTML & assets that are part of the Webpack build.
isEnvProduction &&
new WorkboxWebpackPlugin.GenerateSW({
clientsClaim: true,
exclude: [/\.map$/, /asset-manifest\.json$/],
importWorkboxFrom: 'cdn',
navigateFallback: publicUrl + '/index.html',
navigateFallbackBlacklist: [
// Exclude URLs starting with /_, as they're likely an API call
new RegExp('^/_'),
// Exclude URLs containing a dot, as they're likely a resource in
// public/ and not a SPA route
new RegExp('/[^/]+\\.[^/]+$'),
],
}),
// TypeScript type checking
useTypeScript &&
new ForkTsCheckerWebpackPlugin({
typescript: resolve.sync('typescript', {
return {
files: manifestFiles,
};
},
}),
// Moment.js is an extremely popular library that bundles large locale files
// by default due to how Webpack interprets its code. This is a practical
// solution that requires the user to opt into importing specific locales.
// https://github.com/jmblog/how-to-optimize-momentjs-with-webpack
// You can remove this if you don't use Moment.js:
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
// Generate a service worker script that will precache, and keep up to date,
// the HTML & assets that are part of the Webpack build.
isEnvProduction &&
new WorkboxWebpackPlugin.GenerateSW({
clientsClaim: true,
exclude: [/\.map$/, /asset-manifest\.json$/],
importWorkboxFrom: 'cdn',
navigateFallback: publicUrl + '/index.html',
navigateFallbackBlacklist: [
// Exclude URLs starting with /_, as they're likely an API call
new RegExp('^/_'),
// Exclude URLs containing a dot, as they're likely a resource in
// public/ and not a SPA route
new RegExp('/[^/]+\\.[^/]+$'),
],
}),
// TypeScript type checking
useTypeScript &&
new ForkTsCheckerWebpackPlugin({
typescript: resolve.sync('typescript', {
new HtmlWebpackPlugin({ // Create index.html file
cache: PRODUCTION,
template: 'src/index.ejs',
}),
new ScriptExtHtmlWebpackPlugin({
defaultAttribute: 'defer'
}),
PRODUCTION && new StyleExtHtmlWebpackPlugin(),
PRODUCTION && new CleanWebpackPlugin(), // Cleanup before each build
new CopyWebpackPlugin([{ from: 'public', to: '' }]), // Copy root domain files
ANALYZE && new BundleAnalyzerPlugin({ analyzerMode: 'static', openAnalyzer: false }),
PRODUCTION && new WorkboxPlugin.GenerateSW({
skipWaiting: true,
exclude: [ /htaccess/, /robots\.txt/, /\.php$/ ]
}),
].filter(Boolean),
module: {
strictExportPresence: true,
rules: [
{ parser: { requireEnsure: false } },
{ test: /\.(c|le)ss$/,
use: [
!PRODUCTION ? 'style-loader' : MiniCssExtractPlugin.loader,
{
loader: "css-loader",
options: {
importLoaders: 1,
sourceMap: true,
htmlFiles[item.name] = item.path;
}
if (item.isAsset && item.isModuleAsset) {
assets[item.name] = item.path;
}
});
// fesMap
manifestFile.entry = paths.fesMap.entry;
return manifestFile;
},
}),
]
);
plugins.push(
new WorkboxWebpackPlugin.GenerateSW(
Object.assign(
{
clientsClaim: true,
exclude: [/\.map$/, /asset-manifest\.json$/],
navigateFallback: '/',
navigateFallbackBlacklist: [
// Exclude URLs starting with /_, as they're likely an API call
new RegExp('^/_'),
// Exclude URLs containing a dot, as they're likely a resource in
// public/ and not a SPA route
new RegExp('/[^/]+\\.[^/]+$'),
],
},
appConfig.sw
)
)
return {
files: manifestFiles,
};
},
}),
// Moment.js is an extremely popular library that bundles large locale files
// by default due to how Webpack interprets its code. This is a practical
// solution that requires the user to opt into importing specific locales.
// https://github.com/jmblog/how-to-optimize-momentjs-with-webpack
// You can remove this if you don't use Moment.js:
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
// Generate a service worker script that will precache, and keep up to date,
// the HTML & assets that are part of the Webpack build.
isEnvProduction &&
new WorkboxWebpackPlugin.GenerateSW({
clientsClaim: true,
exclude: [/\.map$/, /asset-manifest\.json$/],
importWorkboxFrom: 'cdn',
navigateFallback: publicUrl + '/index.html',
navigateFallbackBlacklist: [
// Exclude URLs starting with /_, as they're likely an API call
new RegExp('^/_'),
// Exclude URLs containing a dot, as they're likely a resource in
// public/ and not a SPA route
new RegExp('/[^/]+\\.[^/]+$'),
],
}),
// TypeScript type checking
useTypeScript &&
new ForkTsCheckerWebpackPlugin({
typescript: resolve.sync('typescript', {
files: manifestFiles,
entrypoints: entrypointFiles,
};
},
}),
// Moment.js is an extremely popular library that bundles large locale files
// by default due to how Webpack interprets its code. This is a practical
// solution that requires the user to opt into importing specific locales.
// https://github.com/jmblog/how-to-optimize-momentjs-with-webpack
// You can remove this if you don't use Moment.js:
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
// Generate a service worker script that will precache, and keep up to date,
// the HTML & assets that are part of the Webpack build.
// CRL: Update WorkboxWebpackPlugin.GenerateSW check
isEnvDemo &&
new WorkboxWebpackPlugin.GenerateSW({
clientsClaim: true,
exclude: [/\.map$/, /asset-manifest\.json$/],
importWorkboxFrom: 'cdn',
navigateFallback: publicUrl + '/index.html',
navigateFallbackBlacklist: [
// Exclude URLs starting with /_, as they're likely an API call
new RegExp('^/_'),
// Exclude any URLs whose last part seems to be a file extension
// as they're likely a resource and not a SPA route.
// URLs containing a "?" character won't be blacklisted as they're likely
// a route with query params (e.g. auth callbacks).
new RegExp('/[^/?]+\\.[^/]+$'),
],
}),
// TypeScript type checking
useTypeScript &&
const config = {
// `globDirectory` and `globPatterns` must match at least 1 file
// otherwise workbox throws an error
globDirectory: this.config.paths.output,
// TODO: (feature) autogenerate glob patterns from asset manifest
globPatterns: ['**/*.{gif,jpg,png,svg}'],
// activate the worker as soon as it reaches the waiting phase
skipWaiting: true,
// the max scope of a worker is its location
swDest: SW_FILENAME
};
if (this.config.runtimeCacheConfig) {
config.runtimeCaching = this.config.runtimeCacheConfig;
}
new WorkboxPlugin.GenerateSW(config).apply(compiler);
}
// to their corresponding output file so that tools can pick it up without
// having to parse `index.html`.
new ManifestPlugin({
fileName: 'asset-manifest.json',
publicPath: publicPath,
}),
// Moment.js is an extremely popular library that bundles large locale files
// by default due to how Webpack interprets its code. This is a practical
// solution that requires the user to opt into importing specific locales.
// https://github.com/jmblog/how-to-optimize-momentjs-with-webpack
// You can remove this if you don't use Moment.js:
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
// Generate a service worker script that will precache, and keep up to date,
// the HTML & assets that are part of the Webpack build.
isEnvProduction &&
new WorkboxWebpackPlugin.GenerateSW({
clientsClaim: true,
exclude: [/\.map$/, /asset-manifest\.json$/],
importWorkboxFrom: 'cdn',
navigateFallback: publicUrl + '/index.html',
navigateFallbackBlacklist: [
// Exclude URLs starting with /_, as they're likely an API call
new RegExp('^/_'),
// Exclude URLs containing a dot, as they're likely a resource in
// public/ and not a SPA route
new RegExp('/[^/]+\\.[^/]+$'),
],
}),
// TypeScript type checking
useTypeScript &&
new ForkTsCheckerWebpackPlugin({
typescript: resolve.sync('typescript', {