mirror of
https://github.com/umami-software/umami.git
synced 2026-02-04 04:37:11 +01:00
Move environment variables to middleware. Closes #972.
This commit is contained in:
parent
736347d37c
commit
7270d95240
6 changed files with 47 additions and 30 deletions
|
|
@ -5,7 +5,6 @@ import { IntlProvider } from 'react-intl';
|
|||
import { Provider } from 'react-redux';
|
||||
import { useStore } from 'redux/store';
|
||||
import useLocale from 'hooks/useLocale';
|
||||
import useForceSSL from 'hooks/useForceSSL';
|
||||
import 'styles/variables.css';
|
||||
import 'styles/bootstrap-grid.css';
|
||||
import 'styles/index.css';
|
||||
|
|
@ -25,7 +24,6 @@ const Intl = ({ children }) => {
|
|||
};
|
||||
|
||||
export default function App({ Component, pageProps }) {
|
||||
useForceSSL(process.env.FORCE_SSL);
|
||||
const store = useStore();
|
||||
const { basePath } = useRouter();
|
||||
|
||||
|
|
|
|||
44
pages/_middleware.js
Normal file
44
pages/_middleware.js
Normal file
|
|
@ -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();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue