Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn in dev mode when script tags are added with next/head #33968

Merged
merged 2 commits into from
Feb 4, 2022

Conversation

kara
Copy link
Collaborator

@kara kara commented Feb 4, 2022

This commit adds a development mode warning in the console
if you try to include <script> tags in next/head, e.g.

<Head>
  <script async src="..." />
</Head>

The warning message explains that this pattern will not
work well with Suspense/streaming and recommends using the
next/script component instead.

TODO in follow-up PR: add same warning for stylesheets, etc

Feature

  • Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
  • Integration tests added
  • Documentation added
  • Errors have helpful link attached, see contributing.md

@ijjk ijjk added area: documentation created-by: Chrome Aurora PRs by the Google Chrome team: https://web.dev/aurora type: next labels Feb 4, 2022
@ijjk
Copy link
Member

ijjk commented Feb 4, 2022

Failing test suites

Commit: ef9f1ca

test/integration/amphtml-fragment-style/test/index.test.js

  • AMP Fragment Styles > adds styles from fragment in AMP mode correctly
Expand output

● AMP Fragment Styles › adds styles from fragment in AMP mode correctly

expect(received).toBe(expected) // Object.is equality

Expected: "PASS"
Received: {"i": "FAIL"}

  16 |     }
  17 |   }
> 18 |   expect(result.status).toBe('PASS')
     |                         ^
  19 | }
  20 |

  at Object.validateAMP (lib/amp-test-utils.js:18:25)
  at Object.<anonymous> (integration/amphtml-fragment-style/test/index.test.js:35:5)

test/integration/amp-export-validation/test/index.test.js

  • AMP Validation on Export > should have shown errors during build
  • AMP Validation on Export > should export AMP pages
Expand output

● AMP Validation on Export › should have shown errors during build

expect(received).toMatch(expected)

Expected pattern: /error.*The mandatory attribute 'height' is missing in tag 'amp-video'\./
Received string:  "info  - Checking validity of types...
info  - Creating an optimized production build...
info  - Compiled successfully
info  - Collecting page data...
info  - Generating static pages (0/8)
info  - Generating static pages (2/8)
info  - Generating static pages (4/8)
info  - Generating static pages (6/8)
info  - Generating static pages (8/8)
Amp Validation·
/cat          warn  The native JavaScript AMPHTML Validator (validator.js) has been turned down. If you are seeing this error, update your tooling to instead load the API compatible WebAssembly AMPHTML Validator (validator_wasm.js) instead.·
/dog-cat      warn  The native JavaScript AMPHTML Validator (validator.js) has been turned down. If you are seeing this error, update your tooling to instead load the API compatible WebAssembly AMPHTML Validator (validator_wasm.js) instead.·
/dog          warn  The native JavaScript AMPHTML Validator (validator.js) has been turned down. If you are seeing this error, update your tooling to instead load the API compatible WebAssembly AMPHTML Validator (validator_wasm.js) instead.·
/first        warn  The native JavaScript AMPHTML Validator (validator.js) has been turned down. If you are seeing this error, update your tooling to instead load the API compatible WebAssembly AMPHTML Validator (validator_wasm.js) instead.·
/second       warn  The native JavaScript AMPHTML Validator (validator.js) has been turned down. If you are seeing this error, update your tooling to instead load the API compatible WebAssembly AMPHTML Validator (validator_wasm.js) instead.·
/third?amp=1  warn  The native JavaScript AMPHTML Validator (validator.js) has been turned down. If you are seeing this error, update your tooling to instead load the API compatible WebAssembly AMPHTML Validator (validator_wasm.js) instead.·
info  - Finalizing page optimization...·
Page                                       Size     First Load JS
┌ ○ /404                                   195 B          71.2 kB
├ ○ /cat (670 ms)                          AMP                AMP
├ ○ /dog                                   AMP                AMP
├ ○ /dog-cat                               AMP                AMP
├ ○ /first                                 AMP                AMP
├ ○ /second                                AMP                AMP
└ ○ /third                                 280 B          71.3 kB
+ First Load JS shared by all              71.1 kB
  ├ chunks/framework-158f450427c55907.js   41.9 kB
  ├ chunks/main-d137f23c48360464.js        27 kB
  ├ chunks/pages/_app-26477814f4b13881.js  1.37 kB
  └ chunks/webpack-fd82975a6094609f.js     727 B·
