question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

next/script using beforeInteractive strategy don't work with alert

See original GitHub issue

What version of Next.js are you using?

11.0.0

What version of Node.js are you using?

14.17.0

What browser are you using?

Chrome

What operating system are you using?

macOS

How are you deploying your application?

next start

Describe the Bug

When I try to add next/script into “_document” or “_app” files calling a inline alert function, don’t work using beforeInteractive strategy, but when I don’t explicitly inform a strategy value, it works.

image

Expected Behavior

Should work with beforeInteractive strategy inside _document.js or _app.js.

To Reproduce

Just add <Script strategy="beforeInteractive">{`alert('Not work')`}</Script> to _document.js or _app.js, and try to run with next build && next start.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:4
  • Comments:29 (15 by maintainers)

github_iconTop GitHub Comments

15reactions
xd-hearstcommented, May 21, 2022

seems the narrative has changed regarding this in the doc: https://nextjs.org/docs/basic-features/script#beforeinteractive

It is stated that:

This strategy only works inside _document.js

However, if you try with their example, it does not work at all:

// In _document.js
import { Html, Head, Main, NextScript } from 'next/document'
import Script from 'next/script'

export default function Document() {
  return (
    <Html>
      <Head />
      <body>
        <Main />
        <NextScript />
        <Script
          src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js"
          strategy="beforeInteractive"
        ></Script>
      </body>
    </Html>
  )
}

Put it in the app does work.

I am curious why

8reactions
prichodkocommented, Aug 19, 2021

Another use-case is a code that detects user selected light / dark theme, to avoid flickering on the first load.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Basic Features: Handling Scripts - Next.js
Optimize your third-party scripts with the built-in `next/script` component. ... Note: The worker strategy is not yet stable and does not yet work...
Read more >
Why does the next.js Script tag with the beforeInteractive ...
js Script tag with the strategy beforeInteractive works. For testing i just used lodash. But i keep getting a ReferenceError: _ is not...
Read more >
Script Loading, performance and Next Script
Using Next Script is easy, deciding which strategy to use is hard. Harder than just sticking the script somewhere anyway. beforeInteractive.
Read more >
Best practices to increase the speed for Next.js apps
By setting the value of the strategy prop in the next/script component, you can use three different script loading approaches: afterInteractive ...
Read more >
NextJS Core Web Vitals - fix third party scripts
The Next.js Script component, next/script , is an extension of the HTML <script> ... The beforeInteractive strategy should only be used on scripts...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found