orestbida / cookieconsent

:cookie: Simple cross-browser cookie-consent plugin written in vanilla js
https://playground.cookieconsent.orestbida.com/
MIT License
4.06k stars 416 forks source link

[Bug]: INP issue in Google Search Console #738

Open bennetsadyba opened 1 month ago

bennetsadyba commented 1 month ago

Expected Behavior

No INP issues in Google Search Console, INP below 200ms

Current Behavior

Google Search Console (Core Web Vitals) reports a problem with INP: "INP issue: longer than 200ms (mobile)". The problem regards the action of pushing Accept button. You can confirm the problem with debugbear inp-debugger report. Correct me if I'm wrong, but I suspect that click action triggers some code that takes long to process (long in terms of INP metric).

consent

Duration breakdown: Input Delay | 28 ms Processing Time | 50 ms Presentation Delay | 130 ms

I might have found the bottleneck, however the script is minified and thous hardly readable. The click event takes a lot of time.

Recalc recalc2

There is as whole article on this topic at https://web.dev/articles/cookie-notice-best-practices

Steps to reproduce

Either look into your Google Search Console - Core Web vitals Report, or use debugbear inp-debugger report.

Proposed fix or additional info.

The proposed solution is to defer code execution. So visually code looks like it has been executed successfully while some work is still underway in the background.

The logic is as follows:

function saveSettings () {
  // Do critical work that is user-visible:
  validateForm();
  showSpinner();
  updateUI();

  // Defer work that isn't user-visible to a separate task:
  setTimeout(() => {
    saveToDatabase();
    sendAnalytics();
  }, 0);
}

Version

3.0.0

On which browser do you see the issue?

No response

bennetsadyba commented 1 month ago

One more thing - the function that replaces <script src= should be run with a delay after consent button click. This might be the function "manageScriptTags". This will improve INP furthermore