Skip to content

iFrame will not call the onLoad handler in dev mode #69736

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

Open
robcaldecott opened this issue Sep 5, 2024 · 1 comment
Open

iFrame will not call the onLoad handler in dev mode #69736

robcaldecott opened this issue Sep 5, 2024 · 1 comment
Labels
bug Issue was opened via the bug report template.

Comments

@robcaldecott
Copy link

Link to the code that reproduces this issue

https://github.com/robcaldecott/iframe-nextjs

To Reproduce

Clone the repo and first build the app that will be hosted in an iframe (this part is build using Vite but that does not matter).

cd remote
npm install
npm run dev

This will start a little app on http://localhost:5173. Leave this running.

Now, in a separate shell, run the NextJS app that exhibits the issue:

cd host
npm install
npm run dev

Open http://localhost:3000 in your browser, and open the DevTools so you can see the console.
Refresh the page and note that there are no console messages and the three iframe components on the page do not get the onLoad handler called.

image

However, if you make changes to the source and the app is reloaded by HMR then sometimes the onLoad event will work. But this is intermittent.

Next, shut down the NextJS host, build it for production and start it up:

cd host
npm run build
npm start

Go to http://localhost:3000 and refresh the page - you will see that onLoad is now working for all three iframe components correctly.

image

image

Current vs. Expected behavior

When running the NextJS host app in dev mode I would expect the onLoad handler on the iframe elements to work. However, it never works in dev mode when refreshing the page.

In production mode everything works correctly and the three onLoad events work as expected.

Included in the repo is a host app written using Vite and the latest React 18 build. This app does not suffer from this issue and onLoad works in dev and prodcuction.

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.6.0: Mon Jul 29 21:13:04 PDT 2024; root:xnu-10063.141.2~1/RELEASE_ARM64_T6020
  Available memory (MB): 16384
  Available CPU cores: 12
Binaries:
  Node: 20.16.0
  npm: 10.8.1
  Yarn: N/A
  pnpm: N/A
Relevant Packages:
  next: 14.2.8 // Latest available version is detected (14.2.8).
  eslint-config-next: 14.2.8
  react: 18.3.1
  react-dom: 18.3.1
  typescript: 5.5.4
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Not sure

Which stage(s) are affected? (Select all that apply)

next dev (local)

Additional context

I have tried the following to get the onLoad event to fire in dev mode:

  • Make everything a client component with use client.
  • Disabling React Strict mode in the NextJS config.
  • Throttling the network.
  • Throttling the CPU.
  • Using addEventListener on the iframe ref to capture the load event.
  • Replacing the iframe content with a public website URL.
  • Using Safari instead of Chrome.

None of these make any difference: onLoad will not work when refreshing the page in dev mode. It will only fire occasionally when editing the code and causing the module to reload.

While the app works fine in production, I am relying on onLoad working in dev mode as the host then communicates with each iframe using postMessage (this code has been removed to simplify the example.) I can hack around this using a useEffect with a setTimeout to call setLoaded but this is not ideal.

@robcaldecott robcaldecott added the bug Issue was opened via the bug report template. label Sep 5, 2024
@smith558

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue was opened via the bug report template.
Projects
None yet
Development

No branches or pull requests

2 participants