Skip to content

Commit fbfc4e0

Browse files
committed
fix(site): combine page stylesheets from the assets binding
1 parent dc5cdef commit fbfc4e0

3 files changed

Lines changed: 26 additions & 11 deletions

File tree

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// The Worker's bindings, in the shape SST's Resource had: `Resource.App.stage` and
2-
// `Resource.NAME.value`. STAGE is a text binding in services/www/cloudflare.config.ts; the other
3-
// names are Worker secrets set on the www Worker. Reading a name that isn't bound throws, as SST's
4-
// Resource did, so callers keep their fallbacks. package.json `imports` picks this file for builds.
2+
// `Resource.NAME.value`. STAGE is a text binding in cloudflare.config.ts; the other names are
3+
// Worker secrets set on the site Worker. Reading a name that isn't bound throws, as SST's Resource
4+
// did, so callers keep their fallbacks. package.json `imports` picks this file for builds.
55
import { env } from "cloudflare:workers"
6-
import type { Resources } from "./resource.node"
6+
import type { AssetsBinding, Resources } from "./resource.node"
77

88
export { waitUntil } from "cloudflare:workers"
99

@@ -13,7 +13,16 @@ export const Resource = new Proxy({} as Resources, {
1313
get(_target, name: string) {
1414
if (name === "App") return { stage: bindings.STAGE === "production" ? "production" : "dev" }
1515
const value = bindings[name]
16-
if (typeof value !== "string" || !value) throw new Error(`"${name}" is not bound to the www Worker`)
16+
if (typeof value !== "string" || !value) throw new Error(`"${name}" is not bound to the site Worker`)
1717
return { value }
1818
},
1919
})
20+
21+
/**
22+
* The Worker's static assets. Code that needs a built file reads it here: a fetch to the site's
23+
* own URL would not reach this Worker again (Cloudflare sends a Worker's subrequests to its own
24+
* routes past it).
25+
*/
26+
export function assets() {
27+
return bindings.ASSETS as AssetsBinding
28+
}

‎services/site/src/lib/resource.node.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
// `vite dev`'s stand-in for resource.cloudflare.ts: the same shape, read from process.env
2-
// (STAGE and any secrets you export), and waitUntil that just awaits.
2+
// (STAGE and any secrets you export), waitUntil that just awaits, and no assets binding (the dev
3+
// server serves built files itself).
34
export type Resources = { App: { stage: "production" | "dev" } } & Record<string, { value: string }>
45

6+
export type AssetsBinding = { fetch(request: Request): Promise<Response> }
7+
58
export const waitUntil = async (promise: Promise<unknown>) => {
69
await promise
710
}
@@ -14,3 +17,7 @@ export const Resource = new Proxy({} as Resources, {
1417
return { value }
1518
},
1619
})
20+
21+
export function assets(): AssetsBinding | undefined {
22+
return undefined
23+
}

‎services/site/src/routes/stylesheet/[file].ts‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { APIEvent } from "@solidjs/start/server"
2-
import { getRequestEvent } from "solid-js/web"
2+
import { assets } from "#resource"
33
import { edgeCache } from "~/lib/edge-cache"
44

55
// A page's stylesheets as one file (lib/one-stylesheet.ts): /stylesheet/<name>~<name>….css joins
@@ -20,13 +20,12 @@ export async function GET(event: APIEvent) {
2020
const hit = cache && (await cache.match(key).catch(() => undefined))
2121
if (hit) return hit
2222

23-
const assets = (
24-
getRequestEvent()?.nativeEvent.context as { cloudflare?: { env?: { ASSETS?: { fetch: typeof fetch } } } }
25-
).cloudflare?.env?.ASSETS
23+
// From the assets binding: a fetch of the site's own URL would not come back to this Worker.
24+
const binding = assets()
2625
const parts = await Promise.all(
2726
names.map(async (part) => {
2827
const request = new Request(`${url.origin}/_build/assets/${part}.css`)
29-
const response = await (assets ? assets.fetch(request) : fetch(request))
28+
const response = await (binding ? binding.fetch(request) : fetch(request))
3029
return response.ok ? response.text() : undefined
3130
}),
3231
)

0 commit comments

Comments
 (0)