Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to embed an old JS application in an iframe in new React website

I have an older JS application that I want to modify a little and embed in a React website I am creating, it is legacy software so I can't rewrite it. I tried to integrate it but the older JS app has no node_modules and uses require.js to require local dependencies which seemed to cause issues as the requirejs.config() seemed to have no effect so none of the dependencies were available.

I thought it may be possible to run this older JS app separate to the rest of the website and thought maybe I could embed it in an iframe then communicate to it with window.postMessage() or similar, I know it's not as simple as creating an iframe with src="oldJSApp.html" so was hoping to get some help on how this would be achievable?

Was hoping it would be possible to bundle it with my existing build system (webpack) but know it may not be possible.

like image 523
Connor Cairns Avatar asked Feb 19 '21 17:02

Connor Cairns


People also ask

Can we use iframe in Reactjs?

In React, developers use iframes to create either a sandboxed component or an application that is isolated from its parent component. In an iframe, when a piece of content is embedded from an external source, it is completely controlled by the source instead of the website it is embedded in.

How do I set the iframe content of a react component?

We can simply use the ReactDOMServer. renderToString() to convert a react component to string and then set it to srcDoc attribute of iframe.


1 Answers

I ended up going with my original idea, hosting the old JS app on its own on a different server and using an iframe to render it in, then window.postMessage() to communicate when required. Ended up being a much better solution than I expected.

like image 68
Connor Cairns Avatar answered Nov 07 '22 19:11

Connor Cairns