Repository: next.js
Issue: next/script onReady does not fire when rendering multiple instances with the same src
Labels: bug,Script (next/script)

### Link to the code that reproduces this issue

https://github.com/carlyle/next-script-onready-reproduction-app

### To Reproduce

You can see it on a built and deployed app:

1. Go to [https://next-script-onready-reproduction-app.vercel.app/single](https://next-script-onready-reproduction-app.vercel.app/single). Verify that `Embed #1` renders without error and with ✅s by "Loaded?" and "Ready?", which is expected
2. Go to [https://next-script-onready-reproduction-app.vercel.app/multiple](https://next-script-onready-reproduction-app.vercel.app/multiple). Embed 1 renders without error and with ✅s by "Loaded?" and "Ready?" but the following embeds do not render as expected

As well as in local development:

1. Start the application (`next dev`)
4. Go to [http://localhost:3000/single](http://localhost:3000/single). Verify that Embed 1 renders without error and with ✅s by "Loaded?" and "Ready?", which is expected
5. Go to [http://localhost:3000/multiple](http://localhost:3000/multiple). Embed 1 renders without error and with ✅s by "Loaded?" and "Ready?" but the following embeds do not render as expected

### Current vs. Expected behavior

#### Current Behavior

The first instance of `<Script>` with a particular `src`:
* fires the `onLoad` callback as expected
* fires the `onReady` callback as expected

The second instance of `<Script>` with the same `src`:
* sometimes fires the `onLoad` callback, which is unexpected because it is intended for ["run[ning] JavaScript code once after the script has finished loading"](https://nextjs.org/docs/app/api-reference/components/script#onload )
* does not fire the `onReady` callback, which is unexpected because it is intended for ["run[ning] JavaScript code after the script has finished loading and every time the component is mounted"](https://nextjs.org/docs/app/api-reference/components/script#onready)

The third and later instances of `<Script>` with the same `src`:
* do not fire the `onLoad` callback, which is expected
* do not fire the `onReady` callback, which is unexpected because it is intended for ["run[ning] JavaScript code after the script has finished loading and every time the component is mounted"](https://nextjs.org/docs/app/api-reference/components/script#onready)

#### Expected Behavior

The first instance of `<Script>` with a particular `src`:
* fires the `onLoad` callback
* fires the `onReady` callback

The second and later instances of `<Script>` with the same `src`:
* do not fire the `onLoad` callback
* fire the `onReady` callback

### Provide environment information

```bash
Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.3.0: Wed Dec 20 21:31:00 PST 2023; root:xnu-10002.81.5~7/RELEASE_ARM64_T6020
  Available memory (MB): 32768
  Available CPU cores: 12
Binaries:
  Node: 20.11.0
  npm: 10.2.4
  Yarn: N/A
  pnpm: N/A
Relevant Packages:
  next: 14.2.0-canary.22 // Latest available version is detected (14.2.0-canary.22).
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.1.3
Next.js Config:
  output: N/A
```


### Which area(s) are affected? (Select all that apply)

App Router, Script optimization (next/script)

### Which stage(s) are affected? (Select all that apply)

next dev (local), next start (local), Vercel (Deployed), Other (Deployed)

### Additional context

The motivation behind having a `<Script>` render within each instance of `<Embed>`, as opposed to having a single `<Script>` somewhere else in the app, is:
- to keep all of the embed-related logic in one place
- to prevent the remote script from loading if we don't end up rendering an embed

An example use case would be embedding videos in news articles: we want to load the video player library whenever an article has any video embeds, but wouldn't want to load that library unnecessarily on articles without video embeds