Unloading earlier posts in PostStream shifts the content that is on screen (CLS ~1.0 per trim)

Abierto
#5,008 1 comentario 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
48/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
javascript, typescript

Línea de trabajo

Comienza con PostStreamState.ts en _loadNext() y loadPage(), y después lee common/utils/anchorScroll.js para seguir el redibujado y la corrección del desplazamiento. Reprodúcelo en un viewport de 412x823 usando una discusión con más de 80 publicaciones y observa las entradas de layout-shift mientras se recorta. Se considera terminado cuando el desplazamiento ya no salta visiblemente ni registra desplazamientos grandes, mientras que la carga de publicaciones anteriores sigue siendo coherente con el rango conservado.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

Current Behavior

When you scroll deep into a discussion, PostStreamState#_loadNext() unloads the posts two pages back:

https://github.com/flarum/framework/blob/2.x/framework/core/js/src/forum/states/PostStreamState.ts#L187-L204

const twoPagesAway = start - PostStreamState.loadCount * 2;
if (twoPagesAway > this.visibleStart && twoPagesAway >= 0) {
  this.visibleStart = twoPagesAway + PostStreamState.loadCount + 1;
  ...
}

loadPage() wraps the resulting redraw in anchorScroll(), and the anchor element and arithmetic are correct — scrollY really does move by the height of the removed posts. But anchorScroll measures, runs m.redraw.sync(), and then sets scrollTop:

https://github.com/flarum/framework/blob/2.x/framework/core/js/src/common/utils/anchorScroll.js

The correction therefore lands after the redraw's layout, so the browser still records the movement. The result is a layout shift of ~1.0 every two pages of scrolling, attributed to the post items that are on screen at the time — plus a jump that visibly costs the reader their place.

Captured at the moment of a trim (mobile viewport 412x823):

range 0-60 -> 41-80,  DOM items 60 -> 39,  scrollY 17160 -> 6090
v=1.0
  DIV.PostStream-item[i=55]  prev y=0 h=0  -> cur y=0   h=400
  DIV.PostStream-item[i=56]  prev y=0 h=0  -> cur y=400 h=228
  DIV.PostStream-item[i=57]  prev y=0 h=0  -> cur y=628 h=195

Reading one 85-post thread end to end accumulates CLS 1.018. A 752-post thread accumulates ~18, one shift per trim. Desktop is much less affected (~0.003 on the same thread) because the moved area is a smaller fraction of a large viewport.

Steps to Reproduce
  1. Open a discussion with 80+ posts on a phone-sized viewport (412x823), throttled (~1.6 Mbps, 150 ms latency).
  2. Scroll down continuously past the third page of posts, until visibleStart moves forward.
  3. Observe the jump, and a layout-shift entry of ~1.0 whose sources are the on-screen PostStream-items.

Measured with a buffered PerformanceObserver on layout-shift (hadRecentInput excluded), scrolling with real wheel input via CDP Input.dispatchMouseEvent, headless Chrome 140, 4x CPU throttle. Reproduced identically on two rc.8 installs.

Expected Behavior

Trimming the stream should not move what the reader is looking at.

Options, in the order I'd rank them:

  1. Keep the space instead of the nodes — when posts are trimmed from the top, carry their measured height as padding on .PostStream and release it when loadPrevious restores them. Nothing below moves, no scroll manipulation is needed, and the DOM stays bounded.

    Worth flagging: I tried this as a small patch and it does not work that way. Adding the padding on trim without symmetric handling in loadPrevious makes the stream thrash — the scroll position stops matching the loaded range, loadPrevious fires, posts are re-added while the padding stays, and it compounds (on a 752-post thread the padding ran away to 250,000 px and visibleStart oscillated 21→42→44→46→68→49…). Doing it properly means making "space above" a single consistent notion handled in both directions.

  2. Correct the scroll without an intermediate layout — adjust the scroll position in the same layout pass as the removal rather than reading offsets around a sync redraw.

  3. Trim less often — widen the retention window, ideally viewport-aware. Reduces the frequency but does not remove the shift.

I'm running option 3 as an extension override in production (keep loaded posts, hand back to core above 200) which takes the 85-post thread from 1.018 to 0.018, but that is a mitigation and not the fix.

Environment
  • Flarum version: 2.0.0-rc.8
  • Website URL: https://pianoclack.com
  • Webserver: nginx
  • Hosting environment: VPS
  • PHP version: 8.4.24
  • Browser: Chrome 140 (mobile emulation, and reproduced on real Android Chrome field data)
Lenguaje dominante
PHP
Estrellas
6.8k
Forks
891
Merge medio
1 d 10 h
PR fusionados (30 d)
209

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de flarum/framework

Todos los issues de flarum/framework

Issues similares

Más issues de PHP

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.