forked from umami-software/umami
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move environment variables to middleware. Closes umami-software#972.
- Loading branch information
Showing
6 changed files
with
47 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { NextResponse } from 'next/server'; | ||
|
||
function redirectHTTPS(req) { | ||
if ( | ||
process.env.FORCE_SSL && | ||
!req.headers.get('host').includes('localhost') && | ||
req.nextUrl.protocol !== 'https' | ||
) { | ||
return NextResponse.redirect(`https://${req.headers.get('host')}${req.nextUrl.pathname}`, 301); | ||
} | ||
} | ||
|
||
function customScriptName(req) { | ||
const scriptName = process.env.TRACKER_SCRIPT_NAME; | ||
|
||
if (scriptName) { | ||
const url = req.nextUrl.clone(); | ||
const { pathname } = url; | ||
|
||
if (pathname.endsWith(`/${scriptName}.js`)) { | ||
url.pathname = '/umami.js'; | ||
return NextResponse.rewrite(url); | ||
} | ||
} | ||
} | ||
|
||
function disableLogin(req) { | ||
if (process.env.DISABLE_LOGIN && req.nextUrl.pathname.endsWith('/login')) { | ||
return new Response('403 Forbidden', { status: 403 }); | ||
} | ||
} | ||
|
||
export function middleware(req) { | ||
const fns = [redirectHTTPS, customScriptName, disableLogin]; | ||
|
||
for (const fn of fns) { | ||
const res = fn(req); | ||
if (res) { | ||
return res; | ||
} | ||
} | ||
|
||
return NextResponse.next(); | ||
} |