You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I followed instructions in documentation written for my React-PDF version
I have checked if this bug is not already reported
I have checked if an issue is not listed in Known issues
If I have a problem with PDF rendering, I checked if my PDF renders properly in PDF.js demo
Description
I'm building an app that needs to work offline.
To handle this we cache PDFs that we need.
Initial load works just fine but when revisiting the PDF the PDF fails to load.
Steps to reproduce
Set up a service worker to cache any pdfs (I've placed my source code)
Load the PDF and display it in browser (I'm running my app in a WebView on Android)
Close the page and reopen the PDF with the now cached PDF
Expected behavior
Loads just fine
Actual behavior
Getting an error "cannot read properties of null (reading sendWithPromise)".
I have seen other issues talking about this, but can't seem to find a proper fix.
Additional information
Find attached the below source code
The service worker source code
constCACHE_NAME='cache-worker-v1';constASSET_CACHE_NAME_LOG=(...log: Parameters<typeofconsole.log>)=>console.log(CACHE_NAME, ...log)typeAssetCachingDetails={id: string}//https://github.com/uuidjs/uuid/blob/main/src/regex.tsconstgetIsUUID=(id: string)=>{constregex=/^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000|ffffffff-ffff-ffff-ffff-ffffffffffff)$/ireturnregex.test(id)}constgetValidId=(index: number,pathname: string,shouldBeUUID=true): AssetCachingDetails|null=>{ASSET_CACHE_NAME_LOG("getIsValidId",pathname)constsplit=pathname.split("/");constid=split.at(index);ASSET_CACHE_NAME_LOG("getIsValidId",`pathname: ${pathname}`,`id: ${id}`)if(id===undefined)returnnull;if(shouldBeUUID){constisUUID=getIsUUID(id)ASSET_CACHE_NAME_LOG("getIsValidId",pathname,`shouldBeUUID: ${shouldBeUUID}`,`isUUID: ${isUUID}`)if(!isUUID)returnnull}return{id};}/** * Will determine the id of the data to be cached if it matches a url that we cache. * * We cache * GET /v2/app/info/recommended/{caseId} * GET /v2/app/info/{id} * GET /v2/app/info/asset/{orgId}/{assetId}/{filenameWithExt} * * @returns the correct id to pass to the cache or NULL if the url was invalid * @param pathname the pathname to check */constgetAssetCachingDetails=(pathname: string): AssetCachingDetails|null=>{constrecommendedPathnamePrefix="/v2/app/info/recommended/"constinfoCardPathnamePrefix="/v2/app/info/"constassetPathnamePrefix="/v2/app/info/asset/"constisRecommended=pathname.startsWith(recommendedPathnamePrefix)constisAsset=pathname.startsWith(assetPathnamePrefix)constisInfoCard=pathname.startsWith(infoCardPathnamePrefix)&&!isAsset&&!isRecommendedletid: ReturnType<typeofgetAssetCachingDetails>=nullif(isRecommended)id=getValidId(5,pathname,false)elseif(isAsset)id=getValidId(6,pathname)elseif(isInfoCard)id=getValidId(4,pathname)returnid}self.addEventListener("fetch",(ev)=>{// @ts-expect-error the FetchEvent is untypedASSET_CACHE_NAME_LOG(JSON.stringify(ev.request.url))// @ts-expect-error the FetchEvent is untypedconsturl=newURL(ev.request.url)ASSET_CACHE_NAME_LOG(`URL: ${url.toString()}`)ASSET_CACHE_NAME_LOG(`pathname: ${url.pathname}`)constassetCachingDetails=getAssetCachingDetails(url.pathname)ASSET_CACHE_NAME_LOG(url.pathname,JSON.stringify(assetCachingDetails===null ? "Not to be cached" : assetCachingDetails))if(assetCachingDetails===null)return;const{id}=assetCachingDetails;ASSET_CACHE_NAME_LOG(`Begin caching ${id} for url ${url.pathname}`)// @ts-expect-error the FetchEvent is untypedev.respondWith((async()=>{constcache=awaitcaches.open(CACHE_NAME);letnetworkResponse: Responsetry{/** * We want to use the cached response from previous fetch requests IF this response fails */// @ts-expect-error the FetchEvent is untypednetworkResponse=awaitfetch(ev.request);if(!networkResponse.ok)throwError()constclonedResponse=networkResponse.clone();ASSET_CACHE_NAME_LOG(`Caching the response for ${id}`);awaitcache.put(id,clonedResponse);returnnetworkResponse;}catch{constcachedResponse=awaitcache.match(id);if(cachedResponse){ASSET_CACHE_NAME_LOG(`Using cache for ${id}`);returncachedResponse;}returnnetworkResponse;}})());})
// useQueryAsset.tsimport{useQuery}from"@tanstack/react-query";import{EvamApi}from"@evam-life/sdk";/** * Query hook to fetch an asset from CS * @param src */constuseQueryAsset=(src: string)=>useQuery({queryFn: async()=>{const{data}=awaitEvamApi.cs.instance.get(src,{responseType: "blob"});returnURL.createObjectURL(data)},queryKey: [src],refetchOnMount: "always"})exportdefaultuseQueryAsset
Before you start - checklist
Description
I'm building an app that needs to work offline.
To handle this we cache PDFs that we need.
Initial load works just fine but when revisiting the PDF the PDF fails to load.
Steps to reproduce
Expected behavior
Loads just fine
Actual behavior
Getting an error "cannot read properties of null (reading sendWithPromise)".
I have seen other issues talking about this, but can't seem to find a proper fix.
Additional information
Find attached the below source code
The service worker source code
(I am using Tanstack Router for routing)
Environment
The text was updated successfully, but these errors were encountered: