Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
43393d53
Unverified
Commit
43393d53
authored
Aug 10, 2021
by
Janicklas Ralph
Committed by
GitHub
Aug 10, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix `next/script` unhandled promise rejection (#27903)
## Bug - [x] fixes #27747 - [x] Integration tests added
parent
eb871d30
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
24 additions
and
9 deletions
+24
-9
script.tsx
packages/next/client/script.tsx
+10
-9
page3.js
test/integration/script-loader/pages/page3.js
+8
-0
index.test.js
test/integration/script-loader/test/index.test.js
+6
-0
No files found.
packages/next/client/script.tsx
View file @
43393d53
...
@@ -10,8 +10,8 @@ const LoadCache = new Set()
...
@@ -10,8 +10,8 @@ const LoadCache = new Set()
export
interface
ScriptProps
extends
ScriptHTMLAttributes
<
HTMLScriptElement
>
{
export
interface
ScriptProps
extends
ScriptHTMLAttributes
<
HTMLScriptElement
>
{
strategy
?:
'afterInteractive'
|
'lazyOnload'
|
'beforeInteractive'
strategy
?:
'afterInteractive'
|
'lazyOnload'
|
'beforeInteractive'
id
?:
string
id
?:
string
onLoad
?:
()
=>
void
onLoad
?:
(
e
:
any
)
=>
void
onError
?:
()
=>
void
onError
?:
(
e
:
any
)
=>
void
children
?:
React
.
ReactNode
children
?:
React
.
ReactNode
}
}
...
@@ -56,18 +56,19 @@ const loadScript = (props: ScriptProps): void => {
...
@@ -56,18 +56,19 @@ const loadScript = (props: ScriptProps): void => {
const
el
=
document
.
createElement
(
'script'
)
const
el
=
document
.
createElement
(
'script'
)
const
loadPromise
=
new
Promise
<
void
>
((
resolve
,
reject
)
=>
{
const
loadPromise
=
new
Promise
<
void
>
((
resolve
,
reject
)
=>
{
el
.
addEventListener
(
'load'
,
function
()
{
el
.
addEventListener
(
'load'
,
function
(
e
)
{
resolve
()
resolve
()
if
(
onLoad
)
{
if
(
onLoad
)
{
onLoad
.
call
(
this
)
onLoad
.
call
(
this
,
e
)
}
}
})
})
el
.
addEventListener
(
'error'
,
function
()
{
el
.
addEventListener
(
'error'
,
function
(
e
)
{
reject
()
reject
(
e
)
if
(
onError
)
{
onError
()
}
})
})
}).
catch
(
function
(
e
)
{
if
(
onError
)
{
onError
(
e
)
}
})
})
if
(
src
)
{
if
(
src
)
{
...
...
test/integration/script-loader/pages/page3.js
View file @
43393d53
...
@@ -15,6 +15,14 @@ const Page = () => {
...
@@ -15,6 +15,14 @@ const Page = () => {
src
=
"https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js?a=scriptLazyOnload"
src
=
"https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js?a=scriptLazyOnload"
strategy
=
"lazyOnload"
strategy
=
"lazyOnload"
><
/Script
>
><
/Script
>
<
Script
src
=
"https://example.com/doesntexist"
strategy
=
"lazyOnload"
onError
=
{(
e
)
=>
{
console
.
log
(
'error'
)
console
.
log
(
e
)
}}
/
>
<
div
>
page3
<
/div
>
<
div
>
page3
<
/div
>
<
/div
>
<
/div
>
)
)
...
...
test/integration/script-loader/test/index.test.js
View file @
43393d53
...
@@ -69,6 +69,12 @@ describe('Script Loader', () => {
...
@@ -69,6 +69,12 @@ describe('Script Loader', () => {
await
browser
.
waitForElementByCss
(
'#onload-div'
)
await
browser
.
waitForElementByCss
(
'#onload-div'
)
await
waitFor
(
1000
)
await
waitFor
(
1000
)
const
logs
=
await
browser
.
log
(
'browser'
)
const
filteredLogs
=
logs
.
filter
(
(
log
)
=>
!
log
.
message
.
includes
(
'Failed to load resource'
)
)
expect
(
filteredLogs
.
length
).
toBe
(
0
)
async
function
test
(
id
)
{
async
function
test
(
id
)
{
const
script
=
await
browser
.
elementById
(
id
)
const
script
=
await
browser
.
elementById
(
id
)
const
endScripts
=
await
browser
.
elementsByCss
(
const
endScripts
=
await
browser
.
elementsByCss
(
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment