I connected the service worker.
I need to make a new response, replacing it in the service worker
self.addEventListener('install', event => {
self.skipWaiting();
});
self.addEventListener('fetch', event => {
try {
const url = new URL(event.request.url);
const scope = self.registration.scope;
if(url.pathname.includes('/nk-self/')) {
console.log('new Request', url.pathname)
.....
}
} catch (e) {
console.error('error', e)
}
});
Next I create a documentFragment and add it to the page
In the document fragment I add import js file * import {test} from ‘./index.mjs’ *
const fragment = new DocumentFragment();
const script = document.createElement("script");
script.type = 'module'
script.textContent = `
import {test} from './index.mjs'
`
fragment.appendChild(script);
self.control.run.appendChild(fragment);
I need to receive this request in the service worker and substitute the correct value
I tried to put a catch block in the service worker
try {
} catch(e) {
}
But this import does not reach the service worker.
And the error appears earlier.
Uncaught SyntaxError: The requested module './index.mjs' does not provide an export named 'test'
I can’t intercept this request and override the response.
How can I make sure that any request, even with an error, is sent to the service worker? ?