|
| 1 | +--- |
| 2 | +title: Setup Caddy for SPA Stored at S3 Object Storage. (React with react-router) |
| 3 | +authors: ajay_dhangar |
| 4 | +tags: [docker, self-hosting, SPA, React] |
| 5 | +description: Set up Caddy for serving a Single Page Application (SPA) like a React app with react-route. The react app is stored at a remote storage like S3, minio. |
| 6 | +--- |
| 7 | + |
| 8 | +To set up Caddy for serving a Single Page Application (SPA) like a React app with react-router, you need to handle routing such that all navigation requests are routed to `index.html`, while also serving the static assets correctly. |
| 9 | + |
| 10 | +{/* truncate */} |
| 11 | + |
| 12 | +## Directory Structure on Remote Storage |
| 13 | + |
| 14 | +Let's assume that the React app is stored at a remote storage like S3, minio, etc. The directory structure of the React app on the remote storage should look like this: |
| 15 | + |
| 16 | +```md title="Directory Structure" |
| 17 | +├── index.html |
| 18 | +├── static |
| 19 | +│ ├── css |
| 20 | +│ │ └── main.2d2b7b6a.chunk.css |
| 21 | +│ ├── js |
| 22 | +│ │ ├── 2.2d2b7b6a.chunk.js |
| 23 | +│ │ ├── main.2d2b7b6a.chunk.js |
| 24 | +│ │ └── runtime-main.2d2b7b6a.js |
| 25 | +│ └── media |
| 26 | +│ └── logo.2d2b7b6a.png |
| 27 | +``` |
| 28 | + |
| 29 | +## Caddyfile Configuration |
| 30 | + |
| 31 | +To serve the React app using Caddy, you need to create a `Caddyfile` with the following configuration: |
| 32 | + |
| 33 | +```md title="Caddyfile" |
| 34 | +your-domain.com { |
| 35 | + root * /path/to/your/react-app |
| 36 | + file_server |
| 37 | + try_files {path} /index.html |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +In the above configuration: |
| 42 | + |
| 43 | +- `your-domain.com` is your domain name. |
| 44 | +- `/path/to/your/react-app` is the path to your React app on the remote storage. |
| 45 | +- `file_server` is used to serve the static assets. |
| 46 | +- `try_files {path} /index.html` is used to route all navigation requests to `index.html`. |
| 47 | + |
| 48 | +## Docker Configuration |
| 49 | + |
| 50 | +If you are using Docker to run Caddy, you can create a `Dockerfile` with the following configuration: |
| 51 | + |
| 52 | +```dockerfile title="Dockerfile" |
| 53 | +FROM caddy:2.4.3 |
| 54 | + |
| 55 | +COPY Caddyfile /etc/caddy/Caddyfile |
| 56 | +``` |
| 57 | + |
| 58 | +You can then build the Docker image and run the container using the following commands: |
| 59 | + |
| 60 | +```bash title="Build and Run Docker Container" |
| 61 | +docker build -t caddy-spa . |
| 62 | +docker run -d -p 80:80 caddy-spa |
| 63 | +``` |
| 64 | + |
| 65 | +That's it! You have now set up Caddy to serve a Single Page Application (SPA) like a React app with react-router. |
| 66 | + |
| 67 | +## Conclusion |
| 68 | + |
| 69 | +In this article, we discussed how to set up Caddy for serving a Single Page Application (SPA) like a React app with react-router. By handling routing and serving static assets correctly, you can ensure that your SPA works as expected when hosted on a remote storage like S3, minio, etc. |
0 commit comments