You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Given that React and ReactDOM are so prevalent and therefore likely to exist in the browser cache, it seems to make sense to have an option to build the package with an option to to include React and ReactDOM statically in the HTML file.
It would make the initial load that much snappier.
I agree. There are currently reasons why we can’t easily do this (addons reach into React internals). But with React 16 this will no longer be a problem and I think we can reconsider offering this.
Reacted by Marc Schipperheyn, Richard, Sergey Makarov, Felix Schlitter, Ryan Harvey, Peter Kerpedjiev, Fritz Lekschas, atlanteh, SydAmir, Jorge Gonzalez and 9 more
have you offered this in the newest version of create-react-app?
No but I’m open to suggestions for how it should work! I think maybe we can do this automatically if we detect CDN links in the HTML? What do you think?
Hello! I would love to contribute to create-react-app and this seems an easy enough project for me. I would be happy to put into practice @gaearon 's suggestion. Which files should I look at?
I was thinking the way @Ayc0 used to implement vendors in #3145 could be reused for this and #3878 - Im currently working in a project where some resources are loaded via CDN statically in index.html and its far from optimal. Its pretty convenient to have one source of truth of your packages and dependencies - package.json - easier to know which versions you are using and to keep track of updates.
Anyway Im just getting started with #3878 lets see how it goes.
with this proposal, CDN is opt in. you are consciously choosing to use CDN. so we don't need to handle fallback mechanism for the user. (I am learning that a lot of people dislike CDNs on principle)
@mastilver Good to know, thanks. In the context of CRA, there'd need to be customisation which mightn't align with no customising Webpack (eg @gaearon's [comment](#2758 (comment) above). Perhaps CRA having its own resolver implementation that looks up package.json or some other config location might be the answer.
I think both @czebe and @davidjb might be overlooking @iansu's suggestion: "One option might be to look in index.html for CDN links and then don't bundle React and/or ReactDOM if they're present."
edit: i am unsubscribing to this issue bc i am not so keen on it anymore. hopefully yarn plug n play comes soon.
Is there any plans for this feature ^ to be incorporated into create-react-app? Ideally I'm look for a sound way to exclude specified modules and to include my own cdn links. From what I can see dynamic-cdn-webpack-plugin doesn't work with create-react-app, has anyone done this well yet?
Finally, update your root package.json's dependencies to point to the new shims (you may also need to nuke yarn.lock and node_modules to get the shims picked up):
This is a fair bit of work, and I feel like it's probably not even a good idea in most cases, but it'll let you externalize any packages you need with stock CRA.
The http cache in browsers isn't predictable anymore ( https://www.jefftk.com/p/shared-cache-is-going-away ). Browsers are moving a away from single http cache to partitioned cache (for security reasons).
In 2019 it's not safe to make an assumption that React is in the cache anymore. Also cheap to free hosting options for single page apps has advanced even further in the recent years.
A better way to increase performance in your apps is to work with code splitting.
Therefor its time to close this issue. Sorry for any inconvenience.
Activity
gaearon commentedon Jul 10, 2017
I agree. There are currently reasons why we can’t easily do this (addons reach into React internals). But with React 16 this will no longer be a problem and I think we can reconsider offering this.
chj-damon commentedon Jan 22, 2018
@gaearon have you offered this in the newest version of create-react-app?
swyxio commentedon Jan 22, 2018
hi! just had the same idea too. also wanted to point out it maybe doesnt take much more effort than configuring babel: https://www.youtube.com/watch?v=2rhkgB8Cohc
edit: i meant webpack, leaving my mistake as dan responded to me below
gaearon commentedon Jan 22, 2018
Not sure what this has to do with Babel.
No but I’m open to suggestions for how it should work! I think maybe we can do this automatically if we detect CDN links in the HTML? What do you think?
czebe commentedon Jan 22, 2018
Just offer a way to customize webpack.
I believe these scripts should go into "externals".
gaearon commentedon Jan 22, 2018
Nah, we're not going to allow to customize webpack :-) There are dozens of issues about this if you're wondering why.
yvesgurcan commentedon Jan 23, 2018
Hello! I would love to contribute to create-react-app and this seems an easy enough project for me. I would be happy to put into practice @gaearon 's suggestion. Which files should I look at?
andriijas commentedon Jan 23, 2018
I was thinking the way @Ayc0 used to implement vendors in #3145 could be reused for this and #3878 - Im currently working in a project where some resources are loaded via CDN statically in index.html and its far from optimal. Its pretty convenient to have one source of truth of your packages and dependencies - package.json - easier to know which versions you are using and to keep track of updates.
Anyway Im just getting started with #3878 lets see how it goes.
Ayc0 commentedon Jan 23, 2018
@andriijas I don't know if you can use the chunk plugin (or the new split chunk plugin) to replace a module by a cdn
the
externalfield of webpack can do this I thinkMaybe, we can add a field
externalsin the package.json that's gonna be read by webpackAyc0 commentedon Jan 23, 2018
https://github.com/mastilver/dynamic-cdn-webpack-plugin
iansu commentedon Jan 23, 2018
That webpack plugin looks like a good option. Does someone want to try it out and submit a PR?
swyxio commentedon Jan 24, 2018
giving it a shot. i think this could be an easy win.
@iansu - any thoughts on would this be a prod-only feature or both dev and prod?
swyxio commentedon Jan 24, 2018
I have implemented this only for prod. as one might expect it shaves 30kb off the bundle size (aka react)
chj-damon commentedon Jan 25, 2018
@sw-yx would you like to share your process that how you make it?
9 remaining items
czebe commentedon Feb 7, 2018
How would you handle situations when the CDN is not available? Fallback mechanism can be quite complex, impossible to define in HTML.
swyxio commentedon Feb 7, 2018
with this proposal, CDN is opt in. you are consciously choosing to use CDN. so we don't need to handle fallback mechanism for the user. (I am learning that a lot of people dislike CDNs on principle)
mastilver commentedon Feb 8, 2018
@davidjb You can already use your own implementation using the resolver options, I'm planning to add more "official" resolver (cdnjs for example)
https://github.com/mastilver/dynamic-cdn-webpack-plugin#optionsresolver
davidjb commentedon Feb 9, 2018
@mastilver Good to know, thanks. In the context of CRA, there'd need to be customisation which mightn't align with no customising Webpack (eg @gaearon's [comment](#2758 (comment) above). Perhaps CRA having its own resolver implementation that looks up package.json or some other config location might be the answer.
swyxio commentedon Feb 9, 2018
I think both @czebe and @davidjb might be overlooking @iansu's suggestion: "One option might be to look in index.html for CDN links and then don't bundle React and/or ReactDOM if they're present."
edit: i am unsubscribing to this issue bc i am not so keen on it anymore. hopefully yarn plug n play comes soon.
crebbo commentedon Oct 4, 2018
Is there any plans for this feature ^ to be incorporated into create-react-app? Ideally I'm look for a sound way to exclude specified modules and to include my own cdn links. From what I can see dynamic-cdn-webpack-plugin doesn't work with create-react-app, has anyone done this well yet?
ryouaki commentedon Oct 28, 2018
I am looking for the solution about this feature.
ryouaki commentedon Oct 28, 2018
But this is why we need eject the script from cra...... ( T_T )
Ayc0 commentedon Oct 28, 2018
If you want to modify the package.json, I don't recommand you to eject.
Instead try react-app-rewired. I'm using it on a big codebase and it works like a charm.
I'm also using it in cra2
Kiranprajapati2719 commentedon Nov 9, 2018
What about adding a custom react script allowing use of dynamic-cdn-webpack-plugin? Any possibility?
yushengyuan1993 commentedon May 21, 2019
there is a solution, it works for me:
apple:
banana:
cat:
you kan use
Reactlike this:dog:
i started learning react a few days ago, so i am still a newcomer to using react.
jamesknelson commentedon Nov 6, 2019
If anyone still wants to do this -- which I wouldn't recommend but sometimes you get odd requirements -- you can just add some shim packages.
First, you'll need to load React from the CDN of your choice, in index.html:
Then, add a
shims/reactdirectory withpackage.jsonandindex.jsfiles:Next, repeat this for
react-domandreact-is.Finally, update your root
package.json's dependencies to point to the new shims (you may also need to nuke yarn.lock and node_modules to get the shims picked up):{ "dependencies": { "react": "./shims/react", "react-dom": "./shims/react-dom", "react-is": "./shims/react-is", } }This is a fair bit of work, and I feel like it's probably not even a good idea in most cases, but it'll let you externalize any packages you need with stock CRA.
andriijas commentedon Nov 6, 2019
Thanks for the update @jamesknelson
The http cache in browsers isn't predictable anymore ( https://www.jefftk.com/p/shared-cache-is-going-away ). Browsers are moving a away from single http cache to partitioned cache (for security reasons).
In 2019 it's not safe to make an assumption that React is in the cache anymore. Also cheap to free hosting options for single page apps has advanced even further in the recent years.
A better way to increase performance in your apps is to work with code splitting.
Therefor its time to close this issue. Sorry for any inconvenience.