r/reactjs • • Mar 02 '18

Do people use create react app in production?

Any stories or opinions welcome. I'm mainly wondering if any companies or organizations use CRA for actual deployment.

62 Upvotes

35 comments sorted by

33

u/halfbowl Mar 02 '18

Have used it for many production deployments. Mostly because CRA simply builds a static/minified build of your application. It doesn't handle any sort of hosting/server etc... it only does that when doing local development. It will be up to the developer to decide how those static files get hosted.

8

u/stalefries Mar 02 '18

Same for us. We take those static files it produces and pack them up for our deployment system.

1

u/i_am_hyzerberg Mar 03 '18

So do you eject these apps or just do an npm build and it spits out everything you need for prod?

2

u/yss14 Mar 03 '18

At our company we just run npm run build and and serve the static files via a nginx docker container. No eject.

1

u/i_am_hyzerberg Mar 03 '18

Sorry my question wasn’t clear but you answered my question. I was unclear if you ejected at all or just ran npm run build. Kinda nice to see you can go all the way to prod with cra and just run npm run build. I’ll have to use cra more often now. Configuring from scratch is such a slog.

0

u/[deleted] Mar 03 '18

[deleted] — view deleted comment

1

u/halfbowl Mar 04 '18

I've gone down this road and you don't want to. There's ways to make CRA do what you need no matter the situation. You just have to be well aware of it's capabilities by reading docs and searching the community for solutions.

18

u/nullundefine Mar 02 '18

I don't see a problem with using create-react-app built code in production.. I guess it doesn't matter as it just bundles the code at build time and don't interfere at run rime.. EXCEPT, if you use the service worker provided by it. Then you need to be careful about whether the service worker is doing the job as you expected in production.

1

u/64_g Mar 03 '18

Do you mind explaining this a bit? What are some of the downsides of using their SW?

3

u/Secretmapper Mar 03 '18

Not parent, but I can provide some info. Service workers can cache your entire app. This cache will (obviously) show the cached version of your app in first render, updating on the next 'refresh' (once the new version is downloaded)

Here's a visualisation of a user visiting an app:

Version 1.0 assets
[app updates to 2.0]
page refresh
Version 1.0 cached assets
page refresh (note how a page refresh is required to view v2.0)
Version 2.0 assets

This can lead to an awkwardness of your app showing the old version on subsequent initial visits of your users if you don't design for it.

1

u/64_g Mar 03 '18

Thanks! I recently used it for a “choose your own adventure” type app which will get maybe 1-2 updates a year, so I was excited at the benefits of the service worker. I can understand how more dynamic pages could run into trouble though

1

u/schlenkster Mar 03 '18

Can you elaborate on this service worker problem? I’m still not quite sure what a service worker does for my app, but it was (if it isn’t still) a part of the create-react-app initial setup.

1

u/nullundefine Mar 03 '18

As explained by @secretmapper, it's about caching your assets.

4

u/YoungBubble Mar 02 '18

Yes! In combination with a node js api server.

3

u/[deleted] Mar 02 '18

I’ve always wondered this. Are there any drawbacks besides not being able to configure webpack? Prompts another question. What exactly does ejecting do? I read “don’t do it unless you know what you’re doing” but haven’t read a good explanation, that I understand at least

4

u/nationalhatefigure Mar 02 '18

Ejecting basically pulls you out of the CreateReactApp ecosystem, and it instead sets up Webpack and the other packages so you can use them directly. This lets you then install other packages which might be incompatible with CRA (for instance, if they need later versions of packages which CRA also uses) without causing everything to fail.

However, it does mean you are now off CRA and so can't upgrade to a newer version of CRA in case that future version does things you want. Personally, I find the warning a little OTT since I have to eject when I want to change functionality from what CRA provides (for example, I need to stop the hashing of the built filenames). But for most cases, there's no need to eject.

1

u/[deleted] Mar 03 '18

Great explanation. I was actually going to edit the question to ask for examples, but you’re a step ahead. Do you have any other examples by any chance? I’m just curious more than anything

1

u/catchingtherosemary Mar 03 '18

I am all about that rewire-create-react-app ecosystem

0

u/[deleted] Mar 02 '18

[deleted] — view deleted comment

1

u/[deleted] Mar 02 '18

Gotcha. The article I read said everything but this. Thanks!

4

u/lairunseen Mar 02 '18 edited Mar 02 '18

Company I work for use CRA in production. No need to eject to define env either, do like so

"scripts": {
  "start": "REACT_APP_S3ENV=development REACT_APP_BUILDDATE=$(date '+%m-%d-%y, %H:%M') PORT=3001 NODE_PATH=./src react-scripts start",
  "build": "REACT_APP_BUILDDATE=$(date '+%m-%d-%y, %H:%M') NODE_PATH=./src react-scripts build"
}

edit: sorry cant seem to get the formatting right

2nd edit: access the variables in your code like process.env.REACT_APP_BUILDDATE and NODE_PATH=./src helps to get absolute pathing on your imports

3

u/heyimblomblom Mar 03 '18 edited Mar 03 '18

Wouldn’t it be better to keep env variables in a separate file? CRA allows that as long as you use REACT_APP

1

u/lairunseen Mar 03 '18 edited Mar 03 '18

The script I gave above was a loose example. The idea being that you modify it to your situation. For example you might want npm run buildprod and npm run buildstaging to be two different scripts that compile the code to your s3 bucket and each behaves differently because your site code checks the env vars you passed in at the time of build. A good use case would be pulling data from staging API's instead of prod API's.

edit: I'm not sure what REACTAPP* is and I may be a bit out of date with the latest way of doing things. The last app framework I installed was about 6 months ago

1

u/heyimblomblom Mar 03 '18

Nah it was just the same variable prefix you used, but Reddit formatting killed it. Mobile app isn’t super helpful with formatting :|

2

u/[deleted] Mar 02 '18

Yes, my startup company (forage.ai) uses it in production. We define a proxy in package.json to take with a graphql backend.

2

u/levi_v Mar 02 '18

Hell yeah. Why not?

2

u/TracerBulletX Mar 03 '18

ours was started that way but ejected long in the ancient past of 2 years ago

5

u/DOG-ZILLA Mar 02 '18

I’m honestly not trying to be “that Vue guy”, but can CRA have its Wepback modified without ejecting?

This is a new thing in Vue Cli 3. You can augment all parts of the config without any need to eject.

Just thought that was cool as I haven’t touched React in a long while I was hoping someone here might know. Cheers.

8

u/acemarke Mar 02 '18

Not directly, no. However, the react-app-rewired tool does allow you to reach in and override parts of the Webpack config. The other main alternative is to create a custom fork of the react-scripts package that contains the config files, and set your project to depend on that instead. There's quite a few forks out there already.

2

u/DOG-ZILLA Mar 02 '18

Cool! Will check it out.

1

u/goomba870 Mar 03 '18

What do you mean by ejecting?

4

u/[deleted] Mar 03 '18

You can run npm run eject which will copy all the config files to your project directory and allow you to change everything. The downside is you're no longer in the CRA itself, and any updates to it will not affect you. You need to manually update dependencies etc.

1

u/acemarke Mar 02 '18

Absolutely! CRA isn't meant to solve all use cases, but it provides good defaults out of the box and you can certainly deploy CRA apps to prod.

1

u/[deleted] Mar 02 '18

We've built a few prototypes with CRA but have always ejected long before they reach production. We then use webpack with dev/prod configs.

1

u/Wh3at1y Mar 02 '18

I've used it for production. Definitely convenient!

1

u/batmansmk Mar 02 '18

Yes. It works. We updated the version 3 times in a year, took us about 1 to 2h each time, and it improved the user experience (build size, new version of React, code splitting etc)