Share
Embed
Solving the Layout Shift Problem in LINE NEWS
Akinori Inoue
LINE / Front-end Dev Team1 / Front-end Engineer
https://linedevday.linecorp.com/2021/ja/sessions/97
https://linedevday.linecorp.com/2021/en/sessions/97
https://linedevday.linecorp.com/2021/ko/sessions/97
LINE DEVDAY 2021
November 11, 2021
Transcript
124 slides
-
Agenda - Overview of LINE NEWS - Front-end problems LINE
NEWS faces - Implementation of the skeleton screen - Two problems in our own skeleton screen - What changed and future challenges -
Agenda - Overview of LINE NEWS - Front-end problems LINE
NEWS faces - Implementation of the skeleton screen - Two problems in our own skeleton screen - What changed and future challenges -
LINE NEWS - MAU: 77 million - MPV: 15.4 billion
- A large scale of development - Web front-end development -
- Technology - TypeScript - React NEWS Tab - Features
- Pages are arranged in tabs - Multiple gateways to articles -
Agenda - Overview of LINE NEWS - Front-end problems LINE
NEWS faces - Implementation of the skeleton screen - Two problems in our own skeleton screen - What changed and future challenges -
Personalization API Features Affects every user Response time is slow
Cannot use CDN cache Articles related to cars -
A/B Testing API Features Expose different UIs for sets of
users Release features gradually to users User A User B -
- Wait for all APIs to be executed ︙ Conditions
in which the skeleton screen disappears -
- Wait for all APIs to be executed Not ideal
︙ Conditions in which the skeleton screen disappears -
Not ideal - Wait for some APIs - Wait for
all APIs to be executed ︙ Conditions in which the skeleton screen disappears -
Not ideal - Wait for some APIs - Wait for
all APIs to be executed ︙ Not ideal Conditions in which the skeleton screen disappears -
Target Target Target User don't care if a layout shift
occurs In viewport Minimum conditions -
APIs + Heights of contents Conditions in which the skeleton
screen disappears = -
Front-end problems LINE NEWS faces - The adaptation of skeleton
screens - The problem: the layout shift -
Agenda - Overview of LINE NEWS - Front-end problems LINE
NEWS faces - Implementation of the skeleton screen - Two problems in our own skeleton screen - What changed and future challenges -
Agenda - Overview of LINE NEWS - Front-end problems LINE
NEWS faces - Implementation of the skeleton screen - Front-end architecture of NEWS Tab - Skeleton screen components - Two heights required to hide the skeleton screen - How and when the content height is determined - Utilities - Two problems in our own skeleton screen - What changed and future challenges -
Agenda - Overview of LINE NEWS - Front-end problems LINE
NEWS faces - Implementation of the skeleton screen - Front-end architecture of NEWS Tab - Skeleton screen components - Two heights required to hide the skeleton screen - How and when the content height is determined - Utilities - Two problems in our own skeleton screen - What changed and future challenges -
Front-end architecture of NEWS Tab APIs Stores ︙ <Root />
<Tab /> <Tab /> <Tab /> Actions -
APIs Stores Actions ︙ <Root /> <Tab /> <Tab />
<Tab /> Front-end architecture of NEWS Tab -
APIs Stores Actions ︙ <Root /> <Tab /> <Tab />
<Tab /> Front-end architecture of NEWS Tab -
APIs Actions ︙ Stores <Root /> <Tab /> <Tab />
<Tab /> Front-end architecture of NEWS Tab -
︙ <Root /> <Tab /> <Tab /> <Tab /> Front-end
architecture of NEWS Tab Stores Actions APIs -
Front-end architecture of NEWS Tab APIs Stores ︙ <Root />
<Tab /> <Tab /> <Tab /> Actions -
Agenda - Overview of LINE NEWS - Front-end problems LINE
NEWS faces - Implementation of the skeleton screen - Front-end architecture of NEWS Tab - Skeleton screen components - Two heights required to hide the skeleton screen - How and when the content height is determined - Utilities - Two problems in our own skeleton screen - What changed and future challenges -
Agenda - Overview of LINE NEWS - Front-end problems LINE
NEWS faces - Implementation of the skeleton screen - Front-end architecture of NEWS Tab - Skeleton screen components - Two heights required to hide the skeleton screen - How and when the content height is determined - Utilities - Two problems in our own skeleton screen - What changed and future challenges -
Apply skeleton screen components APIs Stores Actions ︙ <Root />
<Tab /> <Tab /> <Tab /> -
APIs Stores Actions ︙ <Root /> <Skeleton> <Tab /> </Skeleton>
<Skeleton> <Tab /> </Skeleton> <Skeleton> <Tab /> </Skeleton> Apply skeleton screen components -
- A component with only style applied - Does not
contain business logic - Switch the display of skeleton screen component - Manage fade-out animation <SkeletonScreen /> <SkeletonOverlap /> Two components used to create the skeleton screen -
- Switch the display of skeleton screen component - Manage
fade-out animation <SkeletonScreen /> <SkeletonOverlap /> The SkeletonScreen component: 1/2 - A component with only style applied - Does not contain business logic -
export const SkeletonScreen = (props: Props) => { return (
<div className="skeleton” onTransitionEnd={props.onTransitionEnd}> <div className="loading" /> <div className="component1"> ... </div> ... </div> ); }; <SkeletonScreen /> The SkeletonScreen component: 1/2 -
<SkeletonScreen /> <SkeletonOverlap /> The SkeletonOverlap component: 2/2 - A
component with only style applied - Does not contain business logic - Switch the display of skeleton screen component - Manage fade-out animation -
export const SkeletonOverlap = props => { const [state, done]
= useAnimationSkeletonOverlap(props.readyToHide); return ( <div className={transformAnimationToStyle(state)}> {state !== ANIMATION_DONE && ( <SkeletonScreen onTransitionEnd={done} /> )} {props.children} </div> ); }; <SkeletonOverlap /> The SkeletonOverlap component: 2/2 -
export const SkeletonOverlap = props => { const [state, done]
= useAnimationSkeletonOverlap(props.readyToHide); return ( <div className={transformAnimationToStyle(state)}> {state !== ANIMATION_DONE && ( <SkeletonScreen onTransitionEnd={done} /> )} {props.children} </div> ); }; <SkeletonOverlap /> The SkeletonOverlap component: 2/2 -
export const SkeletonOverlap = props => { const [state, done]
= useAnimationSkeletonOverlap(props.readyToHide); return ( <div className={transformAnimationToStyle(state)}> {state !== ANIMATION_DONE && ( <SkeletonScreen onTransitionEnd={done} /> )} {props.children} </div> ); }; <SkeletonOverlap /> The SkeletonOverlap component: 2/2 -
export const SkeletonOverlap = props => { const [state, done]
= useAnimationSkeletonOverlap(props.readyToHide); return ( <div className={transformAnimationToStyle(state)}> {state !== ANIMATION_DONE && ( <SkeletonScreen onTransitionEnd={done} /> )} {props.children} </div> ); }; <SkeletonOverlap /> The SkeletonOverlap component: 2/2 -
Agenda - Overview of LINE NEWS - Front-end problems LINE
NEWS faces - Implementation of the skeleton screen - Front-end architecture of NEWS Tab - Skeleton screen components - Two heights required to hide the skeleton screen - How and when the content height is determined - Utilities - Two problems in our own skeleton screen - What changed and future challenges -
(1) The height of viewport (The skeleton screen) (2) The
total height of fixed contents If (2) is greater than (1), the skeleton screen disappears Conditions in which the skeleton screen disappears -
Manage the display of skeleton screens APIs Actions ︙ Stores
<Root /> <SkeletonOverlap> <Tab /> </SkeletonOvelap> <SkeletonOverlap> <Tab /> </SkeletonOvelap> <SkeletonOverlap> <Tab /> </SkeletonOvelap> -
The skeleton screen store export interface SkeletonOverlapState { screenHeight: number;
tabs: SkeletonOverlapTab[]; } Stores -
The skeleton screen store export interface SkeletonOverlapState { screenHeight: number;
tabs: SkeletonOverlapTab[]; } -
export interface SkeletonOverlapTab { [index: number]: { elementHeight: number; };
totalElementHeight: number; readyToHide: boolean; } export interface SkeletonOverlapState { screenHeight: number; tabs: SkeletonOverlapTab[]; } new ResizeObserver(() => { setScreenSize(document.documentElement.clientHeight); }); The skeleton screen store -
export interface SkeletonOverlapState { screenHeight: number; tabs: SkeletonOverlapTab[]; } export
interface SkeletonOverlapTab { [index: number]: { elementHeight: number; }; totalElementHeight: number; readyToHide: boolean; } The skeleton screen store -
export interface SkeletonOverlapTab { [index: number]: { elementHeight: number; };
totalElementHeight: number; readyToHide: boolean; } export interface SkeletonOverlapState { screenHeight: number; tabs: SkeletonOverlapTab[]; } The skeleton screen store -
export interface SkeletonOverlapState { screenHeight: number; tabs: SkeletonOverlapTab[]; } export
interface SkeletonOverlapTab { [index: number]: { elementHeight: number; }; totalElementHeight: number; readyToHide: boolean; } The skeleton screen store -
export interface SkeletonOverlapState { screenHeight: number; tabs: SkeletonOverlapTab[]; } export
interface SkeletonOverlapTab { [index: number]: { elementHeight: number; }; totalElementHeight: number; readyToHide: boolean; } The skeleton screen store -
export interface SkeletonOverlapState { screenHeight: number; tabs: SkeletonOverlapTab[]; } export
interface SkeletonOverlapTab { [index: number]: { elementHeight: number; }; totalElementHeight: number; readyToHide: boolean; } The skeleton screen store -
Manage the display of skeleton screens APIs Actions ︙ Stores
<Root /> <SkeletonOverlap> <Tab /> </SkeletonOvelap> <SkeletonOverlap> <Tab /> </SkeletonOvelap> <SkeletonOverlap> <Tab /> </SkeletonOvelap> -
Agenda - Overview of LINE NEWS - The layout shift
problem and the adaptation of skeleton screens - Implementation of the skeleton screen - Front-end architecture of NEWS Tab - Skeleton screen components - Two heights required to hide the skeleton screen - How and when the content height is determined - Utilities - Two problems in our own skeleton screen - What changed and future challenges -
Fixed Fixed Fixed Content Store Content Store Content Store API
API API Data status Data status Data status How and when the content height is determined -
Data status Success Pending None The data doesn't exist yet
Data is available Not ready to render Not ready to render Ready to render In the process of retrieving data -
Data status Whether the content can be rendered None Pending
Success 㾎 Failed 㾎 Cached 㾎 Cached & Pending 㾎 -
Data status Stores Actions (Dispatcher) APIs getArticles() { return API.fetchArticles(),
.then((articles) => { dispatcher.dispatch({ type: ACTION_TYPE_GET_ARTICLES, articles, }); }); } -
Data status Stores Actions (Dispatcher) APIs getArticles() { return API.fetchArticles(),
.then((articles) => { dispatcher.dispatch({ type: ACTION_TYPE_GET_ARTICLES, articles, }); }); } -
APIs Data status Stores Actions (Dispatcher) getArticles() { return API.fetchArticles(),
.then((articles) => { dispatcher.dispatch({ type: ACTION_TYPE_GET_ARTICLES, articles, }); }); } -
Data status getArticles() { return API.fetchArticles(), .then((articles) => { dispatcher.dispatch({
type: ACTION_TYPE_GET_ARTICLES, articles, }); }); } getArticles() { dispatcher.dispatch({ type: ACTION_TYPE_GET_ARTICLE_PENDING, }); return API.fetchArticles(), .then((articles) => { dispatcher.dispatch({ type: ACTION_TYPE_GET_ARTICLES_SUCCESS, articles, }); }) .catch(() => { dispatcher.dispatch ({ type: ACTION_TYPE_GET_ARTICLES_FAILED, }); }); } -
Data status None Data status flows getArticles() { return API.fetchArticles(),
.then((articles) => { dispatcher.dispatch({ type: ACTION_TYPE_GET_ARTICLES, articles, }); }); } getArticles() { dispatcher.dispatch({ type: ACTION_TYPE_GET_ARTICLE_PENDING, }); return API.fetchArticles(), .then((articles) => { dispatcher.dispatch({ type: ACTION_TYPE_GET_ARTICLES_SUCCESS, articles, }); }) .catch(() => { dispatcher.dispatch ({ type: ACTION_TYPE_GET_ARTICLES_FAILED, }); }); } -
getArticles() { return API.fetchArticles(), .then((articles) => { dispatcher.dispatch({ type: ACTION_TYPE_GET_ARTICLES,
articles, }); }); } getArticles() { dispatcher.dispatch({ type: ACTION_TYPE_GET_ARTICLE_PENDING, }); return API.fetchArticles(), .then((articles) => { dispatcher.dispatch({ type: ACTION_TYPE_GET_ARTICLES_SUCCESS, articles, }); }) .catch(() => { dispatcher.dispatch ({ type: ACTION_TYPE_GET_ARTICLES_FAILED, }); }); } Data status None Pending Data status flows -
Data status None Data status flows Success Pending getArticles() {
return API.fetchArticles(), .then((articles) => { dispatcher.dispatch({ type: ACTION_TYPE_GET_ARTICLES, articles, }); }); } getArticles() { dispatcher.dispatch({ type: ACTION_TYPE_GET_ARTICLE_PENDING, }); return API.fetchArticles(), .then((articles) => { dispatcher.dispatch({ type: ACTION_TYPE_GET_ARTICLES_SUCCESS, articles, }); }) .catch(() => { dispatcher.dispatch ({ type: ACTION_TYPE_GET_ARTICLES_FAILED, }); }); } -
Data status getArticles() { return API.fetchArticles(), .then((articles) => { dispatcher.dispatch({
type: ACTION_TYPE_GET_ARTICLES, articles, }); }); } getArticles() { dispatcher.dispatch({ type: ACTION_TYPE_GET_ARTICLE_PENDING, }); return API.fetchArticles(), .then((articles) => { dispatcher.dispatch({ type: ACTION_TYPE_GET_ARTICLES_SUCCESS, articles, }); }) .catch(() => { dispatcher.dispatch ({ type: ACTION_TYPE_GET_ARTICLES_FAILED, }); }); } None Success Failed Data status flows Pending -
Dependencies on multiple data status Fixed Fixed Fixed Content Store
Content Store Content Store Data status Data status Data status -
Fixed Fixed Content Store Content Store Content Store Data status
Data status Content Store Data status Fixed Dependencies on multiple data status -
Data A Data B Data C AND AND = Dependencies
on multiple data status -
Data A Data B Data C AND AND = Success
Cached Pending Not Ready Dependencies on multiple data status -
Data A Data B Data C AND AND = Success
Cached Failed Ready Dependencies on multiple data status -
Fixed Fixed Fixed Content Store Content Store Content Store Data
status Data status Content Store Data status Store parameters to determine when each skeleton screen disappears -
Content Store Content Store Content Store Data status Data status
Content Store SkeletonScreen Store Data status Height Height Height Store parameters to determine when each skeleton screen disappears -
SkeletonScreen Store Height, Index Height, Index Height, Index Content Store
Content Store Content Store Data status Data status Content Store Data status Store parameters to determine when each skeleton screen disappears -
Content A Content B Content C Not just the total
height but the order matters, too -
Ready Ready Not Ready Content A Content B Content C
Not just the total height but the order matters, too -
Content A Content C + > = Height of viewport
Should we hide the skeleton in this case? Not just the total height but the order matters, too -
Ready Ready Not Ready Ready Ready Ready Content A Content
B Content C Content A Content B Content C Not just the total height but the order matters, too -
Ready Ready Not Ready Content A Content B Content C
Not just the total height but the order matters, too -
Ready Ready Not Ready Content A Content B Content C
Content A Content B Content C Ready Ready Not Ready Not just the total height but the order matters, too -
Ready Ready Not Ready Content A Content B Content C
Content A Content B Content C Ready Ready Not Ready Not just the total height but the order matters, too -
SkeletonScreen Store Height, Index Height, Index Height, Index Content Store
Content Store Content Store Data status Data status Content Store Data status Store parameters to determine when each skeleton screen disappears -
Agenda - Overview of LINE NEWS - Front-end problems LINE
NEWS faces - Implementation of the skeleton screen - Front-end architecture of NEWS Tab - Skeleton screen components - Two height to disappear the skeleton screen - How and when the content height is determined - Utilities - Two problems in our own skeleton screen - What changed and future challenges -
Utilities For hooks export function useRenderReady<T extends HTMLElement>({ dataStatus, onRenderReady,
}) { … } For class components export function RenderReadyWrapper<T extends HTMLElement = HTMLElement>({ dataStatus, onRenderReady, childrenCallback, }: RenderReadyWrapperProps<T>) { const rootRef = useRenderReady<T>({ … }); return childrenCallback(rootRef); } -
Utilities For hooks export function useRenderReady<T extends HTMLElement>({ dataStatus, onRenderReady,
}: UseRenderReadyParams<T>) { const rootRef = useRef<T>(null); … return rootRef; } -
Utilities Usage const Content = (props) => { const ref
= useRenderReady({ onRenderReady: (elementHeight) => { readyContent( props.tabIndex, props.contentIndex, props.elementHeight ); }, dataStatus: [props.dataStatus1, props.dataStatus2], }); return <div ref={ref} >… </div>; } -
Utilities Usage const Content = (props) => { const ref
= useRenderReady({ onRenderReady: (elementHeight) => { readyContent( props.tabIndex, props.contentIndex, props.elementHeight ); }, dataStatus: [props.dataStatus1, props.dataStatus2], }); return <div ref={ref} >… </div>; } useRenderReady <div /> -
Utilities Usage const Content = (props) => { const ref
= useRenderReady({ onRenderReady: (elementHeight) => { readyContent( props.tabIndex, props.contentIndex, props.elementHeight ); }, dataStatus: [props.dataStatus1, props.dataStatus2], }); return <div ref={ref} >… </div>; } useRenderReady Data status ︙ Data status -
Utilities Usage const Content = (props) => { const ref
= useRenderReady({ onRenderReady: (elementHeight) => { readyContent( props.tabIndex, props.contentIndex, props.elementHeight ); }, dataStatus: [props.dataStatus1, props.dataStatus2], }); return <div ref={ref} >… </div>; } -
Utilities Usage const Content = (props) => { const ref
= useRenderReady({ onRenderReady: (elementHeight) => { readyContent( props.tabIndex, props.contentIndex, props.elementHeight ); }, dataStatus: [props.dataStatus1, props.dataStatus2], }); return <div ref={ref} >… </div>; } -
Usage const Content = (props) => { const ref =
useRenderReady({ onRenderReady: (elementHeight) => { readyContent( props.tabIndex, props.contentIndex, props.elementHeight ); }, dataStatus: [props.dataStatus1, props.dataStatus2], }); return <div ref={ref} >… </div>; } Utilities -
Utilities Usage const Content = (props) => { const ref
= useRenderReady({ onRenderReady: (elementHeight) => { readyContent( props.tabIndex, props.contentIndex, props.elementHeight ); }, dataStatus: [props.dataStatus1, props.dataStatus2], }); return <div ref={ref} >… </div>; } -
Agenda - Overview of LINE NEWS - Front-end problems LINE
NEWS faces - Implementation of the skeleton screen - Two problems in our own skeleton screen - What changed and future challenges -
Our own skeleton screen Broken Ideal skeleton screen Comparisons in
skeleton screen methods -
Ideal skeleton screen Broken Broken Our own skeleton screen Comparisons
in skeleton screen methods -
Two problems in our own skeleton screen Skeleton screens not
disappearing Hard to detect where the problem is occurring -
Set a timeout for displaying the skeleton screen The solutions
Skeleton screens not disappearing Hard to detect where the problem is occurring -
Provide a dedicated debugging mechanism Set a timeout for displaying
the skeleton screen Skeleton screens not disappearing Hard to detect where the problem is occurring The solutions -
A debugger for the skeleton screen - Enable or disabled
- Select a tab - The height of viewport - The height of contents - Ready or not status for contents - The index of contents - Targeted contents for the skeleton screen disappearing - Number of consecutive contents -
- Enable or disabled - Select a tab - The
height of viewport - The height of contents - Ready or not status for contents… ① - The index of contents - Targeted contents for the skeleton screen disappearing… ② - Number of consecutive contents A debugger for the skeleton screen ① Ready or not -
① Ready or not - Enable or disabled - Select
a tab - The height of viewport - The height of contents - Ready or not status for contents… ① - The index of contents - Targeted contents for the skeleton screen disappearing… ② - Number of consecutive contents A debugger for the skeleton screen Highlighted in red ② -
Agenda - Overview of LINE NEWS - Front-end problems LINE
NEWS faces - Implementation of the skeleton screen - Two problems in our own skeleton screen - What changed and future challenges -
Cumulative Layout Shift (CLS) 0.348 0.002 “To provide a good
user experience, sites should strive to have a CLS score of 0.1 or less.” https://web.dev/i18n/en/cls/