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

React, Iframes, and a Back-Navigation Bug #84

Open
AleksandrHovhannisyan opened this issue May 29, 2021 · 16 comments
Open

React, Iframes, and a Back-Navigation Bug #84

AleksandrHovhannisyan opened this issue May 29, 2021 · 16 comments
Labels
comments Comments section for an article.

Comments

@AleksandrHovhannisyan
Copy link
Owner

No description provided.

@AleksandrHovhannisyan AleksandrHovhannisyan added the comments Comments section for an article. label May 29, 2021
@georgeloring
Copy link

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.

@AleksandrHovhannisyan
Copy link
Owner Author

Thanks! Glad to hear it helped 😄

@abhi-shek-gupta
Copy link

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

@AleksandrHovhannisyan
Copy link
Owner Author

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

@justinwerd
Copy link

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?

@AleksandrHovhannisyan
Copy link
Owner Author

@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!

@justinwerd
Copy link

@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.

@AleksandrHovhannisyan
Copy link
Owner Author

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

@solution-loisir
Copy link

solution-loisir commented Dec 2, 2021

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.

@Alessandro-Avallone-Developer

Great! solved my problem

@poseneror
Copy link

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!

@Maarondesigns
Copy link

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.

@cannacavender
Copy link

Thank you so much for the detailed write up

@AnujK1Singh
Copy link

Thanks Very much for the detailed solution.

@jhxxs
Copy link

jhxxs commented Mar 24, 2023

Thanks a lot.

@Pertempto
Copy link

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comments Comments section for an article.
Projects
None yet
Development

No branches or pull requests