Skip to content

Commit 2816342

Browse files
committed
template
1 parent 3a81c6f commit 2816342

File tree

4 files changed

+44
-12
lines changed

4 files changed

+44
-12
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,2 @@
11
/// <reference types="@remix-run/cloudflare" />
22
/// <reference types="vite/client" />
3-
4-
import type { KVNamespace } from "@cloudflare/workers-types";
5-
6-
declare module "@remix-run/cloudflare" {
7-
interface AppLoadContext {
8-
env: {
9-
MY_KV: KVNamespace;
10-
};
11-
}
12-
}

templates/unstable-vite-cloudflare/functions/[[path]].ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import { createPagesFunctionHandler } from "@remix-run/cloudflare-pages";
44
// @ts-ignore - the server build file is generated by `remix vite:build`
55
// eslint-disable-next-line import/no-unresolved
66
import * as build from "../build/server";
7+
import { getLoadContext } from "../load-context";
78

89
export const onRequest = createPagesFunctionHandler({
910
build,
10-
getLoadContext: (context) => ({ env: context.env }),
11+
getLoadContext,
1112
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import type { KVNamespace } from "@cloudflare/workers-types";
2+
import { type AppLoadContext } from "@remix-run/cloudflare";
3+
4+
// In the future, types for bindings will be generated by `wrangler types`
5+
// See https://github.com/cloudflare/workers-sdk/pull/4931
6+
type Bindings = {
7+
// Add types for bindings configured in `wrangler.toml`
8+
MY_KV: KVNamespace;
9+
};
10+
11+
declare module "@remix-run/cloudflare" {
12+
interface AppLoadContext {
13+
env: Bindings;
14+
15+
extra: string; // Example of augmenting load context
16+
}
17+
}
18+
19+
// Subset of Cloudflare context supported by `getBindingsProxy` for use in Node-based environments like Vite
20+
// The Cloudflare team is working to improve their Node proxies to support:
21+
// - https://github.com/cloudflare/workers-sdk/issues/4875
22+
// - https://github.com/cloudflare/workers-sdk/issues/4876
23+
// - https://github.com/cloudflare/workers-sdk/issues/4879
24+
type Context = { request: Request; env: Bindings };
25+
26+
// Shared implementation compatible with Vite, Wrangler, and Cloudflare Pages
27+
export const getLoadContext = async (
28+
context: Context
29+
): Promise<AppLoadContext> => {
30+
return {
31+
...context,
32+
extra: "stuff", // Example of augmenting load context
33+
};
34+
};

templates/unstable-vite-cloudflare/vite.config.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,18 @@ import {
44
} from "@remix-run/dev";
55
import { defineConfig } from "vite";
66
import tsconfigPaths from "vite-tsconfig-paths";
7+
import { getBindingsProxy } from "wrangler";
8+
9+
import { getLoadContext } from "./load-context";
710

811
export default defineConfig({
912
plugins: [
1013
remix({
11-
presets: [cloudflare()],
14+
presets: [
15+
cloudflare(getBindingsProxy, {
16+
getRemixDevLoadContext: getLoadContext,
17+
}),
18+
],
1219
}),
1320
tsconfigPaths(),
1421
],

0 commit comments

Comments
 (0)