kiliman / remix-express-vite-plugin

This package includes a Vite plugin to use in your Remix app. It configures an Express server for both development and production using TypeScript.
118 stars 6 forks source link

Context.get() is not a function #30

Open timzolleis opened 2 months ago

timzolleis commented 2 months ago

Details

Error description

Route loaders fail with the error context.get() is not a function at some point in the development process (this was, as far as I could see, completely random but happened at least once every 10 minutes)

I logged context, and it is just an empty object in this case (default remix server AppLoadContext)

Additional details

I saw that this occured (at least what I could record) when either the root.tsx file itself changed, or imports in that root file (for example the css file):

10:01:46 AM [vite] hmr invalidate /app/root.tsx Could not Fast Refresh. Learn more at https://github.com/vitejs/vite-plugin-react/tree/main/packages/plugin-react#consistent-components-exports
10:01:46 AM [vite] page reload app/root.tsx
10:01:46 AM [vite] hmr invalidate /app/root.tsx Could not Fast Refresh. Learn more at https://github.com/vitejs/vite-plugin-react/tree/main/packages/plugin-react#consistent-components-exports
10:01:46 AM [vite] page reload app/root.tsx

Maybe this is caused by function exports in my root.tsx file? Since removing them, I was not able to observe the error again.

Thanks for the great package! I'll post updates here, if I'm able to provide some kind of reproduction.

kiliman commented 2 months ago

Ugh. I thought I had resolved that issue. I guess it depends on how your code is set up.

I think the culprit is that the Remix Vite plugin will also load your SSR module and handle requests, at the same time the ExpressDevServer plugin also hooks into the request pipeline.

This means that sometimes the Remix handler will process the request before my plugin can set up the context.

One option is to set Vite up in middleware mode, which means I would have to create the dev server. Remix will not configure a handler if in middleware mode.

https://github.com/remix-run/remix/blob/bbead33bf530be030d16a1466b52cd99d077382a/packages/remix-dev/vite/plugin.ts#L1369-L1390