So I have a setup where I want to allow custom components to be created and referenced via an API fetch call made by my react client site.
I know using dynamic imports and React.lazy these can be loaded dynamically instead of with the rest of the bundle.
https://reactjs.org/docs/code-splitting.html
And I know ES6 imports support importing from urls
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import
So is it possible to load a component from a url? I tried and got the error:
OtherComponent.js hosted at url
function OtherComponent() {
return (
<div>Other component</div>
)
}
export default OtherComponent;
A snippet of my code
const url = '/url/to/OtherComponent.js'; // this is found via a fetch request, so not known beforehand
const OtherComponent = React.lazy(() => import(url));
...
<Suspense fallback={<div>Loading...</div>}>
<OtherComponent {...props} />
</Suspense>
Uncaught SyntaxError: Unexpected token '<'
I'm assuming because it's loading in a normal javascript file and not whatever modified chunk file the webpack build process creates.
Is what I'm trying to do impossible? Would the only way to be to rebuild with every new component added/modified?