-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
templates(cloudflare-workers): use static assets (#10034)
Co-authored-by: Brooks Lybrand <[email protected]>
- Loading branch information
1 parent
b70e0fd
commit 04abe37
Showing
6 changed files
with
45 additions
and
55 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,22 @@ | ||
import { type PlatformProxy } from "wrangler"; | ||
|
||
// PlatformProxy’s caches property is incompatible with the caches global | ||
// https://github.com/cloudflare/workers-sdk/blob/main/packages/wrangler/src/api/integrations/platform/caches.ts | ||
type Cloudflare = Omit<PlatformProxy<Env>, "dispose" | "caches"> & { | ||
caches: CacheStorage; | ||
}; | ||
type GetLoadContextArgs = { | ||
request: Request; | ||
context: { | ||
cloudflare: Omit<PlatformProxy<Env>, "dispose" | "caches" | "cf"> & { | ||
caches: PlatformProxy<Env>['caches'] | CacheStorage; | ||
cf: Request['cf']; | ||
}; | ||
}; | ||
} | ||
|
||
declare module "@remix-run/cloudflare" { | ||
interface AppLoadContext { | ||
cloudflare: Cloudflare; | ||
// eslint-disable-next-line @typescript-eslint/no-empty-interface | ||
interface AppLoadContext extends ReturnType<typeof getLoadContext> { | ||
// This will merge the result of `getLoadContext` into the `AppLoadContext` | ||
} | ||
} | ||
|
||
export function getLoadContext({ context }: GetLoadContextArgs) { | ||
return context; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,37 @@ | ||
import { getAssetFromKV } from "@cloudflare/kv-asset-handler"; | ||
import { createRequestHandler, type ServerBuild } from "@remix-run/cloudflare"; | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore This file won’t exist if it hasn’t yet been built | ||
import * as build from "./build/server"; // eslint-disable-line import/no-unresolved | ||
// eslint-disable-next-line import/no-unresolved | ||
import __STATIC_CONTENT_MANIFEST from "__STATIC_CONTENT_MANIFEST"; | ||
import { getLoadContext } from "./load-context"; | ||
|
||
const MANIFEST = JSON.parse(__STATIC_CONTENT_MANIFEST); | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const handleRemixRequest = createRequestHandler(build as any as ServerBuild); | ||
|
||
export default { | ||
async fetch(request, env, ctx) { | ||
const waitUntil = ctx.waitUntil.bind(ctx); | ||
const passThroughOnException = ctx.passThroughOnException.bind(ctx); | ||
try { | ||
const url = new URL(request.url); | ||
const ttl = url.pathname.startsWith("/assets/") | ||
? 60 * 60 * 24 * 365 // 1 year | ||
: 60 * 5; // 5 minutes | ||
return await getAssetFromKV( | ||
{ request, waitUntil }, | ||
{ | ||
ASSET_NAMESPACE: env.__STATIC_CONTENT, | ||
ASSET_MANIFEST: MANIFEST, | ||
cacheControl: { | ||
browserTTL: ttl, | ||
edgeTTL: ttl, | ||
const loadContext = getLoadContext({ | ||
request, | ||
context: { | ||
cloudflare: { | ||
// This object matches the return value from Wrangler's | ||
// `getPlatformProxy` used during development via Remix's | ||
// `cloudflareDevProxyVitePlugin`: | ||
// https://developers.cloudflare.com/workers/wrangler/api/#getplatformproxy | ||
cf: request.cf, | ||
ctx: { | ||
waitUntil: ctx.waitUntil.bind(ctx), | ||
passThroughOnException: ctx.passThroughOnException.bind(ctx), | ||
}, | ||
caches, | ||
env, | ||
}, | ||
} | ||
); | ||
} catch (error) { | ||
// No-op | ||
} | ||
|
||
try { | ||
const loadContext = { | ||
cloudflare: { | ||
// This object matches the return value from Wrangler's | ||
// `getPlatformProxy` used during development via Remix's | ||
// `cloudflareDevProxyVitePlugin`: | ||
// https://developers.cloudflare.com/workers/wrangler/api/#getplatformproxy | ||
cf: request.cf, | ||
ctx: { waitUntil, passThroughOnException }, | ||
caches, | ||
env, | ||
}, | ||
}; | ||
}); | ||
return await handleRemixRequest(request, loadContext); | ||
} catch (error) { | ||
console.log(error); | ||
return new Response("An unexpected error occurred", { status: 500 }); | ||
} | ||
}, | ||
} satisfies ExportedHandler<Env & { __STATIC_CONTENT: KVNamespace<string> }>; | ||
} satisfies ExportedHandler<Env>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,14 @@ | ||
#:schema node_modules/wrangler/config-schema.json | ||
name = "remix-cloudflare-workers-template" | ||
|
||
main = "./server.ts" | ||
workers_dev = true | ||
# https://developers.cloudflare.com/workers/platform/compatibility-dates | ||
compatibility_date = "2023-04-20" | ||
compatibility_date = "2024-09-26" | ||
|
||
[site] | ||
bucket = "./build/client" | ||
[assets] | ||
# https://developers.cloudflare.com/workers/static-assets/binding/ | ||
directory = "./build/client" | ||
|
||
[build] | ||
command = "npm run build" |