React-pdf: Support non-blocking rendering (Node/Web Workers)

Created on 25 Jan 2019  路  4Comments  路  Source: diegomura/react-pdf

Describe the bug
React PDF rendering hogs the main thread on a Hapi web server, blocking other operations, e.g. HTTP responses and resulting in HTTP 500/503 errors

To Reproduce
Render a document to PDF by streaming ReactPDF.renderToStream to Node鈥檚 HTTP response (which takes >5000ms) while also trying to handle lots of normal incoming HTTP requests 馃ぃ (I can provide a reproducible sample if needed)

Expected behavior
Ideally, ReactPDF.renderToStream would _optionally_ return a Promise resolving to a stream that didn鈥檛 block the main thread. This could be by delegating the actual PDF creation to another thread, like in a Worker thread in Node or a Web Worker in the browser. Although Node seems a higher priority right now, it seems it would be relatively trivial to code for both. We could then stream the PDF back to the calling thread.

I have started working on a solution along these lines but wanted to see if this sounds like a good approach to take. Let me know what you think @diegomura and other contributors.

new feature

Most helpful comment

This sounds like a really good idea! I don't have that much experience working with web workers, and less with worker threads, but since the rendering is quite independent in terms of code I guess it shouldn't be that hard. I might be wrong with that though

All 4 comments

This sounds like a really good idea! I don't have that much experience working with web workers, and less with worker threads, but since the rendering is quite independent in terms of code I guess it shouldn't be that hard. I might be wrong with that though

So, I tried implementing this last night, but I had issues writing the workers scripts since then need to be in a separate file. Even though the render logic is quite isolated, I couldn't find a solution yet. I have no much experience working with workers in general, so if someone has a clearer picture about how this can be implemented I would be very thankful to hear your thoughts!

I also tried to use rollup-plugin-bundle-worker but also without any luck yet

I haven鈥檛 had success with this yet, but another approach we have on our todo list is using React.renderToNodeStream to make the React work async 鈥斅爓ould this be possible with react-pdf? We may still need to move yoga-layout to another thread, but making React鈥檚 render process non-blocking should help a lot.

Hello @DuncanMacWeb @diegomura,
Would you like to share any experience or start-point on that? Or did anyone of you had some progress?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

yellowBanano picture yellowBanano  路  3Comments

foureyedraven picture foureyedraven  路  3Comments

jbrat picture jbrat  路  3Comments

pavle-lekic-htec picture pavle-lekic-htec  路  4Comments

dvenkatsagar picture dvenkatsagar  路  3Comments