This package provides a simple Next.js middleware to automatically redirect HTTP requests to HTTPS, primarily useful for hosting environments like Heroku that do not offer native 'force SSL' functionality out of the box. It leverages Next.js's middleware system to intercept incoming requests and issue a 301 (or configurable) redirect when the `x-forwarded-proto` header indicates an HTTP connection. The current stable version is 0.1.5. As a focused utility, it likely has a low release cadence, receiving updates mainly for critical bug fixes or compatibility with new Next.js versions. Its key differentiator is its simplicity and direct integration into the Next.js middleware architecture, offering a lightweight solution without complex server-side configurations. It aims to solve a specific problem for specific hosting providers.
npm install next-ssl-redirect-middlewareVerified import paths — ran on the pinned version, not inferred.
This code sets up the SSL redirect middleware to force HTTPS on 'production' and 'staging' environments with a 302 temporary redirect.
Consult the Next.js migration guide for middleware and check for an updated version of `next-ssl-redirect-middleware`. Consider pinning your Next.js version to avoid unexpected breakages.
Pass an `environments` array to `sslRedirect` with all desired `NODE_ENV` values, e.g., `sslRedirect({ environments: ['production', 'staging'] })`.Only enable this middleware if your hosting environment explicitly requires it (e.g., certain Heroku setups, custom Node.js servers). Disable it if your platform (like Vercel or Netlify) provides automatic HTTPS enforcement.
Ensure your hosting provider's proxy or load balancer correctly sets and forwards the `x-forwarded-proto` header (e.g., `x-forwarded-proto: http` or `x-forwarded-proto: https`). Consult your hosting provider's documentation.
Ensure `middleware.ts` (or `.js`) is correctly placed at the root of your Next.js project and that your Next.js version is compatible with the middleware API being used. Verify the file is recognized by Next.js.
Verify that your hosting environment (e.g., load balancer, proxy) correctly sets `x-forwarded-proto` to `https` for requests that have already been redirected. Check for other server-side or CDN-level SSL configurations that might be interfering. Ensure the `environments` option is configured correctly and not causing redirects in an already secure context.
Run `npm install next-ssl-redirect-middleware` or `yarn add next-ssl-redirect-middleware` in your project's root directory. Double-check that the import statement `import sslRedirect from 'next-ssl-redirect-middleware';` is exactly correct.