Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Host a static site single page app in Google Cloud Storage with routes

There are guides and questions all over the place on how to do this, but never really a concrete answer that is satisfactory. Basically, I'm wondering if it's possible to host a static SPA (HTML/CSS/JS) in GCP Cloud Storage.

The main caveat of this is that the SPA has its own routing system (ReactRouter) so I want all paths to be served by index.html.

Most guides will tell you to set the ErrorDocument to index.html instead of 404.html. While this is a clever hack, it causes the site's HTTP response code to be 404 which is a disaster for SEO or monitoring tools. So that will work, as long as I can change the response code.

Is there any way to make this work? I have CloudFlare up and running too but from what I can tell there are no ways to trim the path or change the response status from there.

like image 646
Matt Dodge Avatar asked Apr 02 '18 17:04

Matt Dodge


People also ask

How do I host a static site in Google Drive?

To host a web page on Google Drive: Create a folder in Google Drive and set the sharing permission to “Public on the Web.” Upload the HTML, JavaScript and CSS files for your web page to the new folder. Select the HTML file, open it and click the “Preview” button in the toolbar.

Can I host my website on Google Cloud?

Use our free trial to start hosting your website or web app with any Google Cloud product. Watch an overview on how to build and host a website on Google Cloud. Learn how to serve or migrate a website or choose an option in our technical article.


2 Answers

A good approach here is to use Google App Engine to host a static SPA. https://cloud.google.com/appengine/docs/standard/python/getting-started/hosting-a-static-website

You can use the app.yaml file to map urls to the static file. Here’s an example:

runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /
  static_files: www/index.html
  upload: www/index.html

- url: /(.*)
  static_files: www/\1
  upload: www/(.*)

Documentation for app.yaml https://cloud.google.com/appengine/docs/standard/python/config/appref

like image 65
Federico Panunzio Avatar answered Dec 11 '22 20:12

Federico Panunzio


One way to circumvent the problem is to use server-side rendering. In SSR all client requests are passed to a backend app so there's no need for a Cloud Storage-hosted index.html.

This of course comes with its own set of complications but we're avoiding the above-mentioned 404 hack or resorting to any further dependencies like App Engine.

Alternatively you could go with hash-based routing, i.e. paths like https://example.com/#some-path.

like image 35
Aleksi Avatar answered Dec 11 '22 22:12

Aleksi