Skip to content

fix: yoga error and infinite loop while page breaking#3190

Merged
diegomura merged 2 commits intodiegomura:masterfrom
carlobeltrame:fix-invalid-array-length
Sep 23, 2025
Merged

fix: yoga error and infinite loop while page breaking#3190
diegomura merged 2 commits intodiegomura:masterfrom
carlobeltrame:fix-invalid-array-length

Conversation

@carlobeltrame
Copy link
Collaborator

@carlobeltrame carlobeltrame commented Jul 15, 2025

Part 1 of this PR:
Always return both height and width when measuring text
Fixes #2974
Returning an undefined width translated into a NaN width when passing the measurement result back to yoga. This NaN width would then be converted to 0 and a warning as follows would be emitted: Measure function returned an invalid dimension to Yoga: [width=nan, height=21.779297]
The warnings in yoga's javascript bindings are stored into a long array buffer. If a document is very large (> 100 pages) and contains a lot of text nodes, this can produce millions of yoga warnings, inflating this array buffer, until at some point the browser refuses to push another character onto the end of the buffer. This results in the error RangeError: Invalid array length at Array.push ()

Part 2 of this PR:
Try harder to avoid infinite loops while page splitting
Fixes #2884
Fixes #2895
Fixes more issues reported in #2974
When a node is already at the top of the page, it does not help to break it to the next page. We knew that already, but the implementation of this knowledge has been flawed. Apparently, page paddings, page borders, fixed elements and possibly other things can cause the previous position-based detection method to not work correctly.
This commit replaces the flawed implementation with a hopefully more solid one: When there are no non-fixed nodes before the current one on the page, we can assume that the current node will not be placed higher up on the page even if we were to break it to the next page.

Fixes diegomura#2974
Returning an undefined width translated into a NaN width when passing
the measurement result back to yoga. This NaN width would then be
converted to 0 and a warning as follows would be emitted:
Measure function returned an invalid dimension to Yoga: [width=nan,
height=21.779297]
The warnings in yoga's javascript bindings are stored into a long
array buffer. If a document is very large (> 100 pages) and contains
a lot of text nodes, this can produce millions of yoga warnings,
inflating this array buffer, until at some point the browser refuses to
push another character onto the end of the buffer. This results in the
error `RangeError: Invalid array length at Array.push ()`
@changeset-bot
Copy link

changeset-bot bot commented Jul 15, 2025

🦋 Changeset detected

Latest commit: 3789d74

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 7 packages
Name Type
@react-pdf/layout Patch
@react-pdf/renderer Patch
next-14 Patch
next-15 Patch
@react-pdf/vite-example Patch
@react-pdf/e2e-node-cjs Patch
@react-pdf/e2e-node-esm Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@sagar-jani
Copy link

Thanks for the PR, i can't find this file in node_modules - packages/layout/src/text/measureText.ts, is it part of @react-pdf ?

@carlobeltrame
Copy link
Collaborator Author

Thanks for the PR, i can't find this file in node_modules - packages/layout/src/text/measureText.ts, is it part of @react-pdf ?

It is, otherwise I couldn't have created this PR. Please double check you have installed the package correctly according to the documentation.

When a node is already at the top of the page, it does not help to break
it to the next page. We knew that already, but the implementation of
this knowledge has been flawed. Apparently, page paddings, page borders,
fixed elements and possibly other things can cause the previous
position-based detection method to not work correctly.

This commit replaces the flawed implementation with a hopefully more
solid one: When there are no non-fixed nodes before the current one on
the page, we can assume that the current node will not be placed higher
up on the page even if we were to break it to the next page.

Fixes diegomura#2884
Fixes diegomura#2895
Fixes further parts of diegomura#2974
@carlobeltrame carlobeltrame force-pushed the fix-invalid-array-length branch from dcb3081 to 3789d74 Compare August 18, 2025 20:26
@carlobeltrame carlobeltrame changed the title Always return both height and width when measuring text Fix yoga error and infinite loop while page breaking Aug 18, 2025
Copy link

@Chivas1717 Chivas1717 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great fix! let's merge asap

@carlobeltrame
Copy link
Collaborator Author

Thanks everyone for testing the fixes!
@diegomura would you care to review? These problems have reportedly affected a number of users now, and part of the problems were caused by my PR #2400, so I'd like to get this situation sorted out if at all possible.

@KaramveerSidhu
Copy link

Thanks for the fix @carlobeltrame. I can also confirm that we faced the same problem when generating PDFs greater than 100 pages, and patching the fix, works perfectly. However patching in production is difficult to maintain, and we are waiting for this PR to be merged.

@Stith87
Copy link

Stith87 commented Sep 8, 2025

Any plans to get this merged in soon? This error is causing significant issues for my org and would love to get this fix in. @diegomura

@brianshultz
Copy link

brianshultz commented Sep 16, 2025

@diegomura mind taking a look at this one? Have a few customers that are having issues as a result of this bug. Would appreciate a speedy merge!

@mohsom
Copy link

mohsom commented Sep 17, 2025

pls merge

@roger-serra-leanon
Copy link

Fix worked for me. But I am not sure if related, it's only working if I use a standard page size like 'letter', 'a4', etc. The process just hangs if a custom number is set. Using @react-pdf/renderer": "4.3.0 and react 19.

@diegomura diegomura changed the title Fix yoga error and infinite loop while page breaking fix: yoga error and infinite loop while page breaking Sep 23, 2025
@diegomura diegomura merged commit a99d10f into diegomura:master Sep 23, 2025
@carlobeltrame
Copy link
Collaborator Author

Fix worked for me. But I am not sure if related, it's only working if I use a standard page size like 'letter', 'a4', etc. The process just hangs if a custom number is set. Using @react-pdf/renderer": "4.3.0 and react 19.

@roger-serra-leanon interesting. Can you provide a minimal reproduction example, e.g. using https://react-pdf.org/repl ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RangeError: Invalid array length at Array.push (<anonymous>) Infinite loop when splitting pages

9 participants