Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Issues serving static files from sub folders in Google Cloud Storage Bucket

 

I have a very simple React application which I built using the npm run build command. This generates a build folder containing an index.html file and a sub-folder called static with CSS, JS, and media files. Here’s the structure:

build/

├── asset-manifest.json
├── favicon.ico
├── index.html
├── logo192.png
├── logo512.png
├── manifest.json
├── robots.txt

└── static/
├── css/
│ ├── main.fd921077.css
│ └── main.fd921077.css.map

├── js/
│ ├── 787.a1dc0f1b.chunk.js
│ ├── 787.a1dc0f1b.chunk.js.map
│ ├── main.7d56bfaa.js
│ ├── main.7d56bfaa.js.LICENSE.txt
│ └── main.7d56bfaa.js.map

└── media/
└── logo.8c7360d016c3baff0193f89f1077c721.svg

I uploaded the contents of the build folder to a Google Cloud Storage bucket and made the bucket public with Object Viewer access to allUsers. However, I encountered an issue:

Here’s the content of index.html:

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<link rel="icon" href="/favicon.ico"/>
<meta name="viewport" content="width=device-width,initial-scale=1"/>
<meta name="theme-color" content="#000000"/>
<meta name="description" content="Web site created using create-react-app"/>
<link rel="apple-touch-icon" href="/logo192.png"/>
<link rel="manifest" href="/manifest.json"/>
<title>Simple React App</title>
<script defer="defer" src="/static/js/main.4aebbb13.js"></script>
<link href="/static/css/main.fd921077.css" rel="stylesheet">
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
</body>
</html>

Problem:

  • The paths in index.html for static files (JS, CSS) do not include the bucket name, causing them to not load properly.

Workaround: As a temporary fix, I added "homepage": "https://storage.googleapis.com/myapp-bucket-202409081110" to my package.json, which resolved the issue and the app is working as expected. However, I’d prefer not to rely on this workaround and would like GCP to handle the path configuration automatically.

How can I resolve this issue without adding the homepage field to package.json?

Any guidance on how to handle this directly through Google Cloud Platform without modifying the package.json file would be greatly appreciated!

Solved Solved
0 1 467
1 ACCEPTED SOLUTION

Hi @Souradip22,

Welcome to Google Cloud Community!

To resolve the issue of static files not loading properly from your Google Cloud Storage (GCS) bucket without modifying the homepage field in package.json, you may consider the following options:

I hope the above information is helpful.

View solution in original post

1 REPLY 1

Hi @Souradip22,

Welcome to Google Cloud Community!

To resolve the issue of static files not loading properly from your Google Cloud Storage (GCS) bucket without modifying the homepage field in package.json, you may consider the following options:

I hope the above information is helpful.