fix: Layout shift before mounted should trigger relayouts #20
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 fixes #18 and #8. There're other reports such as https://twitter.com/sebastienlorber/status/1610230370531688451, and potentially https://twitter.com/nicopellerin_io/status/1608230343907082245.
The cause for this issue is mostly related to web fonts. A common scenario is when the
page.js
script loads slower than the font, this happens in order:<h1>
and inlined<script>
executed, rebalanced → ✅page.js
loaded and React hydrated, registered Resize Observer and rebalanced the title → ✅But if 2) and 3) swaps, it’s not a problem.
The solution here is simply register the Resize Observer ASAP, before other potential layout shifts. That is, putting it inside the inlined script tag too.
But this solution makes the script tag larger which is bad when you have many titles. Hence in this PR I added a
<Provider>
API, which inserts the global<script>
tag before anything else. All<Balancer>
components inside can know that and skip inserting that part. It's a good optimization and totally optional and non-breaking.Note that it can still happen without web font. Any CSS or JS caused layout shift for that title happened before hydration might cause an issue like #18.