Skip to content

Bug: <iframe /> onLoad is not getting triggered after the reload triggered by moving the DOM Node #18752

Open

Description

@adamziel

When an <iframe /> DOM Node is moved to a different place in the tree, Chrome triggers a reload. That reload does not notify event handlers registered via onLoad=. In other words:

  • If there is an event handler registered via onLoad={myCallback}, it will not be fired.
  • If there is an event handler registered via iframe.addEventListener("load", myCallback, false), it will be fired.

React version: 16.13.1

Steps To Reproduce

Open a code example below and move the iframe down using arrow button next to it.

https://codesandbox.io/s/serverless-surf-k1ypg?file=/src/App.js

The current behavior

The iframe does not say "Initialized" anymore.

The expected behavior

The iframe should say "Initialized".

Other notes

If you move setIframeContent(e.currentTarget); to nativeLoad and repeat the steps above, it works as expected. The bug does not occur in Firefox.

Activity

  1. changed the title [-]Bug: <iframe /> onLoad is not getting triggered after moving the DOM Node in Chrome[/-] [+]Bug: <iframe /> onLoad is not getting triggered after the reload triggered by moving the DOM Node[/+] on Apr 27, 2020
  2. eps1lon commented on Apr 27, 2020

    @eps1lon
    Collaborator

    The iframe should say "Initialized".

    Can only reproduce this in the actual codesandbox browser preview in firefox 75. It's working fine on the standalone page (https://k1ypg.csb.app/) or in chrome 81 regardless of the view.

  3. adamziel commented on Apr 27, 2020

    @adamziel
    Author

    The firefox part is interesting - I wasn't able to reproduce it there. I just tried on the standalone page and managed to reproduce it with Chrome though:

    2020-04-27 14-05-12 2020-04-27 14_05_41

    It only happens with the down arrow since it makes React move the actual DOM Node. Up arrow will move the previous node.

    Also see the following Gutenberg issue that many people were able to reproduce and the underlying fix:

  4. eps1lon commented on Apr 27, 2020

    @eps1lon
    Collaborator

    Looks like macOS?

    Here are my results on Ubuntu 18.04:

    Chrome Version 81.0.4044.122 (Official Build) (64-bit):
    iframe-google-preview
    iframe-google-page

    Firefox 75.0 (64-bit):
    iframe-ff-preview
    iframe-ff-page

    I had some troubles with iframes recently myself but none of these had to do with nested iframes (which applies to the preview window of codesandbox). Generally I

    • don't use document.write but ReactDOM.createPortal
    • don't portal before the load event or document.readyState === "complete" (which you need to handle in a passive effect if you're hydrating)
      I found that calling document.write before the load event would be dropped in firefox.
  5. adamziel commented on Apr 27, 2020

    @adamziel
    Author

    Looks like macOS?

    Yes it is!

    Here are my results on Ubuntu 18.04:

    Nice! So interesting how we're seeing all these OS/Browser inconsistencies here - I did not expect that. I wonder if these iframes are being reloaded in somehow different way/order on different OSes? Also, let me just link to iframe reload issues in case there is something relevant in comments:

    don't use document.write but ReactDOM.createPortal
    don't portal before the load event or document.readyState === "complete" (which you need to handle in a passive effect if you're hydrating)

    Portals sounds like a good idea for an even better Gutenberg fix. I just played with them and I have some additional findings:

    Setting <iframe srcDoc="<!DOCTYPE html>" fixes the issue for me:

    https://codesandbox.io/s/dazzling-tdd-50r9o?file=/src/App.js

    useRef only works consistently when srcDoc is set

    Otherwise ref.current stays null. If you set ref to a custom callback, it's getting called with a correct DOM node: https://codesandbox.io/s/young-water-km9v5?file=/src/App.js

    This seems like some kind of race condition though as I'm getting inconsistent results:

    Zrzut ekranu 2020-04-27 o 15 04 00

    Zrzut ekranu 2020-04-27 o 15 04 40

    I found that calling document.write before the load event would be dropped in firefox.

    TIL, thank you!

  6. nearestnabors commented on Jul 21, 2020

    @nearestnabors

    MacOS here—was unable to reproduce with Chrome or Firefox using most recent React nor with 16.12.

  7. liqwid commented on May 20, 2022

    @liqwid

    Found how to reproduce the bug easily and consistently:

    MacOs 11.6 20G165
    Chrome Version 101.0.4951.64 (Official Build) (x86_64)
    react, react-dom 17.0.2

    To reproduce turn off all browser extensions, and use the codesandbox in examples - it starts to reproduce consistently.

  8. spector32 commented on Dec 9, 2022

    @spector32

    This also happens for me on Brave:
    [Version 1.45.131 Chromium: 107.0.5304.110 (Official Build) (64-bit)]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions