Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'workbox-core' 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.
responsePromise = responsePromise.then((response) => {
// Workbox is going to handle the route.
// print the routing details to the console.
logger.groupCollapsed(`Precaching is responding to: ` +
getFriendlyURL(event.request.url));
logger.log(`Serving the precached url: ${precachedURL}`);
logger.groupCollapsed(`View request details here.`);
logger.log(event.request);
logger.groupEnd();
logger.groupCollapsed(`View response details here.`);
logger.log(response);
logger.groupEnd();
logger.groupEnd();
return response;
});
}
responsePromise = responsePromise.then((response) => {
// Workbox is going to handle the route.
// print the routing details to the console.
logger.groupCollapsed(`Precaching is responding to: ` +
getFriendlyURL(event.request.url));
logger.log(`Serving the precached url: ${precachedURL}`);
logger.groupCollapsed(`View request details here.`);
logger.log(event.request);
logger.groupEnd();
logger.groupCollapsed(`View response details here.`);
logger.log(response);
logger.groupEnd();
logger.groupEnd();
return response;
});
}
handler = this._defaultHandler;
}
if (!handler) {
if (process.env.NODE_ENV !== 'production') {
// No handler so Workbox will do nothing. If logs is set of debug
// i.e. verbose, we should print out this information.
logger.debug(`No route found for: ${getFriendlyURL(url)}`);
}
return;
}
if (process.env.NODE_ENV !== 'production') {
// We have a handler, meaning Workbox is going to handle the route.
// print the routing details to the console.
logger.groupCollapsed(`Router is responding to: ${getFriendlyURL(url)}`);
debugMessages.forEach((msg) => {
if (Array.isArray(msg)) {
logger.log(...msg);
} else {
logger.log(msg);
}
});
logger.groupEnd();
}
// Wrap in try and catch in case the handle method throws a synchronous
// error. It should still callback to the catch handler.
let responsePromise;
try {
responsePromise = responsePromise.then((response) => {
// Workbox is going to handle the route.
// print the routing details to the console.
logger.groupCollapsed(`Precaching is responding to: ` +
getFriendlyURL(event.request.url));
logger.log(`Serving the precached url: ${precachedURL}`);
logger.groupCollapsed(`View request details here.`);
logger.log(event.request);
logger.groupEnd();
logger.groupCollapsed(`View response details here.`);
logger.log(response);
logger.groupEnd();
logger.groupEnd();
return response;
});
}
responsePromise = responsePromise.then((response) => {
// Workbox is going to handle the route.
// print the routing details to the console.
logger.groupCollapsed(`Precaching is responding to: ` +
getFriendlyURL(event.request.url));
logger.log(`Serving the precached url: ${precachedURL}`);
logger.groupCollapsed(`View request details here.`);
logger.log(event.request);
logger.groupEnd();
logger.groupCollapsed(`View response details here.`);
logger.log(response);
logger.groupEnd();
logger.groupEnd();
return response;
});
}
}
if (process.env.NODE_ENV !== 'production') {
// We have a handler, meaning Workbox is going to handle the route.
// print the routing details to the console.
logger.groupCollapsed(`Router is responding to: ${getFriendlyURL(url)}`);
debugMessages.forEach((msg) => {
if (Array.isArray(msg)) {
logger.log(...msg);
} else {
logger.log(msg);
}
});
logger.groupEnd();
}
// Wrap in try and catch in case the handle method throws a synchronous
// error. It should still callback to the catch handler.
let responsePromise;
try {
responsePromise = handler.handle({url, request, event, params});
} catch (err) {
responsePromise = Promise.reject(err);
}
if (responsePromise instanceof Promise && this._catchHandler) {
responsePromise = responsePromise.catch((err) => {
if (process.env.NODE_ENV !== 'production') {
// Still include URL here as it will be async from the console group
// and may not make sense without the URL
responsePromise = responsePromise.then((response) => {
// Workbox is going to handle the route.
// print the routing details to the console.
logger.groupCollapsed(`Precaching is responding to: ` +
getFriendlyURL(event.request.url));
logger.log(`Serving the precached url: ${precachedURL}`);
logger.groupCollapsed(`View request details here.`);
logger.log(event.request);
logger.groupEnd();
logger.groupCollapsed(`View response details here.`);
logger.log(response);
logger.groupEnd();
logger.groupEnd();
return response;
});
}
const normalizedRangeHeader = rangeHeader.trim().toLowerCase();
if (!normalizedRangeHeader.startsWith('bytes=')) {
throw new WorkboxError('unit-must-be-bytes', {normalizedRangeHeader});
}
// Specifying multiple ranges separate by commas is valid syntax, but this
// library only attempts to handle a single, contiguous sequence of bytes.
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Range#Syntax
if (normalizedRangeHeader.includes(',')) {
throw new WorkboxError('single-range-only', {normalizedRangeHeader});
}
const rangeParts = /(\d*)-(\d*)/.exec(normalizedRangeHeader);
// We need either at least one of the start or end values.
if (!rangeParts || !(rangeParts[1] || rangeParts[2])) {
throw new WorkboxError('invalid-range-values', {normalizedRangeHeader});
}
return {
start: rangeParts[1] === '' ? undefined : Number(rangeParts[1]),
end: rangeParts[2] === '' ? undefined : Number(rangeParts[2]),
};
}
constructor(cacheName: string, config: CacheExpirationConfig = {}) {
if (process.env.NODE_ENV !== 'production') {
assert!.isType(cacheName, 'string', {
moduleName: 'workbox-expiration',
className: 'CacheExpiration',
funcName: 'constructor',
paramName: 'cacheName',
});
if (!(config.maxEntries || config.maxAgeSeconds)) {
throw new WorkboxError('max-entries-or-age-required', {
moduleName: 'workbox-expiration',
className: 'CacheExpiration',
funcName: 'constructor',
});
}
if (config.maxEntries) {
assert!.isType(config.maxEntries, 'number', {
moduleName: 'workbox-expiration',
className: 'CacheExpiration',
funcName: 'constructor',
paramName: 'config.maxEntries',
});
// TODO: Assert is positive
}
}
}
const isValidResponse = cacheWillUpdatePlugin ?
// Use a callback if provided. It returns a truthy value if valid.
// NOTE: invoke the method on the plugin instance so the `this` context
// is correct.
await cacheWillUpdatePlugin.cacheWillUpdate!({event, request, response}) :
// Otherwise, default to considering any response status under 400 valid.
// This includes, by default, considering opaque responses valid.
response.status < 400;
// Consider this a failure, leading to the `install` handler failing, if
// we get back an invalid response.
if (!isValidResponse) {
throw new WorkboxError('bad-precaching-response', {
url,
status: response.status,
});
}
// Redirected responses cannot be used to satisfy a navigation request, so
// any redirected response must be "copied" rather than cloned, so the new
// response doesn't contain the `redirected` flag. See:
// https://bugs.chromium.org/p/chromium/issues/detail?id=669363&desc=2#c1
if (response.redirected) {
response = await copyResponse(response);
}
await cacheWrapper.put({
event,
plugins,