question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Caching loaded documents for reuse

See original GitHub issue

Before you start - checklist

  • I have read documentation in README
  • I have checked sample and test suites to see real life basic implementation
  • I have checked if this question is not already asked

What are you trying to achieve? Please describe.

I have an application that can render multiple PDF documents. The user can select a document from the menu. Currently, whenever the user selects a document, the PDF file is always fetched anew from the server, even if it was already viewed prior. This puts a load on the network, as some documents can reach several MBs in size. I’m looking for a way to cache those PDF documents (incl. all pages) to avoid repeated requests (esp. on metered networks).

Describe solutions you’ve tried

I tried a naive approach to hook into onLoadSuccess and save the PdfDocumentProxy object into state

state = { pdfs: {} }

onDocumentLoadSuccess = pdf => {
  const selectedId = 'xyz'

  this.setState(({ pdfs }) => ({
    ...pdfs,
    [selectedId]: pdf
  }))
}

and then pass the saved object via file prop

render () {
  const { pdfs } = this.state
  const selectedUrl = 'https://example.com/example.pdf', selectedId = 'xyz'
  // If cached, use it, else fetch fresh from the API
  const pdf = pdfs[selectedId] ? pdfs[selectedId] : selectedUrl

  return <Document file={pdf} ... />
}

However, this doesn’t work as file prop doesn’t interpret PdfDocumentProxy objects. I also looked into other hooks such as onSourceSuccess, but it’s not clear how I could retrieve the contents of the PDF file with either one.

Question

Is there any recommendations for caching PDFs with react-pdf? I looked into this in pdfjs-dist but didn’t find anything conclusive. Any suggestions as far which store would be appropriate? I know of limitations of localStorage (5MB), IndexedDB (50MB/5MB), perhaps a POJO map could work? Or is this a silly idea altogether?

Environment

  • react-pdf: 4.0.2
  • react: 16.7.0
  • node: 10
  • Browser: IE11, Chrome, FF, Safari

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:4
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
ghostcommented, Sep 19, 2021

@daweimau,

const [PDFBlob, setPDFBlob] = useState(null)

useEffect(() => {
    const controller = new AbortController();

    fetch(url, { signal: controller.signal })
      .then(res => res.blob())
      .then(file => setPDFBlob(file))
      .catch(e => { 
        if(e.name !== 'AbortError') { /* handle error*/ }
      });

    return () => controller.abort();
  }, [url, setPDFBlob]);
  
  return PDFBlob && <Document file={PDFBlob} ...
1reaction
mparisi76commented, Jul 28, 2020

@wojtekmaj Is there an example of this anywhere? I’m trying to implement the file download myself, and I’m creating a new Blob, however, react-pdf doesn’t render anything. I’m not sure what I’m doing wrong…

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is Caching and How it Works - AWS
Caching allows you to efficiently reuse previously retrieved or computed data. How does Caching work? The data in a cache is generally stored...
Read more >
Caching Dependencies - CircleCI
This document is a guide to caching dependencies in CircleCI pipelines. ... For information about caching and reuse of unchanged layers of a...
Read more >
Adjust Caching Parameters - Accusoft Support
The cache lifetime is the length of time that a document is cached and can be potentially reused by other new viewing sessions....
Read more >
Cache reuse across DoFn's in Beam | Google Cloud | - Medium
LifeCycle of a DoFn · Caching data for reuse across DoFn Instances · Example illustrating below concepts: a) Build an in memory cache...
Read more >
HTTP caching - MDN Web Docs
At 23:22:22, the response becomes stale and the cache cannot be reused. So the request below shows a client sending a request with...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found