Hosting two React apps from two Buckets on one CloudFront distribution. How to deal with two static directories?

Viewed 289

I have two React apps I want to serve with one CloudFront distribution. They both rely on react-router for navigation.

I have two S3 buckets configured as origins. The default (*) behaviour is set to my one bucket that hosts the landing page. I want certain other URLs (like /dashboard, /task) to use my other bucket.

This works fine setting up behaviours that pattern match those certain paths, but I'm stuck on one thing. Building a React app defaults scripts that the app needs to run to a "static" directory. I have to set a behaviour static* for one or the other and only the one that the behaviour points to works.

Is there anything I can do to work around this? From searching I haven't found any way to tell npm run-script build to build an app that uses a differently named static directory.

Thank you.

1 Answers

I took the following steps to fix this:

In package.json add the option "homepage": "landing"

Rebuild the react app with npm run-script build

Upload the files to the bucket but put assets and static into a landing directory.

Add a new behaviour on CloudFront that redirects landing/* to the landing bucket.

It seems to work fine now.

Related