fix: Add delay to ensure content is rendered before capturing #770
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds a small 10ms delay after cloning nodes, to improve the chances that the content is properly rendered before capturing with html2canvas. This isn't ideal (it'll make every call slower by 10ms and still doesn't guarantee anything), but should reduce flakiness.
This was impacting vdiffs and some local demos, which suggests that it may be a noticeable problem in prod as well. It was causing images to sometimes be cut off - the page height was calculated too small, since content wasn't fully rendered and hadn't reflowed to their final sizes.
A better solution would involve not cloning the content myself at all, and leaving window sizing etc to the image-capturing tool (html2canvas). There's a branch I created way back in 2019 to do just that:
https://github.com/eKoopmans/html2pdf.js/tree/bugfix/clone-nodes
Didn't want to jump straight to that solution though, because it'll introduce its own issues and will be a breaking change (no
toContainerstep etc).