-
Notifications
You must be signed in to change notification settings - Fork 830
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
Support for setting fetchOptions in workbox-precaching #1760
Comments
Sure, we should be able to do that. It's a little awkward, but the model to follow is the workbox/packages/workbox-precaching/_default.mjs Lines 294 to 303 in b146d8d
We could implement |
@jeffposnick Thanks for considering my request. Adding this feature will help users whose application requires additional headers or data in every fetch call including fetch calls of precache. While this could easily be achieved in custom workbox strategies handler, as of now its not seems possible in precache's fetch calls. |
I would also find this very useful :) |
Related to #1857. FWIW, if you're wanting to do anything custom with precaching, I'd recommend using an instance of Here's a simple example that shows how to use const PRECACHE_NAME = 'my-precache-name';
const PRECACHE_MANIFEST = [...]; // Inject via workbox-build (or your bundler).
const pc = new workbox.precaching.PrecacheController(PRECACHE_NAME);
pc.addToCacheList(PRECACHE_MANIFEST);
workbox.routing.registerRoute(
// Use whatever matching callback you need.
({url}) => PRECACHE_MANIFEST.includes(url),
workbox.strategies.cacheFirst({
cacheName: PRECACHE_NAME,
plugins: [
// Add fetch options here...
],
})
); |
AFAIK its not possible to set the headers, even when using |
This should be possible by adding plugins (which you can do with both |
Ok, great! I'll give that a go. Thanks! |
I went through the same issue, I am using workbox 3.6.3 and wanted to set custom header for my precacher, I am glad I was able to figure it out, with the help of this issue and digging through the docs. // Precache items
const precacheManifest = [/*** YOUR MANIFEST ***/];
// Custom plugin to alter the fetch
const fetchCustomHeader = {
requestWillFetch: async ({request}) => {
// Check if it's from your domain
if(/^.*\:\/\/(?:yourdomain\.test|yourdomain\.com)\/.*/.test(request.url)){
var myHeader = new Headers(request.headers);
myHeader.append('TITLE', 'VALUE');
}
var customHeader = myHeader;
// Return the new request
return new Request(request.url, {headers: customHeader});
}
};
// Precache Controller
const precacheController = new workbox.precaching.PrecacheController(precacheName);
precacheController.addToCacheList(precacheManifest);
/**
* Install [Event]
*/
self.addEventListener('install', (event) => {
console.log('install event.');
// Install precacher
event.waitUntil(precacheController.install({plugins: [fetchCustomHeader]}));
});
/**
* Activate [Event]
*/
self.addEventListener('activate', (event) => {
console.log('activate event.');
// Activate precacher
event.waitUntil(precacheController.activate());
}); In the fetch event I also check against precache, with precacheController.getCachedUrls(); Helpful Resources: |
I'd also like to know how fetchOptions for workbox-precaching can be made available to workbox-webpack-plugin so it can be configured in webpack.config.js |
@advaittrivedi, I don't think that level of customization is going to be possible via Instead, if you need to customize the |
Library Affected:
workbox-precaching.dev.js, workbox-precaching.prod.js.
Browser & Platform:
All browser
Issue or Feature Request Description:
Provide an option to explicitly pass fetchOptions while adding precache route .
Provide an option to pass fetchOptions while adding precache route either via workbox.precaching.PrecacheController, workbox.precaching.precacheAndRoute or workbox.precaching.addRoute
This is helpful in applications where custom response is served to user based on browser compatibility or user state. This could be easily achieved in custom request handler but for precaching no option is there.
By looking at source, it is observed that workbox precaching in background uses fetch calls for each asset or page listed to be precached.
_cacheEntryInTemp in class PrecacheController (file PrecacheController.mjs) Line is referenced
workbox/packages/workbox-precaching/controllers/PrecacheController.mjs
Line 268 in b146d8d
I couldn't find any way to override method _cacheEntryInTemp in PrecacheController class, so to append custom header on precache fetch calls, As the temporary workaround, I've copied entire workbox and started serving it from my server by modifying _cacheEntryInTemp in PrecacheController as below:
before initializing workbox i set myHeaderK (key), myHeaderV (value) for the application based on user browser and other parameters. This is just an example on how this could be beneficial for applications requiring explicit control over dynamic serving of precache pages based on custom parameters.
Therefore, please check if fetchOptions could be added as args in precache in next release.
The text was updated successfully, but these errors were encountered: