Identifying the Cause of Poor TBT Performance

33 views
Skip to first unread message

Eugene

unread,
Jun 27, 2024, 8:23:15 AM (3 days ago) Jun 27
to web-vitals-feedback

I have a React and Next.js project, and I’m facing an issue. During PageSpeed tests, the TBT for the mobile version falls within the yellow zone.

I’ve already improved the FCP, LCP, Speed Index, and CLS metrics — all of which are now in the green zone. The overall PageSpeed score for mobile devices across 10 tests is 91 points. But the problem with TBT remains. And I want to fix it. 

What I've already done:

  • I switched the site to static generation.
  •  I eliminated all unnecessary libraries from the project.
  • I manually analyzed the minified code of all the files. I found unnecessary code there. I made changes that allowed me to get rid of unnecessary code

The current size of all js code for the mobile version of one page is about 160Kb. These 160Kb are divided between seven files. 

 What I tried to do, but it didn't help reduce the TBT score:

  • I disabled all analytics on the site. This did not result in TBT being in the green zone.
  • I tried adding content-visibility: auto. That didn't help either


Here’s the link to my site: https://pixelplex.io/custom-software-development/
Maybe you have some ideas on how to improve the TBT score for mobile devices?

Paul Irish

unread,
Jun 28, 2024, 7:57:26 AM (2 days ago) Jun 28
to Eugene, web-vitals-feedback
The TBT is influenced by long tasks.

Profiling with the Performance panel is the most direct way to see what those are.  Here's a trace of loading your site: https://trace.cafe/t/SltFXrCOzt
It was recorded on my M1 macbookpro, with 6x cpu throttling applied. 

The huge hydration task is the problem.
image.png

That javascript work needs to either be split up or reduced. https://web.dev/articles/optimize-long-tasks
However that will be hard given this is happening within React's internals. 

I'm not enough of a React expect to give you React-specific guidance.  
Perhaps React or Next tooling can help illuminate why the hydration costs are so high for completely static content.




--
You received this message because you are subscribed to the Google Groups "web-vitals-feedback" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web-vitals-feed...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/web-vitals-feedback/b9634839-5934-480b-9375-c48e421d6fe4n%40googlegroups.com.

Michal Mocny

unread,
Jun 28, 2024, 9:21:08 AM (2 days ago) Jun 28
to Paul Irish, Eugene, web-vitals-feedback
This site is using an older version of next.js with pages/ dir and React 17.

The pattern of hydration is common to those older versions: your whole application is server rendered, then client re-rendered, typically all in one long blocking task.  React 18+ adds features that solve this problem, and frameworks like Next.js + app/ dir automatically leverage these features.  However, if you are stuck with the current setup, there are some strategies you can adopt manually to at least get some similar benefits.

(I tried to save some time by asking Google Gemini to summarize these features, and I think it did a great job-- so pasting full response here)

-----

Prompt:

I know that React 18 supports progressive, selective hydration if you split your app up with <Suspense> boundaries, and that some frameworks, such as Next.js + app/ dir, will do a lot of this automatically across layout boundaries. Can you summarize the full effects of these features, and contrast to what was common in React 17 + Next.js pages/ dir? If you are stuck on the older version, can you manually enroll into some of those features somehow?

Reply:

Absolutely! Let's break down the hydration changes in React 18 and their implications, especially in the context of Next.js.

React 17 + Next.js (pages/ dir): Full Hydration

React 18: Selective and Progressive Hydration

React 18 introduces two key hydration improvements:

Next.js App Router (app/ dir): Automatic Hydration Optimization

  • Next.js's new App Router in the app/ directory leverages these React 18 features to automatically optimize hydration:
    • Automatic Partial Hydration: By default, each route segment in your app becomes a hydration boundary. Only the components for the currently active route are hydrated.
    • Streaming and Suspense: Next.js automatically streams HTML for faster initial rendering and uses <Suspense> for components that load data asynchronously, allowing parts of the page to become interactive while others are still loading.

Impact on User Experience

The combination of React 18's hydration improvements and Next.js's App Router leads to:

  • Faster Time to Interactive (TTI): Users can interact with parts of the page much sooner, even before all resources are fully loaded.
  • Smoother Interactions: The app feels more responsive because interactions aren't blocked by hydration of unrelated components.
  • Reduced JavaScript Execution: Only the necessary JavaScript is loaded and executed for the parts of the page the user is engaging with, saving resources.

React 17 + Next.js (pages/ dir) Workarounds

If you're stuck with the older pages/ directory, you can still get some hydration benefits manually:

Example (Code Splitting in pages/ dir)

JavaScript
import dynamic from 'next/dynamic';

const DynamicComponent = dynamic(() => import('../components/MyComponent'), {
  ssr: false, // Disable server-side rendering for this component
});

Important Note: While these workarounds can help, the best way to get the full benefits of React 18's hydration improvements is to upgrade to the latest versions and use the Next.js App Router if possible. It offers a much smoother and more automatic experience.

Let me know if you have any other questions!


Michal Mocny

unread,
Jun 28, 2024, 9:26:01 AM (2 days ago) Jun 28
to Paul Irish, Eugene, web-vitals-feedback

(woops, the pasted result was cut off half way-- let me try again)

Michal Mocny

unread,
Jun 28, 2024, 9:27:49 AM (2 days ago) Jun 28
to Paul Irish, Eugene, web-vitals-feedback
...nevermind, it just seems that the pasted text just doesn't render nicely in gmail.  I can see it if I try to "view entire message".  Hopefully you can see the results fine.

-Michal
Reply all
Reply to author
Forward
0 new messages