fix: apply network-first strategy to language prefixed URLs in service worker
This commit is contained in:
43
public/sw.js
43
public/sw.js
@@ -5,7 +5,7 @@
|
|||||||
* Version: 1.1.0
|
* Version: 1.1.0
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const CACHE_VERSION = 'bentopdf-v8';
|
const CACHE_VERSION = 'bentopdf-v9';
|
||||||
const CACHE_NAME = `${CACHE_VERSION}-static`;
|
const CACHE_NAME = `${CACHE_VERSION}-static`;
|
||||||
|
|
||||||
const getBasePath = () => {
|
const getBasePath = () => {
|
||||||
@@ -118,7 +118,9 @@ self.addEventListener('fetch', (event) => {
|
|||||||
event.respondWith(cacheFirstStrategyWithDedup(event.request, isCDN));
|
event.respondWith(cacheFirstStrategyWithDedup(event.request, isCDN));
|
||||||
} else if (
|
} else if (
|
||||||
isLocal &&
|
isLocal &&
|
||||||
(url.pathname.endsWith('.html') || url.pathname === '/')
|
(url.pathname.endsWith('.html') ||
|
||||||
|
url.pathname === '/' ||
|
||||||
|
/^\/(en|fr|es|de|zh|zh-TW|vi|tr|id|it|pt|nl)(\/|$)/.test(url.pathname))
|
||||||
) {
|
) {
|
||||||
event.respondWith(networkFirstStrategy(event.request));
|
event.respondWith(networkFirstStrategy(event.request));
|
||||||
}
|
}
|
||||||
@@ -133,7 +135,7 @@ async function cacheFirstStrategyWithDedup(request, isCDN) {
|
|||||||
const fileName = url.pathname.split('/').pop();
|
const fileName = url.pathname.split('/').pop();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const cachedResponse = await findCachedFile(fileName);
|
const cachedResponse = await findCachedFile(fileName, request.url);
|
||||||
if (cachedResponse) {
|
if (cachedResponse) {
|
||||||
// console.log('⚡ [Cache HIT] Instant load:', fileName);
|
// console.log('⚡ [Cache HIT] Instant load:', fileName);
|
||||||
return cachedResponse;
|
return cachedResponse;
|
||||||
@@ -181,14 +183,27 @@ async function cacheFirstStrategyWithDedup(request, isCDN) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function findCachedFile(fileName) {
|
async function findCachedFile(fileName, requestUrl) {
|
||||||
const cache = await caches.open(CACHE_NAME);
|
const cache = await caches.open(CACHE_NAME);
|
||||||
const requests = await cache.keys();
|
|
||||||
|
|
||||||
|
const exactMatch = await cache.match(requestUrl);
|
||||||
|
if (exactMatch && exactMatch.headers.get('content-length') !== '0') {
|
||||||
|
return exactMatch;
|
||||||
|
}
|
||||||
|
|
||||||
|
const requests = await cache.keys();
|
||||||
for (const req of requests) {
|
for (const req of requests) {
|
||||||
const reqUrl = new URL(req.url);
|
const reqUrl = new URL(req.url);
|
||||||
if (reqUrl.pathname.endsWith(fileName)) {
|
if (reqUrl.pathname.endsWith(fileName)) {
|
||||||
return await cache.match(req);
|
const response = await cache.match(req);
|
||||||
|
if (response) {
|
||||||
|
const clone = response.clone();
|
||||||
|
const buffer = await clone.arrayBuffer();
|
||||||
|
if (buffer.byteLength > 0) {
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
await cache.delete(req);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
@@ -283,18 +298,18 @@ function shouldCache(pathname, isCDN = false) {
|
|||||||
async function cacheInBatches(cache, urls, batchSize = 5) {
|
async function cacheInBatches(cache, urls, batchSize = 5) {
|
||||||
for (let i = 0; i < urls.length; i += batchSize) {
|
for (let i = 0; i < urls.length; i += batchSize) {
|
||||||
const batch = urls.slice(i, i + batchSize);
|
const batch = urls.slice(i, i + batchSize);
|
||||||
// console.log(`[ServiceWorker] Caching batch ${Math.floor(i / batchSize) + 1}/${Math.ceil(urls.length / batchSize)}`);
|
|
||||||
|
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
batch.map(async (url) => {
|
batch.map(async (url) => {
|
||||||
try {
|
try {
|
||||||
await cache.add(url);
|
const response = await fetch(url);
|
||||||
const fileName = url.split('/').pop();
|
if (response.ok && response.status === 200) {
|
||||||
const fileSize =
|
const clone = response.clone();
|
||||||
fileName.includes('.wasm') || fileName.includes('.whl')
|
const buffer = await clone.arrayBuffer();
|
||||||
? '(large file)'
|
if (buffer.byteLength > 0) {
|
||||||
: '';
|
await cache.put(url, response);
|
||||||
// console.log(` ✓ Cached: ${fileName} ${fileSize}`);
|
}
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.warn('[ServiceWorker] Failed to cache:', url, error.message);
|
console.warn('[ServiceWorker] Failed to cache:', url, error.message);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
export const PACKAGE_VERSIONS = {
|
export const PACKAGE_VERSIONS = {
|
||||||
ghostscript: '0.1.0',
|
ghostscript: '0.1.1',
|
||||||
pymupdf: '0.1.9',
|
pymupdf: '0.11.14',
|
||||||
} as const;
|
} as const;
|
||||||
|
|||||||
Reference in New Issue
Block a user