Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

(improvement) Preparing for the next selfhosted release (#347)

Andrii Romasiun5cf41bcb88ad
diff --git a/Dockerfile b/Dockerfile
index 4c2b3607c..9f40fc96f 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,7 +1,7 @@
# Stage 1
FROM node:lts-alpine as build
ENV TZ=Etc/UTC \
- REACT_APP_SELFHOSTED=true \
+ SELFHOSTED=true \
CHOKIDAR_USEPOLLING=true \
GENERATE_SOURCEMAP=false
RUN apk add --no-cache tzdata && cp /usr/share/zoneinfo/$TZ /etc/localtime
@@ -9,14 +9,15 @@ WORKDIR /app
COPY . .
RUN chmod +x ./deployment/50-substitute-env-variables.sh
RUN npm install -g pnpm && pnpm install && npm run build
+ENV NODE_ENV=production
+CMD ["npm", "run", "start"]
# Stage 2
FROM nginx:stable-alpine
ENV TZ=Etc/UTC
RUN apk add --no-cache tzdata && cp /usr/share/zoneinfo/$TZ /etc/localtime
RUN rm -rf /usr/share/nginx/html/*
-COPY --from=build /app/build /usr/share/nginx/html
COPY --from=build /app/deployment/default.conf /etc/nginx/conf.d/default.conf
COPY --from=build /app/deployment/50-substitute-env-variables.sh /docker-entrypoint.d/
EXPOSE 80
-HEALTHCHECK CMD curl -f http://localhost/ || exit
+HEALTHCHECK CMD curl -f http://localhost/ || exit 1
\ No newline at end of file
diff --git a/app/App.tsx b/app/App.tsx
index 43be4efe7..284132059 100644
--- a/app/App.tsx
+++ b/app/App.tsx
@@ -17,7 +17,6 @@ import Footer from 'components/Footer'
import Loader from 'ui/Loader'
import ScrollToTop from 'hoc/ScrollToTop'
-import Selfhosted from 'hoc/Selfhosted'
import { getAccessToken } from 'utils/accessToken'
import { authActions } from 'redux/reducers/auth'
import sagaActions from 'redux/sagas/actions'
@@ -144,11 +143,9 @@ const App: React.FC<IApp> = ({ ssrTheme, ssrAuthenticated }) => {
)}
{/* @ts-ignore */}
<ScrollToTop>
- <Selfhosted>
- <Suspense fallback={<Fallback isMinimalFooter={isMinimalFooter} />}>
- <Outlet />
- </Suspense>
- </Selfhosted>
+ <Suspense fallback={<Fallback isMinimalFooter={isMinimalFooter} />}>
+ <Outlet />
+ </Suspense>
</ScrollToTop>
<Footer minimal={isMinimalFooter} authenticated={authenticated} />
</Suspense>
diff --git a/app/api/index.ts b/app/api/index.ts
index 4d33a19e5..05f726f3d 100644
--- a/app/api/index.ts
+++ b/app/api/index.ts
@@ -11,7 +11,7 @@ import { authActions } from 'redux/reducers/auth'
import sagaActions from 'redux/sagas/actions'
import { getAccessToken, removeAccessToken, setAccessToken } from 'utils/accessToken'
import { getRefreshToken, removeRefreshToken } from 'utils/refreshToken'
-import { DEFAULT_ALERTS_TAKE, isSelfhosted, API_URL } from 'redux/constants'
+import { DEFAULT_ALERTS_TAKE, isSelfhosted, isBrowser, API_URL } from 'redux/constants'
import { IUser } from 'redux/models/IUser'
import { IAuth } from 'redux/models/IAuth'
import { IProject, IOverall, IProjectNames } from 'redux/models/IProject'
@@ -21,7 +21,7 @@ import { ISubscribers } from 'redux/models/ISubscribers'
const debug = Debug('swetrix:api')
-const baseURL: string = isSelfhosted ? window.env.API_URL : API_URL
+const baseURL: string = isSelfhosted ? (isBrowser ? window.env.API_URL : null) : API_URL
const api = axios.create({
baseURL,
diff --git a/app/components/Footer/index.tsx b/app/components/Footer/index.tsx
index 0867425f5..6053fd4ef 100644
--- a/app/components/Footer/index.tsx
+++ b/app/components/Footer/index.tsx
@@ -126,7 +126,7 @@ const SelfHostedFooter = () => {
const { t } = useTranslation('common')
return (
- <footer className='bg-gray-800 dark:bg-slate-900 dark:border-t dark:border-slate-800/50'>
+ <footer className='bg-gray-50 dark:bg-slate-900 border-t border-gray-200 dark:border-slate-800/50'>
<div className='max-w-7xl mx-auto py-8 px-4 overflow-hidden sm:px-6 lg:px-8'>
<nav className='-mx-5 -my-2 flex flex-wrap justify-center' aria-label='Footer'>
<div className='px-5 py-2'>
diff --git a/app/hoc/Selfhosted.tsx b/app/hoc/Selfhosted.tsx
deleted file mode 100644
index ae71ef644..000000000
--- a/app/hoc/Selfhosted.tsx
+++ /dev/null
@@ -1,37 +0,0 @@
-import React, { useEffect } from 'react'
-import { useNavigate, useLocation } from '@remix-run/react'
-import _includes from 'lodash/includes'
-
-import { isSelfhosted } from 'redux/constants'
-import routes from 'routesPath'
-
-const selfHostedBlacklist = [
- routes.signup, routes.reset_password, routes.new_password_form, routes.main,
- routes.verify, routes.change_email, routes.billing, routes.open,
-]
-
-const DEFAULT_PAGE = routes.signin
-
-interface ISelfhosted {
- children: JSX.Element
-}
-
-const Selfhosted: React.FC<ISelfhosted> = ({ children }): JSX.Element => {
- const navigate = useNavigate()
- const location = useLocation()
-
- // eslint-disable-next-line consistent-return
- useEffect(() => {
- if (isSelfhosted) {
- if (_includes(selfHostedBlacklist, location.pathname)) {
- navigate(DEFAULT_PAGE)
- }
- }
- // TODO: Investigate this later. https://github.com/remix-run/react-router/discussions/8465
- // eslint-disable-next-line react-hooks/exhaustive-deps
- }, [location])
-
- return children
-}
-
-export default Selfhosted
diff --git a/app/root.tsx b/app/root.tsx
index 6cefe73e0..181a10280 100644
--- a/app/root.tsx
+++ b/app/root.tsx
@@ -188,7 +188,7 @@ export async function loader({ request }: LoaderArgs) {
API_STAGING_URL: process.env.API_STAGING_URL,
CDN_URL: process.env.CDN_URL,
BLOG_URL: process.env.BLOG_URL,
- SELFHOSTED: process.env.REACT_APP_SELFHOSTED,
+ SELFHOSTED: process.env.SELFHOSTED,
STAGING: process.env.STAGING,
}
@@ -252,6 +252,7 @@ export default function App() {
)}`,
}}
/>
+ <script src='/env.js' />
</head>
<body
className={clsx({
diff --git a/app/routes/billing.tsx b/app/routes/billing.tsx
index 351bd349a..6a2e11015 100644
--- a/app/routes/billing.tsx
+++ b/app/routes/billing.tsx
@@ -1,8 +1,10 @@
import type { LoaderArgs } from '@remix-run/node'
import { useLoaderData } from '@remix-run/react'
-import { json } from '@remix-run/node'
+import { json, redirect } from '@remix-run/node'
import type { SitemapFunction } from 'remix-sitemap'
import Billing from 'pages/Billing'
+
+import { isSelfhosted } from 'redux/constants'
import { isAuthenticated } from 'utils/server'
export const sitemap: SitemapFunction = () => ({
@@ -10,6 +12,10 @@ export const sitemap: SitemapFunction = () => ({
})
export async function loader({ request }: LoaderArgs) {
+ if (isSelfhosted) {
+ return redirect('/login', 302)
+ }
+
const isAuth = isAuthenticated(request)
return json({ isAuth })
diff --git a/app/routes/change-email.$id.tsx b/app/routes/change-email.$id.tsx
index be4246780..2d9208b58 100644
--- a/app/routes/change-email.$id.tsx
+++ b/app/routes/change-email.$id.tsx
@@ -1,12 +1,21 @@
import VerifyEmail from 'pages/Auth/VerifyEmail'
import type { SitemapFunction } from 'remix-sitemap'
import type { HeadersFunction } from '@remix-run/node'
+import { redirect } from '@remix-run/node'
+
+import { isSelfhosted } from 'redux/constants'
export const headers: HeadersFunction = ({ parentHeaders }) => {
parentHeaders.set('X-Frame-Options', 'DENY')
return parentHeaders
}
+export async function loader() {
+ if (isSelfhosted) {
+ return redirect('/login', 302)
+ }
+}
+
export const sitemap: SitemapFunction = () => ({
exclude: true,
})
diff --git a/app/routes/index.tsx b/app/routes/index.tsx
index fb0d80357..2463c10f4 100644
--- a/app/routes/index.tsx
+++ b/app/routes/index.tsx
@@ -1,16 +1,22 @@
import MainPage from 'pages/MainPage'
import type { LoaderArgs } from '@remix-run/node'
import { useLoaderData } from '@remix-run/react'
-import { json } from '@remix-run/node'
+import { json, redirect } from '@remix-run/node'
import type { SitemapFunction } from 'remix-sitemap'
import { detectTheme, isAuthenticated } from 'utils/server'
+import { isSelfhosted } from 'redux/constants'
export const sitemap: SitemapFunction = () => ({
priority: 1,
+ exclude: isSelfhosted,
})
export async function loader({ request }: LoaderArgs) {
+ if (isSelfhosted) {
+ return redirect('/login', 302)
+ }
+
const theme = detectTheme(request)
const isAuth = isAuthenticated(request)
diff --git a/app/routes/open.tsx b/app/routes/open.tsx
index 9f2d630df..da72d07de 100644
--- a/app/routes/open.tsx
+++ b/app/routes/open.tsx
@@ -1,4 +1,12 @@
import OpenStartup from 'pages/OpenStartup'
+import { redirect } from '@remix-run/node'
+import { isSelfhosted } from 'redux/constants'
+
+export async function loader() {
+ if (isSelfhosted) {
+ return redirect('/login', 302)
+ }
+}
export default function Index() {
return <OpenStartup />
diff --git a/app/routes/password-reset.$id.tsx b/app/routes/password-reset.$id.tsx
index 05280547e..947199e60 100644
--- a/app/routes/password-reset.$id.tsx
+++ b/app/routes/password-reset.$id.tsx
@@ -1,6 +1,8 @@
import CreateNewPassword from 'pages/Auth/CreateNewPassword'
import type { SitemapFunction } from 'remix-sitemap'
import type { HeadersFunction } from '@remix-run/node'
+import { redirect } from '@remix-run/node'
+import { isSelfhosted } from 'redux/constants'
export const headers: HeadersFunction = ({ parentHeaders }) => {
parentHeaders.set('X-Frame-Options', 'DENY')
@@ -11,6 +13,12 @@ export const sitemap: SitemapFunction = () => ({
exclude: true,
})
+export async function loader() {
+ if (isSelfhosted) {
+ return redirect('/login', 302)
+ }
+}
+
export default function Index() {
return <CreateNewPassword />
}
diff --git a/app/routes/recovery.tsx b/app/routes/recovery.tsx
index bd2a233d8..07ea11344 100644
--- a/app/routes/recovery.tsx
+++ b/app/routes/recovery.tsx
@@ -1,11 +1,19 @@
import ForgotPassword from 'pages/Auth/ForgotPassword'
import type { HeadersFunction } from '@remix-run/node'
+import { redirect } from '@remix-run/node'
+import { isSelfhosted } from 'redux/constants'
export const headers: HeadersFunction = ({ parentHeaders }) => {
parentHeaders.set('X-Frame-Options', 'DENY')
return parentHeaders
}
+export async function loader() {
+ if (isSelfhosted) {
+ return redirect('/login', 302)
+ }
+}
+
export default function Index() {
return <ForgotPassword />
}
diff --git a/app/routes/signup.tsx b/app/routes/signup.tsx
index 24bc5e9fd..0b97f372e 100644
--- a/app/routes/signup.tsx
+++ b/app/routes/signup.tsx
@@ -2,8 +2,9 @@ import Singup from 'pages/Auth/Signup'
import type { SitemapFunction } from 'remix-sitemap'
import type { HeadersFunction, LoaderArgs } from '@remix-run/node'
import { useLoaderData } from '@remix-run/react'
-import { json } from '@remix-run/node'
+import { json, redirect } from '@remix-run/node'
+import { isSelfhosted } from 'redux/constants'
import { detectTheme } from 'utils/server'
export const headers: HeadersFunction = ({ parentHeaders }) => {
@@ -12,6 +13,10 @@ export const headers: HeadersFunction = ({ parentHeaders }) => {
}
export async function loader({ request }: LoaderArgs) {
+ if (isSelfhosted) {
+ return redirect('/login', 302)
+ }
+
const theme = detectTheme(request)
return json({ theme })
@@ -19,6 +24,7 @@ export async function loader({ request }: LoaderArgs) {
export const sitemap: SitemapFunction = () => ({
priority: 0.9,
+ exclude: isSelfhosted,
})
export default function Index() {
diff --git a/app/routes/verify.$id.tsx b/app/routes/verify.$id.tsx
index d610ebc93..ffbf375d6 100644
--- a/app/routes/verify.$id.tsx
+++ b/app/routes/verify.$id.tsx
@@ -1,10 +1,18 @@
import VerifyEmail from 'pages/Auth/VerifyEmail'
import type { SitemapFunction } from 'remix-sitemap'
+import { redirect } from '@remix-run/node'
+import { isSelfhosted } from 'redux/constants'
export const sitemap: SitemapFunction = () => ({
exclude: true,
})
+export async function loader() {
+ if (isSelfhosted) {
+ return redirect('/login', 302)
+ }
+}
+
export default function Index() {
return <VerifyEmail />
}
diff --git a/deployment/default.conf b/deployment/default.conf
index df2ddfddd..fe8eeebaf 100644
--- a/deployment/default.conf
+++ b/deployment/default.conf
@@ -1,17 +1,11 @@
server {
listen 80;
- add_header Cross-Origin-Embedder-Policy "require-corp; report-to='default'";
- add_header Cross-Origin-Opener-Policy "same-site; report-to='default'";
- add_header Cross-Origin-Resource-Policy "same-site";
- add_header Permissions-Policy "interest-cohort=()";
- add_header Referrer-Policy "strict-origin-when-cross-origin";
- add_header X-Frame-Options "DENY";
- add_header X-Powered-By "Mountain Dew";
- add_header X-XSS-Protection "1; mode=block";
- location / {
+ location = /env.js {
root /usr/share/nginx/html;
- index index.html index.htm;
- try_files $uri $uri/ /index.html =404;
+ }
+
+ location / {
+ proxy_pass http://127.0.0.1:3000;
}
}
\ No newline at end of file