You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Feat]: asyncInit: measure from a ResizeObserver, and correctly under a container transform
#1417
This is a follow-up to #1327 and #1328 (both closed) and to the answer on #1327. CONTRIBUTING asks for a discussion before a feature PR, which those PRs skipped, so this is that discussion. We are not asking to reopen them.
What we took from the answer
Skipping the inline transform = 'none' write mis-measures when a stylesheet applies the transform (the SSR plugin does). We agree and dropped it. Measured on its own it saved well under a millisecond, so it was not worth it.
A getComputedStyle check would force style and layout itself, so a synchronous measurement at init can't avoid the reflow.
Not initializing carousels that are out of view is the best optimization. We do that. This proposal is for carousels that are in view on first paint, where deferring isn't an option.
Proposal
An opt-in asyncInit option (default false). When true, activate measures from a ResizeObserver callback, once the browser has already laid the page out, then creates the engine there and emits reinit. With it off, nothing changes.
Numbers
Production Next.js build, a page with two carousels (19 slides), headless Chrome, CPU throttled 4x, three batches of eight runs each:
sync init (stock)
asyncInit
Total blocking time
183, 187, 189 ms
117, 118, 130 ms
CLS (mobile viewport, Slow 4G)
0.0000
0.0000
LCP was too noisy to report. In an isolated microbenchmark of just the init call (12 slides, 600-row page, 4x throttle), the call dropped from 4.3 ms to 0.1 ms, but the carousel is ready one frame later (about 16 ms). The layout work moves into that frame; it isn't removed. This is lab data, not field INP. A user on #1328 reported Lighthouse's Forced Reflow finding mostly gone.
A correctness finding
When the container has a CSS transform, translate, or will-change: transform, it becomes its slides' offsetParent, so their offsetLeft and offsetTop become container-relative. On a six-slide test with the default body margin:
container CSS
stock
observer path as first written
observer path, normalized
none
0, .3185, .6815, 1
same
same
transform: translateX(37px)
0, .3185, .6815, 1
0, .3135, .6791, 1
0, .3185, .6815, 1
translate: 37px 0
0, .3135, .6791, 1
0, .3135, .6791, 1
0, .3185, .6815, 1
will-change: transform
0, .3135, .6791, 1
0, .3135, .6791, 1
0, .3185, .6815, 1
So stock's transform: none reset handles transform but not translate or will-change. In the observer callback layout is already done, so the slides can be put back in the container's frame without a write or a reflow:
constx=container.offsetLeft+container.clientLeftconsty=container.offsetTop+container.clientTop// for each slide whose offsetParent is the container:// left += x, right += x, top += y, bottom += y
With that, the observer path gives the untransformed snaps in every case above, which would also make it safe alongside the SSR plugin.
Questions
Would you take this as an opt-in option? If you prefer another name or shape for deferring measurement, we'd follow that.
Public methods must be safe before the engine exists. We have guards on reActivate, deActivate, goTo, canGoToNext, canGoToPrev and cloneEngine; the other API methods would need the same.
If it's welcome, we'll open a PR on feature/#<issue> after your direction, with tests.
This was investigated and written with AI assistance (Claude Code); the numbers come from scripts we ran, and we can share them.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
This is a follow-up to #1327 and #1328 (both closed) and to the answer on #1327. CONTRIBUTING asks for a discussion before a feature PR, which those PRs skipped, so this is that discussion. We are not asking to reopen them.
What we took from the answer
transform = 'none'write mis-measures when a stylesheet applies the transform (the SSR plugin does). We agree and dropped it. Measured on its own it saved well under a millisecond, so it was not worth it.getComputedStylecheck would force style and layout itself, so a synchronous measurement at init can't avoid the reflow.Proposal
An opt-in
asyncInitoption (defaultfalse). Whentrue,activatemeasures from aResizeObservercallback, once the browser has already laid the page out, then creates the engine there and emitsreinit. With it off, nothing changes.Numbers
Production Next.js build, a page with two carousels (19 slides), headless Chrome, CPU throttled 4x, three batches of eight runs each:
asyncInitLCP was too noisy to report. In an isolated microbenchmark of just the init call (12 slides, 600-row page, 4x throttle), the call dropped from 4.3 ms to 0.1 ms, but the carousel is ready one frame later (about 16 ms). The layout work moves into that frame; it isn't removed. This is lab data, not field INP. A user on #1328 reported Lighthouse's Forced Reflow finding mostly gone.
A correctness finding
When the container has a CSS
transform,translate, orwill-change: transform, it becomes its slides'offsetParent, so theiroffsetLeftandoffsetTopbecome container-relative. On a six-slide test with the defaultbodymargin:0, .3185, .6815, 1transform: translateX(37px)0, .3185, .6815, 10, .3135, .6791, 10, .3185, .6815, 1translate: 37px 00, .3135, .6791, 10, .3135, .6791, 10, .3185, .6815, 1will-change: transform0, .3135, .6791, 10, .3135, .6791, 10, .3185, .6815, 1So stock's
transform: nonereset handlestransformbut nottranslateorwill-change. In the observer callback layout is already done, so the slides can be put back in the container's frame without a write or a reflow:With that, the observer path gives the untransformed snaps in every case above, which would also make it safe alongside the SSR plugin.
Questions
reActivate,deActivate,goTo,canGoToNext,canGoToPrevandcloneEngine; the other API methods would need the same.If it's welcome, we'll open a PR on
feature/#<issue>after your direction, with tests.This was investigated and written with AI assistance (Claude Code); the numbers come from scripts we ran, and we can share them.
All reactions