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.
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.
We can simply use the ReactDOMServer. renderToString() to convert a react component to string and then set it to srcDoc attribute of iframe.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With