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

next/script creates unhandled promise rejection with empty error for every script that fails to load #27747

Closed
SkylarWolfLA opened this issue Aug 4, 2021 · 1 comment · Fixed by #27903
Labels
bug Issue was opened via the bug report template.

Comments

@SkylarWolfLA
Copy link

SkylarWolfLA commented Aug 4, 2021

What version of Next.js are you using?

11

What version of Node.js are you using?

12.14.1

What browser are you using?

Chrome

What operating system are you using?

macOS

How are you deploying your application?

next start

Describe the Bug

Loading any script which fails to load generates an unhandled rejection error "Uncaught (in promise) undefined"

This is due to this code

https://github.com/vercel/next.js/blob/canary/packages/next/client/script.tsx#L58

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

reject is called but loadPromise never has an error handler attached unless you create two tags for the same script which will do ScriptCache.get(src).then(onLoad, onError)

Expected Behavior

No unhandled rejection error for errors and pass the real error to the onError

To Reproduce

<Script
        src="https://somebrokenlink.doesntexist"
        strategy="lazyOnload"
        onLoad={() => {
          console.log('success');
        }}
        onLoad={(e) => {
          console.log('error');
          console.log(e); // this doesn't exist either
        }}
      />
@SkylarWolfLA SkylarWolfLA added the bug Issue was opened via the bug report template. label Aug 4, 2021
janicklas-ralph added a commit to janicklas-ralph/next.js that referenced this issue Aug 9, 2021
@kodiakhq kodiakhq bot closed this as completed in #27903 Aug 10, 2021
kodiakhq bot pushed a commit that referenced this issue Aug 10, 2021
## Bug

- [x] fixes #27747
- [x] Integration tests added
flybayer pushed a commit to blitz-js/next.js that referenced this issue Aug 19, 2021
## Bug

- [x] fixes vercel#27747
- [x] Integration tests added
ijjk added a commit that referenced this issue Aug 24, 2021
* Fix #27747

* Fix lint error

* Add data attribute to script component

* Fix #28036

* Fix tests

* Fix tests

Co-authored-by: JJ Kasper <jj@jjsweb.site>
@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants