Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

S3 Amazon Static Website with React?

I built a website using ReactJs, and to see the website, I generally do npm start and go to localhost:3000 via a browser.

I now want to host this website on S3, but without an EC2 instance. My understanding is that npm is a process, so it is server-side, and therefore, I would need to purchase compute to actually deploy my website.

I found this tutorial that does not mention paying for EC2 instance compute time: https://www.fullstackreact.com/articles/deploying-a-react-app-to-s3/

However, they still use NPM which makes me confused.

My question is: is it possible to use React if I were to only use Static S3 Website, without compute, and if so - how do I bypass NPM process?

React - component in seperate script does not work

In the above post, user tried to make a hello-world app in react, but all of the answers point them in the direction of making a server serve the content. I thought react is a front-end thing and can run without server-side processes. Is this true? Can someone explain why node.js is necessary or is paired with react, and whether is is possible to use react on s3 without ec2 compute?

like image 448
RebeccaK375 Avatar asked Aug 31 '17 20:08

RebeccaK375


3 Answers

It's possible to host a static react site entirely on s3. In this case, you would use node/npm only as build tools and to run your development server (localhost:3000).

npm would download your dependencies and you'd use node or gulp or webpack to build the assets into static files.

Then you would upload the files to s3 where it would serve the static files.

If you have some backend node code, then you would need to use ec2 or some other type of host. But if it's entirely static javascript, then there's no need for a node server.

Here are some links that might help explain in more detail:

  • https://medium.com/@omgwtfmarc/deploying-create-react-app-to-s3-or-cloudfront-48dae4ce0af
  • https://www.fullstackreact.com/articles/deploying-a-react-app-to-s3/
like image 175
Austin Greco Avatar answered Sep 22 '22 17:09

Austin Greco


Try Gatsby! Here: https://www.gatsbyjs.org/

From the Github page: "Blazing fast static site generator for React"

Once you've generated your static pages, you can deploy on S3, Github pages...the choice is yours!

like image 39
ericat Avatar answered Sep 21 '22 17:09

ericat


You can skip EC2 for your case. Here is why:

1) S3 Bucket + CloudFront (CDN) is really fast for static files serving. The React minified app is a group of static files which works best here. For the build of those minified files, I recommend using a CI/CD process or build them locally and just upload them to S3.

2) EC2 requires more work to setup, it consumes resources, it is not necessary for static files or React (unless you are using ReactDOMServer for dynamic content serving), and Node.js is not recommended for static files (Node.js get's blocked since it is single threaded so it is a best practice to keep static files away from it).

Here is a good article on the topic using Angular as an example: https://www.quora.com/Should-I-use-AWS-EC2-to-host-an-Angular-web-app-or-AWS-S3

like image 32
Xavier Reyes Ochoa Avatar answered Sep 22 '22 17:09

Xavier Reyes Ochoa