remix-middleware provides an Express-like middleware stack for Remix's `loader` and `action` functions, allowing developers to centralize cross-cutting concerns such as authentication, logging, and response manipulation. Currently at version `0.1.1`, this library is still considered experimental, indicating that its API may evolve rapidly. It differentiates itself by deeply integrating with Remix's server-side data functions and offering specialized middleware for common Remix patterns, including streamlined integration with `remix-auth`. While there isn't a fixed release cadence, development appears active, with several minor releases building out core features and addressing early architectural concerns, particularly around preventing server-only code from being bundled for the browser.
npm install remix-middlewareVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to set up `remix-middleware` with a global logger and then apply it to a Remix `loader` and `action` to process requests and set responses, including basic form handling. It also outlines the optional setup for `remix-auth` integration.
Review the new API usage documented for `0.1.0` and ensure your middleware setup correctly uses the updated patterns to manage server-only code. Specifically, ensure `mdw.run` is used to wrap your loader/action logic.
Monitor GitHub releases and the project's README for updates. Consider pinning to a specific patch version if stability is critical, and be prepared for potential refactoring when upgrading.
Ensure that your terminal middleware function (the one passed to `mdw.run`) or one of the preceding middleware in the stack explicitly sets `ctx.response = yourData` or `ctx.response = new Response(...)`.
Declare your middleware creator like `export const authed = createMiddleware<AuthCtx<User>>();` where `User` is your authenticated user type.
Ensure your project, especially test setups or unusual build configurations, is correctly configured for ESM. Check `tsconfig.json` for `module` and `moduleResolution` settings (e.g., `"module": "esnext", "moduleResolution": "bundler"` for newer Node/Remix projects) and your `package.json` `type` field (set to `"module"`).
Verify that the final function passed to `mdw.run(props, (ctx) => { /* ... */ })` explicitly assigns `ctx.response = yourData` or `ctx.response = new Response(...)`. The `mdw.response` helper can simplify this for simple JSON data.When creating your middleware instance that uses `isAuthenticated`, explicitly define its context type: `export const authed = createMiddleware<AuthCtx<User>>();` where `User` is your application's user type.