-
Notifications
You must be signed in to change notification settings - Fork 3
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
perf: reduce the latency of downloading pdf.worker.min.js #39
Comments
We did a spike today and found that PWA service worker can mitigate this issue. By caching Evidence: Code skeleton looks something like this: // eslint-disable-next-line no-undef
self.addEventListener('install', () => {
console.debug('service worker installed')
})
// eslint-disable-next-line no-undef
self.addEventListener('activate', () => {
console.debug('service worker activated')
})
// eslint-disable-next-line no-undef
self.addEventListener('fetch', function (e) {
const CACHE_NAME = 'cache-v1'
console.log('fetch', e.request.url)
e.respondWith(
(async function () {
// eslint-disable-next-line no-undef
const cachedResponse = await caches.match(e.request)
if (cachedResponse) {
console.log(
'return from cache for response with request.url: ',
e.request.url
)
return cachedResponse
}
const networkResponse = await fetch(e.request)
const hosts = ['https://unpkg.com']
if (hosts.some((host) => e.request.url.startsWith(host))) {
// Cloning the response is necessary because request and response
// streams can only be read once. In order to return the response to the
// browser and put it in the cache we have to clone it. So the original
// gets returned to the browser and the clone gets sent to the cache.
// They are each read once.
//
// This clone() happens before `return networkResponse`
const clonedResponse = networkResponse.clone()
e.waitUntil(
(async function () {
// eslint-disable-next-line no-undef
const cache = await caches.open(CACHE_NAME)
// This will be called after `return networkResponse`
// so make sure you already have the clone!
console.log('set new cache')
await cache.put(e.request, clonedResponse)
})()
)
}
return networkResponse
})()
)
}) Ref implementation: |
As mentioned in above, we can mitigate the pain by adopting service worker to cache In the end I didn't use code shown in this comment, however, the problem of the original code is, by default it is pretty dangerous to cache a opaque response since it may lead to corrupted PWA app permanently. Instead, I adopted serwist for generating a service worker file, and self hosted pdf.js (in order to make the request to So after all the optimization, the latency to download ![]() ![]() For now, this should be enough for us. Ideally, react-pdf-viewer's |
Description
PPResume use react-pdf-viewer for viewing PDF, however, it has a serious bug, when the encoded PDF (by base64) changed, it will re-init its worker and one of its dependency
pdf.worker.min.js
would be re-downloaded.In the whole session of resume composing,
pdf.worker.min.js
would be re-downloaded tens of hundreds of times, which is both bad for performance and bandwidth.Evidence:
[Optional] Possible Solutions
pdf.worker.min.js
in browser cache and avoid re-download because it doesn't changeAcceptance Criteria
pdf.worker.min.js
should be downloaded for only once from remote CDN and later download request should be cached from browser local cacheTodo list
NA
The text was updated successfully, but these errors were encountered: