question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

webpackJsonp configuration (still) missing to allow multiple react app on single site

See original GitHub issue

Describe the bug

As already described in other tickets ( https://github.com/facebook/create-react-app/issues/6631 ) CRA does not support changing the webpack config parameter output.webpackJsonp ( https://webpack.js.org/configuration/output/#outputjsonpfunction ). This makes CRA generated apps not being able to coexist on a single page. Both apps will try to set window.webpackJsonp and thus override the webpack chunk registry.

Steps to reproduce

  1. Create two CRA apps and build them. They have to have chunks
  2. Deploy both apps on a single index.html
  3. Open the index.html. Only one app will show up

Expected behavior

The parameter does not have to be configurable at all. It would be enough to generate a unique string at build time for the webpack config. A build hash could do the trick. Alternatively, a configuration of the webpack output.webpackjsonp config to allow multiple CRA apps on a single page would solve the issue. Consider a widget approach where each CRA App will be a widget on a portal.

Actual behavior

No chance to reconfigure the property thus no way to make multiple CRA apps work. Each CRA App will use the webpack default value and thus break on coexistance. The other ticket ( https://github.com/facebook/create-react-app/issues/6631 ) suggests a “search and replace” npm package solution. That works, but feels absolutely wrong. You simply do not search-and-replace an compiled code.

Best, Jan

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:12

github_iconTop GitHub Comments

2reactions
JanThielcommented, Jan 8, 2020

@zurcacielos Thank you for your reply 😃 Yes, this would indeed be kind of a “solution” to get it done. Although those CRA config layers tend to deprecate quite often or are run over by CRA changes. So they add complexity and and error-proneness to our projects. Saw this happening with a few of the “old” solutions to “configure” CRA which make me try to avoid them for our (longer living) enterprise projects.

So for “optional” config stuff I would agree that using a CRA config layer would be sufficient. But in this case, it is quite hard to explain someone, why 2 CRA apps would not be able to run on a single page. And it could and should be solved transparently by CRA with no one having to figure this out by themselves, and then even being required to add another package on top auf CRA to fix the webpack config.

Maybe this is a webpack design flaw after all. But using CRA should not make your life harder as far as I understand. Maybe CRA even does not want to support multiple react apps on a single page. This would be also a fair decission. But then it should be clearly documented.

I believe it’s clear, that I would vote for fixing this instead of adding docs stating “does not work out of the box”, but this is something the CRA project should decide.

Best, Jan

1reaction
kirill-konshincommented, Jan 27, 2020

@JanThiel at the end of the README it says that if you need more customization you have alternatives. It seems very outdated. Did you try CRACO? https://github.com/gsoft-inc/craco

Thanks a lot for your suggestion, I’ve fixed the issue with webpackJsonp this way:

// craco.config.js
module.exports = {
    webpack: {
        configure: (webpackConfig, {env, paths}) => {
            webpackConfig.output.jsonpFunction = 'webpackJsonpDemoReact'; //THIS IS NEEDED TO DISTINGUISH FROM OTHER RUNNING WEBPACKS
            return webpackConfig;
        },
    },
};
Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeError: Cannot read property 'webpackJsonp' of undefined
The solution was in the webpack config: target: 'node'. Target defaults to web , thereby allowing creation of async chunks.
Read more >
Hosting multiple React applications on the same document
One simple/naive approach to break up a big single page application is to split it into different single page applications which then are...
Read more >
Create Apps with No Configuration – React Blog
Create React App is a new officially supported way to create single-page React applications. It offers a modern build setup with no ...
Read more >
Add React to a Website
React components are a great way to do that. The majority of websites aren't, and don't need to be, single-page apps. With a...
Read more >
Create React App 2.0: Babel 7, Sass, and More
And this is exactly what Create React App 2.0 contributors have been busy with for the past few months: migrating the configuration and ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found