Skip to content

Commit

Permalink
updates per review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
housseindjirdeh committed Apr 26, 2022
1 parent fbc3d07 commit 3f29ac0
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 18 deletions.
38 changes: 38 additions & 0 deletions errors/invalid-script.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Invalid Script

#### Why This Error Occurred

Somewhere in your application, you are using the `next/script` component without including an inline script or `src` attribute.

#### Possible Ways to Fix It

Look for any usage of the `next/script` component and make sure that `src` is provided or an inline script is used.

**Compatible `src` attribute**

```jsx
<Script src="https://example.com/analytics.js" />
```

**Compatible inline script with curly braces**

```jsx
<Script id="show-banner">
{`document.getElementById('banner').classList.remove('hidden')`}
</Script>
```

**Compatible inline script with `dangerouslySetInnerHtml`**

```jsx
<Script
id="show-banner"
dangerouslySetInnerHTML={{
__html: `document.getElementById('banner').classList.remove('hidden')`,
}}
/>
```

### Useful Links

- [Script Component in Documentation](https://nextjs.org/docs/basic-features/script)
4 changes: 4 additions & 0 deletions errors/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,10 @@
{
"title": "no-assign-module-variable",
"path": "/errors/no-assign-module-variable.md"
},
{
"title": "invalid-script",
"path": "/errors/invalid-script.md"
}
]
}
Expand Down
39 changes: 21 additions & 18 deletions packages/next/pages/_document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,23 +139,28 @@ function getPreNextWorkerScripts(context: HtmlProps, props: OriginProps) {
if (src) {
// Use external src if provided
srcProps.src = src
} else {
} else if (
dangerouslySetInnerHTML &&
dangerouslySetInnerHTML.__html
) {
// Embed inline script if provided with dangerouslySetInnerHTML
if (dangerouslySetInnerHTML && dangerouslySetInnerHTML.__html) {
srcProps.dangerouslySetInnerHTML = {
__html: dangerouslySetInnerHTML.__html,
}
} else if (scriptChildren) {
// Embed inline script if provided with children
srcProps.dangerouslySetInnerHTML = {
__html:
typeof scriptChildren === 'string'
? scriptChildren
: Array.isArray(scriptChildren)
? scriptChildren.join('')
: '',
}
srcProps.dangerouslySetInnerHTML = {
__html: dangerouslySetInnerHTML.__html,
}
} else if (scriptChildren) {
// Embed inline script if provided with children
srcProps.dangerouslySetInnerHTML = {
__html:
typeof scriptChildren === 'string'
? scriptChildren
: Array.isArray(scriptChildren)
? scriptChildren.join('')
: '',
}
} else {
throw new Error(
'Invalid usage of next/script. Did you forget to include a src attribute or an inline script? https://nextjs.org/docs/messages/invalid-script'
)
}

return (
Expand All @@ -174,9 +179,7 @@ function getPreNextWorkerScripts(context: HtmlProps, props: OriginProps) {
)
} catch (err) {
if (isError(err) && err.code !== 'MODULE_NOT_FOUND') {
console.warn(
`Warning: Partytown could not be instantiated in your application due to an error. ${err.message}`
)
console.warn(`Warning: ${err.message}`)
}
return null
}
Expand Down

0 comments on commit 3f29ac0

Please sign in to comment.