worker: fix assets binding and SPA routes
This commit is contained in:
parent
339aef50e0
commit
2e5fb0880f
9 changed files with 510 additions and 11 deletions
|
|
@ -115,17 +115,28 @@ export default {
|
|||
}
|
||||
|
||||
// Serve static assets from the Worker Assets binding.
|
||||
// SPA fallback: unknown paths serve the app shell (`/index.html`).
|
||||
// SPA routing: serve the app shell for "route-like" paths (e.g. `/watch`).
|
||||
//
|
||||
// Cloudflare's Assets binding may otherwise issue a redirect for unknown paths (e.g. `/watch` -> `/`),
|
||||
// which breaks stable share links.
|
||||
const assets = (env as unknown as { ASSETS?: Fetcher }).ASSETS;
|
||||
if (!assets || typeof (assets as any).fetch !== "function") {
|
||||
return new Response("Assets binding not configured", { status: 500 });
|
||||
}
|
||||
|
||||
const res = await assets.fetch(request);
|
||||
if (res.status !== 404) return res;
|
||||
const method = request.method.toUpperCase();
|
||||
const is_nav = method === "GET" || method === "HEAD";
|
||||
const is_likely_asset = url.pathname.includes(".");
|
||||
|
||||
url.pathname = "/index.html";
|
||||
return assets.fetch(new Request(url.toString(), request));
|
||||
// Use `/` as the app shell. Some asset handlers redirect `/index.html` -> `/`,
|
||||
// which turns SPA routes into 307s instead of serving the HTML.
|
||||
if (is_nav && !is_likely_asset && url.pathname !== "/") {
|
||||
const u = new URL(request.url);
|
||||
u.pathname = "/";
|
||||
return assets.fetch(new Request(u.toString(), request));
|
||||
}
|
||||
|
||||
return assets.fetch(request);
|
||||
},
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -12,9 +12,11 @@ routes = [
|
|||
{ pattern = "www.every.channel", custom_domain = true },
|
||||
]
|
||||
|
||||
# Static assets built by Trunk (apps/tauri/ui -> apps/tauri/dist)
|
||||
[assets]
|
||||
directory = "../../apps/web/dist"
|
||||
# Static assets built by Trunk (apps/web -> apps/web/dist).
|
||||
#
|
||||
# Note: Wrangler v4 expects `assets = { ... }` (not a `[assets]` table). If misconfigured,
|
||||
# `env.ASSETS` will be missing in production and the worker will 500 on SPA routes like `/watch`.
|
||||
assets = { directory = "../../apps/web/dist", binding = "ASSETS", run_worker_first = ["/*"] }
|
||||
|
||||
[[durable_objects.bindings]]
|
||||
name = "EC_API"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue