Skip to content

Commit

Permalink
Fixes beforeInteractive inline scripts don't run (#37033)
Browse files Browse the repository at this point in the history
BeforeInteractive inline script in v12.1.7-canary.8  don't run. Beacause the script has unknow src.

![image](https://user-images.githubusercontent.com/17813559/169257330-4419228a-6d10-4815-9451-d9a5dd7f011b.png)

Fixes #31275


## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`
  • Loading branch information
Chastrlove committed May 19, 2022
1 parent 50833d0 commit 5b21f09
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
9 changes: 7 additions & 2 deletions packages/next/pages/_document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -555,8 +555,13 @@ export class Head extends Component<
!script.src && (script.dangerouslySetInnerHTML || script.children)
)
.map((file: ScriptProps, index: number) => {
const { strategy, children, dangerouslySetInnerHTML, ...scriptProps } =
file
const {
strategy,
children,
dangerouslySetInnerHTML,
src,
...scriptProps
} = file
let html = ''

if (dangerouslySetInnerHTML && dangerouslySetInnerHTML.__html) {
Expand Down
15 changes: 15 additions & 0 deletions test/integration/script-loader/base/pages/page7.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Script from 'next/script'

const Page = () => {
return (
<div class="container">
<Script
id="beforeInteractiveInlineScript"
strategy="beforeInteractive"
>{`console.log('beforeInteractive inline script run')`}</Script>
<div>page7</div>
</div>
)
}

export default Page
18 changes: 18 additions & 0 deletions test/integration/script-loader/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,24 @@ describe('Next.js Script - Primary Strategies', () => {
).toBeGreaterThan(0)
})

it('priority beforeInteractive with inline script should execute', async () => {
let browser
try {
browser = await webdriver(appPort, '/page7')

await waitFor(1000)

const logs = await browser.log()
expect(
logs.some((log) =>
log.message.includes('beforeInteractive inline script run')
)
).toBe(true)
} finally {
if (browser) await browser.close()
}
})

it('Does not duplicate inline scripts', async () => {
let browser
try {
Expand Down

0 comments on commit 5b21f09

Please sign in to comment.