Serving Static Files

In addition to handling dynamic requests, applications often need to serve
additional static files such as JavaScript, images, and CSS. The
flexible environment allows you to decide how to serve static
content for your application. You can serve static content directly from your
application, host it on a GCP option like
Cloud Storage, or use a third-party content delivery network
(CDN).

Serving files from App Engine

Serving files from your app is typically straightforward, however, there are a
couple drawbacks that you should consider:

Requests for static files can use resources that otherwise would be used for
dynamic requests.

Depending on your configuration, serving files from your
app can result in response latency, which can also affect when new
instances are created for handling the load.

Tip: In production environments, it's generally best practice to serve your
static content separately from your app, either in
GCP or externally using a third-party
CDN.

Example of serving static files with your app

The PHP runtime runs nginx
to serve your app, which is configured to serve static files in
your project directory. You must declare the document root by specifying
document_root in your app.yaml file:

Serving files from GCP

Serving files from Cloud Storage

Cloud Storage can be used to host static assets for dynamic web
apps. The benefits of using Cloud Storage instead of serving directly
from your app include:

Cloud Storage essentially works as a content delivery network.
This does not require any special configuration because by default any
publicly readable object is cached in the global Cloud Storage network.

Your app's load will be reduced by offloading serving static assets to
Cloud Storage. Depending on how many static assets you have and the
frequency of access, this can reduce the cost of running your app by a
significant amount.

Bandwidth charges for accessing content can often be less with
Cloud Storage.