@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
mdx 176 lines 4.78 KB
Raw
1 ---
2 title: Static sites
3 description: Deploy Hugo, Zola, or any static build to Google Cloud — on Cloud Run with GCP.Website.StaticSite, or from a public Cloud Storage bucket.
4 sidebar:
5 order: 30
6 ---
7
8 You can host a static site on Google Cloud two ways. `StaticSite` runs
9 your build command and serves the output from Cloud Run. A public
10 Cloud Storage bucket serves files with no server at all.
11
12 ## On Cloud Run
13
14 `GCP.Website.StaticSite` runs any build command that writes a
15 directory, then serves that directory from a small file server:
16
17 ```typescript
18 // alchemy.run.ts
19 import * as Alchemy from "alchemy";
20 import * as GCP from "@alchemy.run/gcp";
21 import * as Effect from "effect/Effect";
22
23 export const Blog = GCP.Website.StaticSite("Blog", {
24 build: { command: "hugo --minify", output: "public" },
25 });
26
27 export default Alchemy.Stack(
28 "MyBlog",
29 { providers: GCP.providers(), state: GCP.state() },
30 Effect.gen(function* () {
31 const site = yield* Blog;
32 return { url: site.url };
33 }),
34 );
35 ```
36
37 Install `@alchemy.run/frontend-frameworks` as a dev dependency and
38 keep Docker running when you deploy. An unchanged site skips both the
39 build and the image push.
40
41 ### Single-page apps
42
43 Answer unknown paths with `index.html`:
44
45 ```typescript
46 export const App = GCP.Website.StaticSite("App", {
47 build: { command: "npm run build", output: "dist" },
48 assets: { notFoundHandling: "single-page-application" },
49 });
50 ```
51
52 ### Build in a subdirectory
53
54 ```typescript
55 export const Web = GCP.Website.StaticSite("Web", {
56 path: "apps/web",
57 build: { command: "npm run build", output: "dist" },
58 });
59 ```
60
61 ### Use your dev server
62
63 Under `alchemy dev`, run your own dev command instead of the build:
64
65 ```typescript
66 export const App = GCP.Website.StaticSite("App", {
67 build: { command: "npm run build", output: "dist" },
68 dev: { command: "npm run dev" },
69 });
70 ```
71
72 ## From a Cloud Storage bucket
73
74 A bucket serves files directly. There's no container and no Docker.
75
76 ### Create the bucket
77
78 ```typescript
79 const bucket = yield* GCP.Storage.Bucket("Site", {
80 uniformBucketLevelAccess: true,
81 website: { mainPageSuffix: "index.html", notFoundPage: "404.html" },
82 forceDestroy: true,
83 });
84 ```
85
86 ### Let anyone read it
87
88 ```typescript
89 yield* GCP.IAM.Member("PublicRead", {
90 kind: "storage.bucket",
91 name: bucket.bucketName,
92 role: "roles/storage.objectViewer",
93 member: "allUsers",
94 });
95 ```
96
97 Your organization may block `allUsers` grants with the
98 `storage.publicAccessPrevention` or `iam.allowedPolicyMemberDomains`
99 policies.
100
101 ### Upload the files
102
103 `Storage.Files` uploads a directory, one object per file:
104
105 ```typescript
106 const files = yield* GCP.Storage.Files("SiteFiles", {
107 bucketName: bucket.bucketName,
108 path: fileURLToPath(new URL("./site", import.meta.url)),
109 cacheControl: (key) =>
110 key.endsWith(".html") ? "no-cache" : "public, max-age=300",
111 });
112 ```
113
114 A redeploy uploads only changed files and deletes objects for files
115 you removed. `Content-Type` comes from each file's extension.
116
117 ### Put it together
118
119 ```typescript
120 // alchemy.run.ts
121 import { fileURLToPath } from "node:url";
122 import * as Alchemy from "alchemy";
123 import * as GCP from "@alchemy.run/gcp";
124 import * as Output from "alchemy/Output";
125 import * as Effect from "effect/Effect";
126
127 export default Alchemy.Stack(
128 "MySite",
129 { providers: GCP.providers(), state: GCP.state() },
130 Effect.gen(function* () {
131 const bucket = yield* GCP.Storage.Bucket("Site", {
132 uniformBucketLevelAccess: true,
133 website: { mainPageSuffix: "index.html", notFoundPage: "404.html" },
134 forceDestroy: true,
135 });
136
137 yield* GCP.IAM.Member("PublicRead", {
138 kind: "storage.bucket",
139 name: bucket.bucketName,
140 role: "roles/storage.objectViewer",
141 member: "allUsers",
142 });
143
144 yield* GCP.Storage.Files("SiteFiles", {
145 bucketName: bucket.bucketName,
146 path: fileURLToPath(new URL("./site", import.meta.url)),
147 cacheControl: (key) =>
148 key.endsWith(".html") ? "no-cache" : "public, max-age=300",
149 });
150
151 return {
152 url: Output.interpolate`https://storage.googleapis.com/${bucket.bucketName}/index.html`,
153 };
154 }),
155 );
156 ```
157
158 On `storage.googleapis.com` the bucket name is part of the path, so
159 link pages to each other with relative URLs. `mainPageSuffix` and
160 `notFoundPage` only apply once the bucket is served from your own
161 domain through a load balancer.
162
163 [`examples/gcp-static-site`](https://github.com/alchemy-run/alchemy/tree/main/examples/gcp-static-site)
164 is the complete bucket version with a live test.
165
166 ## Which one?
167
168 Use `StaticSite` when you want an `https://….run.app` URL, SPA
169 routing, and 404 pages with no extra setup. Use a bucket when you want
170 no server at all and will put a load balancer in front for your
171 domain.
172
173 ## Where next
174
175 - [Websites](/gcp/frontend/websites) — framework sites on Cloud Run.
176 - [Cloud Storage](/gcp/data/storage) — buckets and their bindings.