diff --git a/hooks/useForceSSL.js b/hooks/useForceSSL.js new file mode 100644 index 00000000..b9d95e19 --- /dev/null +++ b/hooks/useForceSSL.js @@ -0,0 +1,14 @@ +import { useEffect } from 'react'; +import { useRouter } from 'next/router'; + +export default function useForceSSL(enabled) { + const router = useRouter(); + + useEffect(() => { + if (enabled && typeof window !== 'undefined' && /^http:\/\//.test(location.href)) { + router.push(location.href.replace(/^http:\/\//, 'https://')); + } + }, [enabled]); + + return null; +} diff --git a/next.config.js b/next.config.js index c1e31d7b..9d7d8e99 100644 --- a/next.config.js +++ b/next.config.js @@ -4,9 +4,7 @@ const pkg = require('./package.json'); module.exports = { env: { VERSION: pkg.version, - }, - serverRuntimeConfig: { - PROJECT_ROOT: __dirname, + FORCE_SSL: !!process.env.FORCE_SSL, }, webpack(config) { config.module.rules.push({ diff --git a/package.json b/package.json index c5553e41..f5fe6909 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "umami", - "version": "0.75.0", + "version": "0.76.0", "description": "A simple, fast, website analytics alternative to Google Analytics. ", "author": "Mike Cao ", "license": "MIT", diff --git a/pages/_app.js b/pages/_app.js index 9aad4339..2849d2f0 100644 --- a/pages/_app.js +++ b/pages/_app.js @@ -3,6 +3,7 @@ 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 { messages } from 'lib/lang'; import 'styles/variables.css'; import 'styles/bootstrap-grid.css'; @@ -21,6 +22,7 @@ const Intl = ({ children }) => { }; export default function App({ Component, pageProps }) { + useForceSSL(process.env.FORCE_SSL); const store = useStore(); return (