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

Fix next/script unhandled promise rejection #27903

Merged
merged 3 commits into from
Aug 10, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions packages/next/client/script.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,19 @@ const loadScript = (props: ScriptProps): void => {
const el = document.createElement('script')

const loadPromise = new Promise<void>((resolve, reject) => {
el.addEventListener('load', function () {
el.addEventListener('load', function (e) {
resolve()
if (onLoad) {
onLoad.call(this)
onLoad.call(this, e)
}
})
el.addEventListener('error', function () {
reject()
if (onError) {
onError()
}
el.addEventListener('error', function (e) {
reject(e)
})
}).catch(function (e) {
if (onError) {
onError(e)
}
})

if (src) {
Expand Down
8 changes: 8 additions & 0 deletions test/integration/script-loader/pages/page3.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ const Page = () => {
src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js?a=scriptLazyOnload"
strategy="lazyOnload"
></Script>
<Script
src="https://somebrokenlink.doesntexist"
ijjk marked this conversation as resolved.
Show resolved Hide resolved
strategy="lazyOnload"
onError={(e) => {
console.log('error');
console.log(e);
}}
/>
<div>page3</div>
</div>
)
Expand Down
4 changes: 4 additions & 0 deletions test/integration/script-loader/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ describe('Script Loader', () => {
await browser.waitForElementByCss('#onload-div')
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) {
const script = await browser.elementById(id)
const endScripts = await browser.elementsByCss(
Expand Down