Replies: 2 comments 3 replies
-
I used the instructions from Vite+Cloudflare documentation https://remix.run/docs/en/main/future/vite#augmenting-load-context I'm not sure how your If See Epic Stack https://github.com/epicweb-dev/epic-stack/blob/main/docs/decisions/031-imports.md [Fixed doc link] // package.json
{
"imports": {
"#*": "./*"
},
} import { dispatch } from "#app/dipatch"; // load-context.ts
import { type AppLoadContext } from "@remix-run/cloudflare";
import { type PlatformProxy } from "wrangler";
import { dispatch } from "./app/dipatch";
// When using `wrangler.toml` to configure bindings,
// `wrangler types` will generate types for those bindings
// into the global `Env` interface.
// Need this empty interface so that typechecking passes
// even if no `wrangler.toml` exists.
interface Env {}
type Cloudflare = Omit<PlatformProxy<Env>, "dispose">;
declare module "@remix-run/cloudflare" {
interface AppLoadContext {
cloudflare: Cloudflare;
extra: string; // augmented
dispatch: typeof dispatch;
}
}
type GetLoadContext = (args: {
request: Request;
context: { cloudflare: Cloudflare }; // load context _before_ augmentation
}) => AppLoadContext;
// Shared implementation compatible with Vite, Wrangler, and Cloudflare Pages
export const getLoadContext: GetLoadContext = ({ context }) => {
return {
...context,
extra: "stuff",
dispatch,
};
}; // app/dispatch.ts
export function dispatch(message: string) {
return message;
} // appp/routes/test.tsx
export function loader({ context }: LoaderFunctionArgs) {
const { extra, dispatch } = context;
console.log("dispatch", dispatch("hello world"));
return json({ extra });
} |
Beta Was this translation helpful? Give feedback.
-
@kiliman I Just wanted to say that I nuked (removed) all the path aliases, and now it's working!!! If anyone in the future stumbles upon this post, please, do not barrel the exports (unless they're used together). Ex: // index.ts
export * from "./commands/CreateUserWithPasswordCommand.ts
export * from "./commands/ConnectRepoWithUserCommand.ts" As this can kill tree shaking and bundler splitting. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi everyone 🤗
I'm spearheading a project with Remix V2. First of all, thanks for this fantastic framework. It's a productivity powerhouse!
I posted this question in Stack Overflow:
https://stackoverflow.com/questions/78260782/injecting-custom-context-in-remix-v2-resolving-path-aliases-before-running-vi
But I wanted to keep this simple here.
I use Remix V2 with the default Vite builder and Cloudflare as the backend.
How or where can I inject a custom context?
Note that the custom context will include a dispatcher with a mediator to call commands and queries. I've had difficulties registering directly in Vite as it has path aliases, and it runs before Vite can do any transformations (See Stack Overflow question for more info)
Beta Was this translation helpful? Give feedback.
All reactions