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!

set up env vars

Blue Mouse8d6613631a0e
diff --git a/.env.example b/.env.example
index 92d03e124..6ae813e14 100644
--- a/.env.example
+++ b/.env.example
@@ -1,9 +1,9 @@
-REACT_APP_API_URL=https://api.swetrix.com/
-REACT_APP_API_STAGING_URL=http://localhost:5005/
+API_URL=https://api.swetrix.com/
+API_STAGING_URL=http://localhost:5005/
-REACT_APP_CDN_URL=https://cdn.swetrix.com/
-REACT_APP_BLOG_URL=https://blog.swetrix.com/
-REACT_APP_AIAPI_URL=https://aiapi.swetrix.com/
+CDN_URL=https://cdn.swetrix.com/
+BLOG_URL=https://blog.swetrix.com/
+AIAPI_URL=https://aiapi.swetrix.com/
CHOKIDAR_USEPOLLING=true
GENERATE_SOURCEMAP=false
\ No newline at end of file
diff --git a/app/api/ai.ts b/app/api/ai.ts
index 21666f477..ac54b777c 100644
--- a/app/api/ai.ts
+++ b/app/api/ai.ts
@@ -2,11 +2,12 @@
import axios from 'axios'
import Debug from 'debug'
import _isEmpty from 'lodash/isEmpty'
+import { AIAPI_URL } from 'redux/constants'
const debug = Debug('swetrix:ai')
const api = axios.create({
- baseURL: 'https://aiapi.swetrix.com/', // process.env.REACT_APP_AIAPI_URL,
+ baseURL: AIAPI_URL,
})
/**
diff --git a/app/api/blog.ts b/app/api/blog.ts
index faac07bdf..93976111c 100644
--- a/app/api/blog.ts
+++ b/app/api/blog.ts
@@ -2,11 +2,12 @@
import axios from 'axios'
import Debug from 'debug'
import _isEmpty from 'lodash/isEmpty'
+import { BLOG_URL } from 'redux/constants'
const debug = Debug('swetrix:blog')
const api = axios.create({
- baseURL: 'https://blog.swetrix.com/', // process.env.REACT_APP_BLOG_URL,
+ baseURL: BLOG_URL,
})
export const getLastPost = () =>
diff --git a/app/api/index.ts b/app/api/index.ts
index 64db765d6..ba4275660 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 } from 'redux/constants'
+import { DEFAULT_ALERTS_TAKE, isSelfhosted, 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,15 +21,10 @@ import { ISubscribers } from 'redux/models/ISubscribers'
const debug = Debug('swetrix:api')
-const envApiURL = process.env.REACT_APP_RUNNING_DEV_SCRIPT === 'true'
- ? process.env.REACT_APP_API_STAGING_URL
- : process.env.REACT_APP_API_URL
-
-// @ts-ignore
-const baseURL: string = isSelfhosted ? window.env.API_URL : envApiURL
+const baseURL: string = isSelfhosted ? window.env.API_URL : API_URL
const api = axios.create({
- baseURL: 'https://api.swetrix.com/',
+ baseURL,
})
// Function that will be called to refresh authorization
diff --git a/app/pages/MainPage/index.tsx b/app/pages/MainPage/index.tsx
index bcb99329f..fe3407f56 100644
--- a/app/pages/MainPage/index.tsx
+++ b/app/pages/MainPage/index.tsx
@@ -17,7 +17,7 @@ import _isEmpty from 'lodash/isEmpty'
import routesPath from 'routesPath'
import { nFormatterSeparated } from 'utils/generic'
import {
- GITHUB_URL, MARKETPLACE_URL, LIVE_DEMO_URL, isBrowser,
+ GITHUB_URL, MARKETPLACE_URL, LIVE_DEMO_URL, isBrowser, BLOG_URL,
} from 'redux/constants'
import { StateType } from 'redux/store/index'
import BackgroundSvg from 'ui/icons/BackgroundSvg'
@@ -236,7 +236,7 @@ const Main: React.FC<IMain> = ({ ssrTheme }): JSX.Element => {
) : (
<a
className='inline-flex ml-1 items-center space-x-1 text-sm font-semibold leading-6 text-slate-700 dark:text-slate-300 hover:underline'
- href={`${process.env.REACT_APP_BLOG_URL}post/${lastBlogPost.url_path}`}
+ href={`${BLOG_URL}post/${lastBlogPost.url_path}`}
target='_blank'
rel='noopener noreferrer'
>
diff --git a/app/redux/constants/index.ts b/app/redux/constants/index.ts
index 58f54fbd1..6263b7ea7 100644
--- a/app/redux/constants/index.ts
+++ b/app/redux/constants/index.ts
@@ -197,7 +197,6 @@ export const TWITTER_URL: string = 'https://twitter.com/intent/user?screen_name=
export const TWITTER_USERNAME: string = '@swetrix'
export const DISCORD_URL: string = 'https://discord.gg/tVxGxU3s4B'
export const STATUSPAGE_URL: string = 'https://stats.uptimerobot.com/33rvmiXXEz'
-export const BLOG_URL: string = 'https://blog.swetrix.com'
export const MAIN_URL: string = 'https://swetrix.com'
export const UTM_GENERATOR_URL: string = 'https://url.swetrix.com'
export const LIVE_DEMO_URL: string = '/projects/STEzHcB1rALV'
@@ -205,15 +204,12 @@ export const MARKETPLACE_URL: string = 'https://marketplace.swetrix.com'
export const DOCS_URL: string = 'https://docs.swetrix.com'
export const CAPTCHA_URL: string = 'https://captcha.swetrix.com'
export const DOCS_CAPTCHA_URL: string = `${DOCS_URL}/captcha/introduction`
-export const CDN_URL: string | undefined = process.env.REACT_APP_CDN_URL || 'https://cdn.swetrix.com/'
// Swetrix vs ...
export const SWETRIX_VS_GOOGLE: string = 'https://blog.swetrix.com/post/vs-google-analytics/'
export const SWETRIX_VS_CLOUDFLARE: string = 'https://blog.swetrix.com/post/vs-cloudflare-analytics/'
export const SWETRIX_VS_SIMPLE_ANALYTICS: string = 'https://blog.swetrix.com/post/vs-simple-analytics/'
-export const isDevelopment: boolean = !process.env.NODE_ENV || process.env.NODE_ENV === 'development'
-
export const isBrowser = typeof window !== 'undefined' && typeof document !== 'undefined'
export const SUPPORTED_THEMES: string[] = ['light', 'dark']
@@ -221,10 +217,25 @@ export const SUPPORTED_THEMES: string[] = ['light', 'dark']
export const CONTACT_EMAIL: string = '[email protected]'
export const SECURITY_EMAIL: string = '[email protected]'
-export const isSelfhosted: boolean = Boolean(process.env.REACT_APP_SELFHOSTED)
export const LIVE_VISITORS_UPDATE_INTERVAL: number = 40000
+// Environment variables
+const isStaging = isBrowser ? window.REMIX_ENV.STAGING : process.env.STAGING
+const STAGING_API_URL = isBrowser ? window.REMIX_ENV.API_STAGING_URL : process.env.API_STAGING_URL
+const PRODUCTION_API_URL = isBrowser ? window.REMIX_ENV.API_URL : process.env.API_URL
+
+export const API_URL = isStaging ? STAGING_API_URL : PRODUCTION_API_URL
+export const BLOG_URL = isBrowser ? window.REMIX_ENV.BLOG_URL : process.env.BLOG_URL
+export const AIAPI_URL = isBrowser ? window.REMIX_ENV.AIAPI_URL : process.env.AIAPI_URL
+export const CDN_URL = isBrowser ? window.REMIX_ENV.CDN_URL : process.env.CDN_URL
+export const NODE_ENV = isBrowser ? window.REMIX_ENV.NODE_ENV : process.env.NODE_ENV
+
+export const isDevelopment = !NODE_ENV || NODE_ENV === 'development'
+export const isSelfhosted = Boolean(
+ isBrowser ? window.REMIX_ENV.SELFHOSTED : process.env.SELFHOSTED
+)
+
// Functions
export const getProjectCacheKey = (period: string, timeBucket: string, filters?: any): string => `${period}${timeBucket}${filters ? JSON.stringify(filters) : ''}}`
export const getProjectCaptchaCacheKey = (period: string, timeBucket: string, filters?: any): string => `${period}${timeBucket}captcha${filters ? JSON.stringify(filters) : ''}}`
diff --git a/app/root.tsx b/app/root.tsx
index 2bbabc04f..36742ef18 100644
--- a/app/root.tsx
+++ b/app/root.tsx
@@ -37,6 +37,14 @@ import FlatpickerLibaryCss from 'flatpickr/dist/themes/dark.css'
trackViews()
+declare global {
+ interface Window {
+ REMIX_ENV: any
+ // Set by Docker for self-hosted
+ env: any
+ }
+}
+
if (isBrowser) {
console.log('%cWelcome, hacker, glad you opened your console, you seem serious about your craft and will go a long way!\nP.S. All the bugs, feature requests are welcome to be sent to [email protected]', 'color: #818cf8background: #1f2937font-size: 20pxtext-shadow: 2px 2px black')
}
@@ -81,7 +89,20 @@ export async function loader({ request }: LoaderArgs) {
const locale = detectLanguage(request)
const theme = detectTheme(request)
- return json({ locale, url, theme })
+ const REMIX_ENV = {
+ NODE_ENV: process.env.NODE_ENV,
+ AIAPI_URL: process.env.AIAPI_URL,
+ API_URL: process.env.API_URL,
+ 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,
+ STAGING: process.env.STAGING,
+ }
+
+ return json({
+ locale, url, theme, REMIX_ENV,
+ })
}
export const handle = {
@@ -90,7 +111,7 @@ export const handle = {
export default function App() {
const {
- locale, url, theme,
+ locale, url, theme, REMIX_ENV,
} = useLoaderData<typeof loader>()
const { i18n, t } = useTranslation('common')
const { title } = getPageMeta(t, url)
@@ -115,6 +136,13 @@ export default function App() {
<link key={link.hrefLang} {...link} />
))}
<link rel='preload' href={`/locales/${locale}.json`} as='fetch' type='application/json' crossOrigin='anonymous' />
+ <script
+ dangerouslySetInnerHTML={{
+ __html: `window.REMIX_ENV = ${JSON.stringify(
+ REMIX_ENV
+ )}`,
+ }}
+ />
</head>
<body>
<div className='loader' id='loader'>