○  (Static)  automatically rendered as static HTML (uses no initial props)·
warn  - Detected next.config.js, no exported configuration found. https://nextjs.org/docs/messages/empty-configuration
warn  - The Next.js plugin was not detected in your ESLint configuration. See https://nextjs.org/docs/basic-features/eslint#migrating-existing-config
"

  24 |
  25 |   it('should have shown errors during build', async () => {
> 26 |     expect(buildOutput).toMatch(
     |                         ^
  27 |       /error.*The mandatory attribute 'height' is missing in tag 'amp-video'\./
  28 |     )
  29 |   })

  at Object.<anonymous> (integration/amp-export-validation/test/index.test.js:26:25)

● AMP Validation on Export › should export AMP pages

expect(received).toBe(expected) // Object.is equality

Expected: "PASS"
Received: {"i": "FAIL"}

  16 |     }
  17 |   }
> 18 |   expect(result.status).toBe('PASS')
     |                         ^
  19 | }
  20 |

  at Object.validateAMP (lib/amp-test-utils.js:18:25)
  at integration/amp-export-validation/test/index.test.js:36:9
      at async Promise.all (index 2)
  at Object.<anonymous> (integration/amp-export-validation/test/index.test.js:33:5)

test/integration/amphtml-custom-validator/test/index.test.js

  • AMP Custom Validator > should run in dev mode successfully
Expand output

● AMP Custom Validator › should run in dev mode successfully

expect(received).not.toContain(expected) // indexOf

Expected substring: not "error"
Received string:        "warn  - You have enabled experimental feature(s).
warn  - Experimental features are not covered by semver, and may cause unexpected or broken application behavior. Use them at your own risk.

warn  - Amp Validation

/  warn  The native JavaScript AMPHTML Validator (validator.js) has been turned down. If you are seeing this error, update your tooling to instead load the API compatible WebAssembly AMPHTML Validator (validator_wasm.js) instead.

"

  42 |     await killApp(app)
  43 |
> 44 |     expect(stderr).not.toContain('error')
     |                        ^
  45 |     expect(html).toContain('Hello from AMP')
  46 |   })
  47 | })

  at Object.<anonymous> (integration/amphtml-custom-validator/test/index.test.js:44:24)

@ijjk

This comment has been minimized.

@kara kara force-pushed the warning branch 2 times, most recently from 95bdcb9 to c22c78b Compare February 4, 2022 04:18
This commit adds a development mode warning in the console
if you try to include <script> tags in next/head, e.g.

```
<Head>
  <script async src="..." />
</Head>
```

The warning message explains that this pattern will not
work well with Suspense/streaming and recommends using the
next/script component instead.
@ijjk

This comment has been minimized.

@kara kara marked this pull request as ready for review February 4, 2022 06:07
@ijjk
Copy link
Member

ijjk commented Feb 4, 2022

Stats from current PR

Default Build
General Overall increase ⚠️
vercel/next.js canary kara/next.js warning Change
buildDuration 20.1s 20.2s ⚠️ +65ms
buildDurationCached 7.6s 7.7s ⚠️ +111ms
nodeModulesSize 359 MB 359 MB ⚠️ +511 B
Page Load Tests Overall increase ✓
vercel/next.js canary kara/next.js warning Change
/ failed reqs 0 0
/ total time (seconds) 3.732 3.772 ⚠️ +0.04
/ avg req/sec 669.93 662.84 ⚠️ -7.09
/error-in-render failed reqs 0 0
/error-in-render total time (seconds) 1.817 1.803 -0.01
/error-in-render avg req/sec 1375.53 1386.94 +11.41
Client Bundles (main, webpack, commons)
vercel/next.js canary kara/next.js warning Change
450.HASH.js gzip 179 B 179 B
framework-HASH.js gzip 42.2 kB 42.2 kB
main-HASH.js gzip 27.3 kB 27.3 kB
webpack-HASH.js gzip 1.44 kB 1.44 kB
Overall change 71.2 kB 71.2 kB
Legacy Client Bundles (polyfills)
vercel/next.js canary kara/next.js warning Change
polyfills-HASH.js gzip 31 kB 31 kB
Overall change 31 kB 31 kB
Client Pages
vercel/next.js canary kara/next.js warning Change
_app-HASH.js gzip 1.37 kB 1.37 kB
_error-HASH.js gzip 194 B 194 B
amp-HASH.js gzip 312 B 312 B
css-HASH.js gzip 326 B 326 B
dynamic-HASH.js gzip 2.37 kB 2.37 kB
head-HASH.js gzip 350 B 350 B
hooks-HASH.js gzip 919 B 919 B
image-HASH.js gzip 4.94 kB 4.94 kB
index-HASH.js gzip 263 B 263 B
link-HASH.js gzip 2.19 kB 2.19 kB
routerDirect..HASH.js gzip 321 B 321 B
script-HASH.js gzip 383 B 383 B
withRouter-HASH.js gzip 318 B 318 B
85e02e95b279..7e3.css gzip 107 B 107 B
Overall change 14.3 kB 14.3 kB
Client Build Manifests
vercel/next.js canary kara/next.js warning Change
_buildManifest.js gzip 459 B 459 B
Overall change 459 B 459 B
Rendered Page Sizes
vercel/next.js canary kara/next.js warning Change
index.html gzip 531 B 531 B
link.html gzip 544 B 544 B
withRouter.html gzip 527 B 527 B
Overall change 1.6 kB 1.6 kB

