The Static runtime lets you deploy static websites and frontend apps (HTML, CSS,
JavaScript, and media files) to Cloud Run
[from source](https://docs.cloud.google.com/run/docs/deploying-source-code.md.txt) using a preconfigured Nginx web
server. You don't need to write a separate Dockerfile or configure a web server.

You can use the Static runtime to host prebuilt outputs from single-page
application (SPA) frameworks, such as React, Vue, and Angular, and static site
generators (SSGs), such as Astro, Hugo, Jekyll, Eleventy, and Next.js or Nuxt
static exports. Your service receives Nginx and OS security patches with
[automatic base image updates](https://docs.cloud.google.com/run/docs/configuring/services/automatic-base-image-updates.md.txt).

The key advantages of using the Static runtime include the following:

- **Zero-configuration serving** : Point Cloud Run to any directory that contains an `index.html` file.
- **Kernel-level performance** : Uses an optimized Nginx configuration with `sendfile`, `tcp_nopush`, `tcp_nodelay`, and open-file caching.
- **Automatic compression** : Provides built-in dynamic gzip compression and precompressed static asset serving (`gzip_static on;`). For more information, see [Serve precompressed assets](https://docs.cloud.google.com/docs/buildpacks/static.md.txt#precompressed-assets).
- **Clean URLs** : Maps requested paths such as `/about` to `/about.html` without requiring an explicit `.html` extension.
- **SPA client-side routing** : Automatically falls back to `/index.html` for client-side routes, such as `/dashboard/settings`.
- **Built-in security** : Includes `X-Content-Type-Options: nosniff` headers, blocks requests to hidden files and directories, such as `.git` and `.env`, and runs as a non-root user.
- **Default caching headers** : Automatically includes `ETag` and `Last-Modified` headers so browsers can revalidate unchanged assets with a `304 Not Modified` response, while omitting default `Cache-Control: max-age` headers so new deployments take effect immediately.

## Limitations

The Static runtime only serves static files. It doesn't
support custom server-side code, custom `nginx.conf` overrides, or
reverse-proxying requests to backend Cloud Run services from
within the container.

To route requests between a static frontend and a backend
Cloud Run service under a single domain, such as routing `/api/*`
to a backend service, use an
[external Application Load Balancer](https://docs.cloud.google.com/load-balancing/docs/https/setup-global-ext-https-serverless.md.txt)
or
[Firebase Hosting rewrites](https://firebase.google.com/docs/hosting/cloud-run#direct_requests_to_container).

## Supported stack

The Static runtime is available on the
[`google-24`](http://us-central1-docker.pkg.dev/serverless-runtimes/google-24/runtimes/static24)
stack. To target the static base image, use the runtime alias `static24`.

For more information about the support schedule, see the [Runtime
lifecycle](https://docs.cloud.google.com/docs/buildpacks/runtime-support#static).

## Directory detection

The Static runtime doesn't scan subdirectories, such as `build/` or `dist/`.
Your static files, including `index.html`, must be in the current
directory or in the directory specified by the `--source` flag, such as
`--source ./dist`. The runtime automatically configures Nginx to serve your
static files on port `8080`, which is the default port for
Cloud Run deployments.

For example, deployment fails if your `index.html` file is inside a
subdirectory such as `dist/index.html` and you run the deploy command with
`--source .` instead of `--source ./dist`:

- **Without build (`--no-build`)** : Nginx serves directly from the `/workspace` root directory, and the deployed service returns a `403
  Forbidden` error.
- **With build**: Google Cloud's buildpacks fails to detect your static files during the build step.

> [!NOTE]
> **Note:** The `gcloud run deploy` command respects `.gitignore` and `.gcloudignore` files. Because standard frontend repositories ignore the `dist/` and `build/` directories by default, make sure your static output files aren't excluded by `.gitignore` or `.gcloudignore` when you deploy.

To support single-page applications (SPAs) with client-side routing, the Static
runtime automatically configures Nginx to handle fallback paths. If a requested
path doesn't match a physical file, directory, or `.html` file, Nginx serves
`/index.html` as a fallback without redirecting the browser URL, allowing
client-side routers to resolve the route.

## Serve pre-compressed assets

To reduce response sizes and CPU usage for large files, such as JavaScript
bundles, deploy pre-compressed `.gz` files with your static files. When a
request includes the `Accept-Encoding: gzip` header and a `.gz` file exists for
the requested file, Nginx serves the `.gz` file directly. For example, a
request for `/app.js` returns the contents of the `app.js.gz` file.

If a `.gz` file doesn't exist, Nginx compresses eligible responses on every
request. This compression uses CPU each time and favors speed over size, so
responses are larger than they need to be. Pre-compressed files avoid the
per-request work and can be smaller.

Keep each original file next to its `.gz` file. Nginx requires the original
file to resolve the request and to respond to clients that don't accept gzip
encoding.

The following example compresses the HTML, CSS, JavaScript, JSON, and SVG files
in a directory and keeps the original files. The `-9` flag produces the
smallest files:

```bash
find SOURCE -type f \
  \( -name "*.html" -o -name "*.css" -o -name "*.js" \
     -o -name "*.json" -o -name "*.svg" \) \
  -exec gzip -9 --keep {} +
```

Replace <var translate="no">SOURCE</var> with the path to the directory that contains your
`index.html` file, such as `./dist`.

## Deploy to Cloud Run

Deploy your static files to Cloud Run using the
[`gcloud run deploy`](https://docs.cloud.google.com/sdk/gcloud/reference/run/deploy.md.txt) command. You can choose
between the following deployment methods:

- **Without build (`--no-build`)** : recommended for prebuilt files. Uploads your static files directly onto the `static24` base image and bypasses Cloud Build. Use this option when your HTML, CSS, and JavaScript files are already built locally or in your continuous integration (CI) pipeline.
- **With build**: runs Cloud Build and Google Cloud's buildpacks to
  package your static files into a container image in Artifact Registry before
  deploying.

### Without build

To deploy prebuilt static files without a build step, point the `--source`
flag directly to the directory that contains your `index.html` file:

```bash
gcloud beta run deploy SERVICE \
  --source SOURCE \
  --no-build \
  --base-image=static24 \
  --project PROJECT_ID
```

Replace the following:

- <var translate="no">SERVICE</var>: the name of your Cloud Run service.
- <var translate="no">SOURCE</var>: the path to the directory that contains your `index.html` file, such as `./dist` or `.`.
- <var translate="no">PROJECT_ID</var>: the Google Cloud project ID.

For more information about deploying from source without build, see
[Deploy services from source code](https://docs.cloud.google.com/run/docs/deploying-source-code.md.txt#deploy_from_source_without_build).

### With build

To deploy your static assets from source with a build step, point the
`--source` flag directly to the directory that contains your `index.html`
file:

```bash
gcloud beta run deploy SERVICE \
  --source SOURCE \
  --base-image=static24 \
  --project PROJECT_ID
```

Replace the following:

- <var translate="no">SERVICE</var>: the name of your Cloud Run service.
- <var translate="no">SOURCE</var>: the path to the directory that contains your `index.html` file, such as `./dist` or `.`.
- <var translate="no">PROJECT_ID</var>: the Google Cloud project ID.

For more information about deploying from source with build, see
[Deploy services from source code](https://docs.cloud.google.com/run/docs/deploying-source-code.md.txt#deploy-from-source).