Skip to content

Commit

Permalink
Re-use the response from forceSSL.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikecao committed Apr 5, 2022
1 parent 6eaf090 commit 9696a5f
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions pages/_middleware.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
import { NextResponse } from 'next/server';

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;
}
}

function customScriptName(req) {
const scriptName = process.env.TRACKER_SCRIPT_NAME;

Expand All @@ -30,8 +20,16 @@ function disableLogin(req) {
}
}

function forceSSL(req, res) {
if (process.env.FORCE_SSL && req.nextUrl.protocol === 'http:') {
res.headers.set('Strict-Transport-Security', 'max-age=31536000; includeSubDomains');
}

return res;
}

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

for (const fn of fns) {
const res = fn(req);
Expand All @@ -40,5 +38,5 @@ export function middleware(req) {
}
}

return NextResponse.next();
return forceSSL(req, NextResponse.next());
}

0 comments on commit 9696a5f

Please sign in to comment.