Skip to content

Commit

Permalink
Changed force SSL method to send headers.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikecao committed Apr 5, 2022
1 parent 22195a0 commit 6eaf090
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions pages/_middleware.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { NextResponse } from 'next/server';

function redirectHTTPS(req) {
const host = req.headers.get('host');
if (
process.env.FORCE_SSL &&
process.env.NODE_ENV === 'production' &&
req.nextUrl.protocol === 'http:'
) {
return NextResponse.redirect(`https://${host}${req.nextUrl.pathname}`, 301);
function forceSSL(req) {
if (process.env.FORCE_SSL && req.nextUrl.protocol === 'http:') {
const response = NextResponse.next();

response.headers.set('Strict-Transport-Security', 'max-age=31536000; includeSubDomains');

return response;
}
}

Expand All @@ -32,7 +31,7 @@ function disableLogin(req) {
}

export function middleware(req) {
const fns = [redirectHTTPS, customScriptName, disableLogin];
const fns = [customScriptName, disableLogin, forceSSL];

for (const fn of fns) {
const res = fn(req);
Expand Down

0 comments on commit 6eaf090

Please sign in to comment.