vercel / next.js

The React Framework
https://nextjs.org
MIT License
124.96k stars 26.69k forks source link

next/script afterInteractive is not defering according to lighthouse #43561

Open Pixelatex opened 1 year ago

Pixelatex commented 1 year ago

Verify canary release

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 21.5.0: Tue Apr 26 21:08:37 PDT 2022; root:xnu-8020.121.3~4/RELEASE_ARM64_T6000
Binaries:
  Node: 17.9.1
  npm: 8.11.0
  Yarn: 1.22.19
  pnpm: N/A
Relevant packages:
  next: 13.0.6-canary.2
  eslint-config-next: 13.0.5
  react: 18.2.0
  react-dom: 18.2.0

Which area of Next.js is affected? (leave empty if unsure)

Script optimizatzion (next/script)

Link to reproduction - Issues with a link to complete (but minimal) reproduction code will be addressed faster

https://codesandbox.io/s/nextjs-script-defer-issue-mh6lnq

To Reproduce

Add google tag scripts to your _app as stated in the next documentation.

<Script
  src={`https://www.googletagmanager.com/gtag/js?id=${trackingCode}`}
  strategy="afterInteractive"
/>
<Script id="gtag-init" strategy="afterInteractive">
  {`
  window.dataLayer = window.dataLayer || [];
  function gtag(){window.dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', '${trackingCode}', {
    page_path: window.location.pathname,
  });
`}
</Script>

Describe the Bug

The gtag scripts work correctly... however, when running lighthouse tests, you will see that the gtm scripts are still being loaded and block the main thread.

Expected Behavior

The "afterInteractive" should defer scripts until after initial JS load so the main thread is not blocked.

Which browser are you using? (if relevant)

chrome Version 107.0.5304.110 (Official Build) (arm64)

How are you deploying your application? (if relevant)

GCP

github-actions[bot] commented 1 year ago

We cannot recreate the issue with the provided information. Please add a reproduction in order for us to be able to investigate.

Why was this issue marked with the please add a complete reproduction label?

To be able to investigate, we need access to a reproduction to identify what triggered the issue. We prefer a link to a public GitHub repository (template), but you can also use a tool like CodeSandbox or StackBlitz.

To make sure the issue is resolved as quickly as possible, please make sure that the reproduction is as minimal as possible. This means that you should remove unnecessary code, files, and dependencies that do not contribute to the issue.

Please test your reproduction against the latest version of Next.js (next@canary) to make sure your issue has not already been fixed.

I added a link, why was it still marked?

Ensure the link is pointing to a codebase that is accessible (e.g. not a private repository). "example.com", "n/a", "will add later", etc. are not acceptable links -- we need to see a public codebase. See the above section for accepted links.

What happens if I don't provide a sufficient minimal reproduction?

Issues with the please add a complete reproduction label that receives no meaningful activity (e.g. new comments with a reproduction link) are automatically closed and locked after 30 days.

If your issue has not been resolved in that time and it has been closed/locked, please open a new issue with the required reproduction.

I did not open this issue, but it is relevant to me, what can I do to help?

Anyone experiencing the same issue is welcome to provide a minimal reproduction following the above steps. Furthermore, you can upvote the issue using the :+1: reaction on the topmost comment (please do not comment "I have the same issue" without repro steps). Then, we can sort issues by votes to prioritize.

I think my reproduction is good enough, why aren't you looking into it quicker?

We look into every Next.js issue and constantly monitor open issues for new comments.

However, sometimes we might miss one or two due to the popularity/high traffic of the repository. We apologize, and kindly ask you to refrain from tagging core maintainers, as that will usually not result in increased priority.

Upvoting issues to show your interest will help us prioritize and address them as quickly as possible. That said, every issue is important to us, and if an issue gets closed by accident, we encourage you to open a new one linking to the old issue and we will look into it.

Useful Resources

Pixelatex commented 1 year ago

I have created a sandbox with a minimal reproduction. If you run lighthouse on this sandbox example, you will see the gtm scripts are still blocking the main thread.

https://codesandbox.io/s/nextjs-script-defer-issue-mh6lnq

iljamulders commented 1 year ago

@Pixelatex I'm not seeing it. In my own project, and in your codesandbox. Only the codesandbox js is blocking Screenshot 2022-11-30 at 16 30 16

Tip: use onLoad instead of 2 separate scripts

<Script
  src={`https://www.googletagmanager.com/gtag/js?id=${trackingCode}`}
  strategy="afterInteractive"
  onLoad={() => {
    window.dataLayer = window.dataLayer || [];
    function gtag(){window.dataLayer.push(arguments);}
    gtag('js', new Date());

    gtag('config', '${trackingCode}', {
      page_path: window.location.pathname,
    });
  }}
/>
Develliot commented 1 month ago

@Pixelatex does the building GTM loader fix this issue for you?

import { GoogleTagManager } from "@next/third-parties/google";

mezoistvan commented 3 weeks ago

I have the same issue using import { GoogleTagManager } from "@next/third-parties/google";. The GTM script is the first thing being loaded on the page. It's blocking FCP and making Web Vitals much worse.