aleksandrhovhannisyan.com icon indicating copy to clipboard operation
aleksandrhovhannisyan.com copied to clipboard

React, Iframes, and a Back-Navigation Bug

Open AleksandrHovhannisyan opened this issue 3 years ago • 17 comments

AleksandrHovhannisyan avatar May 29 '21 13:05 AleksandrHovhannisyan

We just ran into this exact issue. Thanks for taking the time to put together this great write-up on it! Saved us a lot of time.

georgeloring avatar Jul 22 '21 16:07 georgeloring

Thanks! Glad to hear it helped 😄

AleksandrHovhannisyan avatar Jul 22 '21 19:07 AleksandrHovhannisyan

Hey @AleksandrHovhannisyan, in my case still facing the issue of the back button, tried both the solutions

abhi-shek-gupta avatar Aug 11 '21 09:08 abhi-shek-gupta

@abhi-shek-gupta Sorry to hear that! Unfortunately, without seeing the code, I can't really say what the problem might be.

AleksandrHovhannisyan avatar Aug 11 '21 11:08 AleksandrHovhannisyan

Thanks @AleksandrHovhannisyan for the informative article. one question I had was about the src property. I'm using typescript and am getting errors because src does not exist on the node object returned by cloneNode. is there another property that would be equivalent?

justinwerd avatar Sep 03 '21 13:09 justinwerd

@justinwerd Hmm, I haven't seen cloneNode used in React; could you share a small code sample to clarify what you're trying to do? Thanks!

AleksandrHovhannisyan avatar Sep 03 '21 15:09 AleksandrHovhannisyan

@AleksandrHovhannisyan Its a bit of an odd scenario. I have a parent application from my company (which I do not have control over), which renders an iframe that contains my react application. Since the iframe lies outside my direct control I am trying to access it via the plain JS queryselector method.

const oldIframe: HTMLIFrameElement = document.querySelector(
        "#parent-application-iframe"
    ) as HTMLIFrameElement;
    
    if (oldIframe) {
        const parentNode = oldIframe.parentNode;
        const newIframe: HTMLIFrameElement = oldIframe.cloneNode() as HTMLIFrameElement;
        if (newIframe) {
            newIframe.src = "some/source";
        }
        if (parentNode) {
            parentNode.replaceChild(newIframe, oldIframe);
        }
    }

casting the object to an HTMLIFrameElement seems to solve the src issue. So far however, I have not been able to access the iframe in the first place when serving my application to the parent application. I'm not sure I am even able to as it may present a cross domain security issue.

justinwerd avatar Sep 03 '21 17:09 justinwerd

Ah, yeah, a type assertion should do the trick. Not sure about the other question. Sorry that I can't be of more help!

AleksandrHovhannisyan avatar Sep 03 '21 18:09 AleksandrHovhannisyan

I have not been able to access the iframe in the first place when serving my application to the parent application. I'm not sure I am even able to as it may present a cross domain security issue.

I'm not sure I fully understand and I might not even be on topic here, but there's a way to communicate between an host and a frame (and the other way around) with the window.postMessage() method which allows cross-origin. It's often used by applications that exposes iframe to embed in one website for exemple: https://www.amilia.com/scripts/amilia-iframe.js.

solution-loisir avatar Dec 02 '21 01:12 solution-loisir

Great! solved my problem

I can't emphasis enough how grateful I am for stumbling across this one! We were struggling for days, scanning the internet for so long to understand why we get multiple entities in our history stack when navigating to specific pages on our app. Thank you so much!

poseneror avatar Oct 25 '22 07:10 poseneror

Thank you! I was wondering what my issue was and this is it. Unfortunately for me I am not changing the source of the iframe from the parent app, but rather navigating inside the iframe and communicating with the parent via postMessage, so it sounds like my solution will be more complex, but hopefully not too bad.

Maarondesigns avatar Dec 27 '22 20:12 Maarondesigns

Thank you so much for the detailed write up

cannacavender avatar Jan 09 '23 23:01 cannacavender

Thanks Very much for the detailed solution.

AnujK1Singh avatar Mar 15 '23 06:03 AnujK1Singh

Thanks a lot.

jhxxs avatar Mar 24 '23 06:03 jhxxs

Thank you!

Pertempto avatar Oct 31 '23 09:10 Pertempto