Diffs

Diff for main-HASH.js
@@ -3826,6 +3826,9 @@
                 return /*#__PURE__*/ _react.default.cloneElement(c, newProps);
               }
             }
+            // TODO(kara): warn for stylesheets as well as scripts
+            if (false) {
+            }
             return /*#__PURE__*/ _react.default.cloneElement(c, {
               key: key
             });
Diff for index.html
@@ -19,7 +19,7 @@
       defer=""
     ></script>
     <script
-      src="/_next/static/chunks/main-3bcc9011ce5cf468.js"
+      src="/_next/static/chunks/main-04b00b1eea9d690e.js"
       defer=""
     ></script>
     <script
Diff for link.html
@@ -19,7 +19,7 @@
       defer=""
     ></script>
     <script
-      src="/_next/static/chunks/main-3bcc9011ce5cf468.js"
+      src="/_next/static/chunks/main-04b00b1eea9d690e.js"
       defer=""
     ></script>
     <script
Diff for withRouter.html
@@ -19,7 +19,7 @@
       defer=""
     ></script>
     <script
-      src="/_next/static/chunks/main-3bcc9011ce5cf468.js"
+      src="/_next/static/chunks/main-04b00b1eea9d690e.js"
       defer=""
     ></script>
     <script

Default Build with SWC (Decrease detected ✓)
General Overall increase ⚠️
vercel/next.js canary kara/next.js warning Change
buildDuration 24.4s 24.1s -305ms
buildDurationCached 7.7s 7.7s ⚠️ +33ms
nodeModulesSize 359 MB 359 MB ⚠️ +511 B
Page Load Tests Overall decrease ⚠️
vercel/next.js canary kara/next.js warning Change
/ failed reqs 0 0
/ total time (seconds) 3.751 3.709 -0.04
/ avg req/sec 666.51 674.03 +7.52
/error-in-render failed reqs 0 0
/error-in-render total time (seconds) 1.799 1.846 ⚠️ +0.05
/error-in-render avg req/sec 1389.32 1354.57 ⚠️ -34.75
Client Bundles (main, webpack, commons)
vercel/next.js canary kara/next.js warning Change
450.HASH.js gzip 179 B 179 B
framework-HASH.js gzip 42.3 kB 42.3 kB
main-HASH.js gzip 27.4 kB 27.4 kB
webpack-HASH.js gzip 1.44 kB 1.44 kB
Overall change 71.3 kB 71.3 kB
Legacy Client Bundles (polyfills)
vercel/next.js canary kara/next.js warning Change
polyfills-HASH.js gzip 31 kB 31 kB
Overall change 31 kB 31 kB
Client Pages
vercel/next.js canary kara/next.js warning Change
_app-HASH.js gzip 1.35 kB 1.35 kB
_error-HASH.js gzip 180 B 180 B
amp-HASH.js gzip 305 B 305 B
css-HASH.js gzip 321 B 321 B
dynamic-HASH.js gzip 2.36 kB 2.36 kB
head-HASH.js gzip 342 B 342 B
hooks-HASH.js gzip 911 B 911 B
image-HASH.js gzip 4.97 kB 4.97 kB
index-HASH.js gzip 256 B 256 B
link-HASH.js gzip 2.21 kB 2.21 kB
routerDirect..HASH.js gzip 314 B 314 B
script-HASH.js gzip 375 B 375 B
withRouter-HASH.js gzip 309 B 309 B
85e02e95b279..7e3.css gzip 107 B 107 B
Overall change 14.3 kB 14.3 kB
Client Build Manifests
vercel/next.js canary kara/next.js warning Change
_buildManifest.js gzip 459 B 459 B
Overall change 459 B 459 B
Rendered Page Sizes
vercel/next.js canary kara/next.js warning Change
index.html gzip 533 B 533 B
link.html gzip 546 B 546 B
withRouter.html gzip 527 B 527 B
Overall change 1.61 kB 1.61 kB

Diffs

Diff for main-HASH.js
@@ -3826,6 +3826,9 @@
                 return /*#__PURE__*/ _react.default.cloneElement(c, newProps);
               }
             }
+            // TODO(kara): warn for stylesheets as well as scripts
+            if (false) {
+            }
             return /*#__PURE__*/ _react.default.cloneElement(c, {
               key: key
             });
Diff for index.html
@@ -19,7 +19,7 @@
       defer=""
     ></script>
     <script
-      src="/_next/static/chunks/main-3bcc9011ce5cf468.js"
+      src="/_next/static/chunks/main-04b00b1eea9d690e.js"
       defer=""
     ></script>
     <script
Diff for link.html
@@ -19,7 +19,7 @@
       defer=""
     ></script>
     <script
-      src="/_next/static/chunks/main-3bcc9011ce5cf468.js"
+      src="/_next/static/chunks/main-04b00b1eea9d690e.js"
       defer=""
     ></script>
     <script
Diff for withRouter.html
@@ -19,7 +19,7 @@
       defer=""
     ></script>
     <script
-      src="/_next/static/chunks/main-3bcc9011ce5cf468.js"
+      src="/_next/static/chunks/main-04b00b1eea9d690e.js"
       defer=""
     ></script>
     <script
Commit: 62d3aff

@kodiakhq kodiakhq bot merged commit 61ea8ef into vercel:canary Feb 4, 2022
@jakejarvis
Copy link
Contributor

Hey @kara and everyone, not a huge deal but it looks like I'm getting a ton of these warnings in canary 6 — not because I'm adding any script tags myself, but because I'm using next-seo's JSON-LD components (which I suppose technically does indirectly add script tags, but with type="application/ld+json" set... what a mess!)

Are these structured data "scripts" intended to be caught by this check? Happy to whip up a quick repo to reproduce this if it would be helpful. 😊

Screen Shot 2022-02-05 at 1 59 37 PM

kara added a commit to kara/next.js that referenced this pull request Feb 5, 2022
In vercel#33968, a warning was added for script tags inserted through the
next/head component. This change unintentionally included
application/ld+json scripts, which shouldn't be triggering the
warnings (as they were originally intended to catch scripts where
loading order or timing could be important). This change adds an
exception for application/ld+json scripts, so they do not log the
warning if they are included through next/head.
@kara
Copy link
Collaborator Author

kara commented Feb 5, 2022

Hmm, no, those types of script tags shouldn't be caught by the warning -- thanks for pointing this out! Just created a PR here to fix the issue.

@jakejarvis
Copy link
Contributor

You are quick! Thanks so much!! 🚀

ijjk pushed a commit that referenced this pull request Feb 5, 2022
…34021)

In #33968, a warning was added for script tags inserted through the
next/head component. This change unintentionally included
application/ld+json scripts, which shouldn't be triggering the
warnings (as they were originally intended to catch scripts where
loading order or timing could be important). This change adds an
exception for application/ld+json scripts, so they do not log the
warning if they are included through next/head.
natew pushed a commit to natew/next.js that referenced this pull request Feb 16, 2022
)

This commit adds a development mode warning in the console
if you try to include <script> tags in next/head, e.g.

```
<Head>
  <script async src="..." />
</Head>
```

The warning message explains that this pattern will not
work well with Suspense/streaming and recommends using the
next/script component instead.

TODO in follow-up PR: add same warning for stylesheets, etc

## Feature

- [x] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [x] Integration tests added
- [x] Documentation added
- [x] Errors have helpful link attached, see `contributing.md`
natew pushed a commit to natew/next.js that referenced this pull request Feb 16, 2022
…ercel#34021)

In vercel#33968, a warning was added for script tags inserted through the
next/head component. This change unintentionally included
application/ld+json scripts, which shouldn't be triggering the
warnings (as they were originally intended to catch scripts where
loading order or timing could be important). This change adds an
exception for application/ld+json scripts, so they do not log the
warning if they are included through next/head.
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 8, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
created-by: Chrome Aurora PRs by the Google Chrome team: https://web.dev/aurora type: next
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants