Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
pcattori committed Feb 13, 2024
1 parent 71f1d59 commit da0b2fb
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 69 deletions.
2 changes: 1 addition & 1 deletion integration/vite-cloudflare-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const files: Files = async ({ port }) => ({
typescript: "^5.1.6",
vite: "^5.1.0",
"vite-tsconfig-paths": "^4.2.1",
wrangler: "^3.24.0",
wrangler: "^3.28.2",
},
engines: {
node: ">=18.0.0",
Expand Down
25 changes: 23 additions & 2 deletions packages/remix-cloudflare-pages/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,19 @@ export type GetLoadContextFunction<
Data extends Record<string, unknown> = Record<string, unknown>
> = (args: {
request: Request;
context: { cloudflare: EventContext<Env, Params, Data> };
context: {
cloudflare: EventContext<Env, Params, Data> & {
cf: EventContext<Env, Params, Data>["request"]["cf"];
ctx: {
waitUntil: EventContext<Env, Params, Data>["waitUntil"];
passThroughOnException: EventContext<
Env,
Params,
Data
>["passThroughOnException"];
};
};
};
}) => AppLoadContext | Promise<AppLoadContext>;

export type RequestHandler<Env = any> = PagesFunction<Env>;
Expand All @@ -41,7 +53,16 @@ export function createRequestHandler<Env = any>({
return async (cloudflare) => {
let loadContext = await getLoadContext({
request: cloudflare.request,
context: { cloudflare },
context: {
cloudflare: {
...cloudflare,
cf: cloudflare.request.cf!,
ctx: {
waitUntil: cloudflare.waitUntil,
passThroughOnException: cloudflare.passThroughOnException,
},
},
},
});

return handleRequest(cloudflare.request, loadContext);
Expand Down
6 changes: 1 addition & 5 deletions packages/remix-dev/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,4 @@ export type {
ServerBundlesFunction,
VitePluginConfig,
} from "./vite";
export {
vitePlugin,
cloudflarePreset,
cloudflareProxyVitePlugin,
} from "./vite";
export { vitePlugin, cloudflareProxyVitePlugin } from "./vite";
27 changes: 17 additions & 10 deletions packages/remix-dev/vite/cloudflare-proxy-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { createRequestHandler } from "@remix-run/server-runtime";
import {
createRequestHandler,
type AppLoadContext,
type ServerBuild,
} from "@remix-run/server-runtime";
import { type Plugin } from "vite";
// TODO: make wrangler import lazy??
import { type PlatformProxy, getPlatformProxy } from "wrangler";

// TODO: make wrangler import lazy??
// TODO: auto-set ssr resolve conditions for workerd

import {
Expand All @@ -16,22 +17,28 @@ import {

let serverBuildId = "virtual:remix/server-build";

type LoadContext = {
cloudflare: PlatformProxy;
type Env = Record<string, unknown>;
type CfProperties = Record<string, unknown>;

type LoadContext<E extends Env, Cf extends CfProperties> = {
cloudflare: PlatformProxy<E, Cf>;
};

type GetLoadContext = (args: {
type GetLoadContext<E extends Env, Cf extends CfProperties> = (args: {
request: Request;
context: LoadContext;
}) => Promise<Record<string, unknown>>;
context: LoadContext<E, Cf>;
}) => AppLoadContext | Promise<AppLoadContext>;

export const cloudflareProxyVitePlugin = (
options: { getLoadContext?: GetLoadContext } = {}
export const cloudflareProxyVitePlugin = <
E extends Env,
Cf extends CfProperties
>(
options: { getLoadContext?: GetLoadContext<E, Cf> } = {}
): Plugin => {
return {
name: "vite-plugin-remix-cloudflare-proxy",
async configureServer(viteDevServer) {
let cloudflare = await getPlatformProxy();
let cloudflare = await getPlatformProxy<E, Cf>();
let context = { cloudflare };
return () => {
if (!viteDevServer.config.server.middlewareMode) {
Expand Down
1 change: 0 additions & 1 deletion packages/remix-dev/vite/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,4 @@ export const vitePlugin: RemixVitePlugin = (...args) => {
return remixVitePlugin(...args);
};

export { cloudflarePreset } from "./presets/cloudflare";
export { cloudflareProxyVitePlugin } from "./cloudflare-proxy-plugin";
50 changes: 0 additions & 50 deletions packages/remix-dev/vite/presets/cloudflare.ts

This file was deleted.

0 comments on commit da0b2fb

Please sign in to comment.