Code Monkey home page Code Monkey logo

Comments (3)

philipwalton avatar philipwalton commented on May 23, 2024 2

You do not need to wait for DCL to use any of these APIs.

As @Zizzamia mentions, PerformanceObserver now has buffering, which means you can observe entries both before or after they happen and still have the callback invoked.

But then further down you do:

The only reason that example waits for DOMContentLoaded is because it's loading the UMD version of the script via a separate script tag (note how there are two script tags in that example). The DOMContentLoaded check is not to wait to call the functions, it's waiting to ensure the first script is loaded and the webVitals global variable is ready.

Note, you do not need to wait if you're loading via <script type=module> or using a module bundler (either of which are recommended over using the UMD version).

from web-vitals.

philipwalton avatar philipwalton commented on May 23, 2024 1

Also I know TTFB and in particular performance.getEntriesByType('navigation') is not available right away, that's why we do the afterLoad callback internally.

Mention these, I personally have some confusion on this topic as well. @philipwalton what's your thoughts on this, and does the Chrome team have more research on this topic?

In general, the recommended approach for the future is to not use performance.getEntries... and to use PerformanceObserver instead—for exactly the issue you raised.

The only reason I used performance.getEntriesByType('navigation') in this library is to get wider browser support, but as you noted that requires waiting until after the load event.

from web-vitals.

Zizzamia avatar Zizzamia commented on May 23, 2024

Generally, all Performance Observer values are buffered (check https://github.com/GoogleChrome/web-vitals/blob/master/src/lib/observe.ts#L38), to be even accessible at a later stage of the application.

The specs says

There are special considerations regarding initial page load when using the PerformanceObserver interface: a registration must be active to receive events but the registration script may not be available or may not be desired in the critical path. To address this, user agents buffer some number of events while the page is being constructed, and these buffered events can be accessed via the buffered flag when registering the observer. When this flag is set, the user agent retrieves and dispatches events that it has buffered, for the specified entry type, and delivers them in the first callback after the observe() call occurs.

Also I know TTFB and in particular performance.getEntriesByType('navigation') is not available right away, that's why we do the afterLoad callback internally.

Mention these, I personally have some confusion on this topic as well. @philipwalton what's your thoughts on this, and does the Chrome team have more research on this topic?

from web-vitals.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.