temp
This commit is contained in:
53
sw/public/sw.js
Normal file
53
sw/public/sw.js
Normal file
@@ -0,0 +1,53 @@
|
||||
async function readStream(stream) {
|
||||
const reader = stream.getReader();
|
||||
let result = '';
|
||||
|
||||
while (true) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) {
|
||||
break;
|
||||
}
|
||||
result += new TextDecoder().decode(value);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
self.addEventListener('fetch', async (event) => {
|
||||
const url = new URL(event.request.url);
|
||||
// Check if the request is for the specific path
|
||||
if (url.pathname.startsWith('/api/s1/sw/js/')) {
|
||||
console.log('event', event);
|
||||
console.log('url', url, event.request.method, 'mode', event.request.mode, event.request.destination);
|
||||
// const body = event.request.body;
|
||||
// const method = event.request.method;
|
||||
// const bodyText = await readStream(body);
|
||||
// console.log('body', bodyText);
|
||||
console.log('event.request', event.request.destination);
|
||||
event.respondWith(
|
||||
new Response('export const a = 1', {
|
||||
status: 200,
|
||||
headers: {
|
||||
'Content-Type': 'application/javascript',
|
||||
},
|
||||
}),
|
||||
);
|
||||
return;
|
||||
}
|
||||
if (url.pathname.startsWith('/api/s1/sw/js/')) {
|
||||
event.respondWith(
|
||||
fetch(event.request).then((response) => {
|
||||
// Clone the response to modify headers
|
||||
const newHeaders = new Headers(response.headers);
|
||||
newHeaders.set('Content-Type', 'application/javascript');
|
||||
|
||||
// Return a new response with the modified headers
|
||||
return new Response(response.body, {
|
||||
status: response.status,
|
||||
statusText: response.statusText,
|
||||
headers: newHeaders,
|
||||
});
|
||||
}),
|
||||
);
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user