Skip to content

Commit

Permalink
set env variables in index.html
Browse files Browse the repository at this point in the history
  • Loading branch information
daveearley committed Apr 26, 2024
1 parent 9801898 commit 5ebcbf2
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 11 deletions.
1 change: 1 addition & 0 deletions frontend/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
/>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<!--render-helmet--><title>Hi.Events</title><!--/render-helmet-->
<!--environment-variables-->
</head>
<body >
<div id="app"><!--app-html--></div>
Expand Down
18 changes: 14 additions & 4 deletions frontend/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ if (!isProduction) {
app.use(base, sirv("./dist/client", {extensions: []}));
}

const getViteEnvironmentVariables = () => {
const envVars = {};
for (const key in process.env) {
if (key.startsWith('VITE_')) {
envVars[key] = process.env[key];
}
}
return JSON.stringify(envVars);
};

app.use("*", async (req, res) => {
const url = req.originalUrl.replace(base, "");

Expand All @@ -69,12 +79,12 @@ app.use("*", async (req, res) => {
.map((value) => value.toString() || "")
.join(" ");

const envVariablesHtml = `<script>window.hievents = ${getViteEnvironmentVariables()};</script>`;

const html = template
.replace("<!--app-html-->", appHtml)
.replace(
"<!--dehydrated-state-->",
`<script>window.__REHYDRATED_STATE__ = ${strifiedState}</script>`
)
.replace("<!--dehydrated-state-->", `<script>window.__REHYDRATED_STATE__ = ${strifiedState}</script>`)
.replace("<!--environment-variables-->", envVariablesHtml)
.replace(/<!--render-helmet-->.*?<!--\/render-helmet-->/s, helmetHtml);

res.setHeader("Content-Type", "text/html");
Expand Down
7 changes: 6 additions & 1 deletion frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import "@mantine/tiptap/styles.css";
import "@mantine/dropzone/styles.css";
import "@mantine/charts/styles.css";
import "./styles/global.scss";

// @ts-ignore
import {messages as en} from "./locales/en.po";
// @ts-ignore
Expand All @@ -27,6 +26,12 @@ import {messages as pt} from "./locales/pt.po";
import {messages as es} from "./locales/es.po";
import {setAuthToken} from "./utilites/apiClient.ts";

declare global {
interface Window {
hievents: Record<string, string>;
}
}

const supportedLocales: Record<string, any> = {
en,
de,
Expand Down
5 changes: 4 additions & 1 deletion frontend/src/api/client.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import axios from "axios";
import {setAuthToken} from "../utilites/apiClient.ts";
import {isSsr} from "../utilites/helpers.ts";
import {getConfig} from "../utilites/config.ts";

const BASE_URL = isSsr() ? import.meta.env.VITE_API_URL_SERVER : import.meta.env.VITE_API_URL_CLIENT;
const BASE_URL = isSsr()
? getConfig('VITE_API_URL_SERVER')
: getConfig('VITE_API_URL_CLIENT');
const LOGIN_PATH = "/auth/login";
const PREVIOUS_URL_KEY = 'previous_url'; // Key for storing the previous URL

Expand Down
7 changes: 4 additions & 3 deletions frontend/src/api/public-client.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import axios from "axios";
import {isSsr} from "../utilites/helpers.ts";
import {getConfig} from "../utilites/config.ts";

const BASE_URL =
(isSsr()
? import.meta.env.VITE_API_URL_SERVER
: import.meta.env.VITE_API_URL_CLIENT) + '/public';
isSsr()
? getConfig('VITE_API_URL_SERVER')
: getConfig('VITE_API_URL_CLIENT') + '/public';

export const publicApi = axios.create({
baseURL: BASE_URL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {t} from "@lingui/macro";
import {eventHomepagePath} from "../../../../utilites/urlHelper.ts";
import {useGetEventPublic} from "../../../../queries/useGetEventPublic.ts";
import {HomepageInfoMessage} from "../../../common/HomepageInfoMessage";
import {getConfig} from "../../../../utilites/config.ts";

const Payment = () => {
const {eventId, orderShortId} = useParams();
Expand All @@ -31,7 +32,7 @@ const Payment = () => {
stripeAccount: stripeAccount
} : {};

setStripePromise(loadStripe(import.meta.env.VITE_STRIPE_PUBLISHABLE_KEY, options));
setStripePromise(loadStripe(getConfig('VITE_STRIPE_PUBLISHABLE_KEY') as string, options));
}, [stripeData]);

if (stripePaymentIntentError && event) {
Expand Down
5 changes: 5 additions & 0 deletions frontend/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
* @todo - This file needs to be organized better. Split into multiple files.
*/

export type ConfigKeys = 'VITE_FRONTEND_URL'
| 'VITE_API_URL_CLIENT'
| 'VITE_STRIPE_PUBLISHABLE_KEY'
| 'VITE_API_URL_SERVER';

export type IdParam = string | undefined | number;

export interface AcceptInvitationRequest {
Expand Down
9 changes: 9 additions & 0 deletions frontend/src/utilites/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {ConfigKeys} from "../types.ts";

export const getConfig = (key: ConfigKeys): string | undefined => {
if (typeof window === "undefined") {
return import.meta.env[key] as string | undefined;
}

return window.hievents[key];
}
3 changes: 2 additions & 1 deletion frontend/src/utilites/urlHelper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Event, IdParam} from "../types.ts";
import {getConfig} from "./config.ts";

export const eventCheckoutPath = (eventId: IdParam, orderShortId: IdParam, subPage = '') => {
return `/checkout/${eventId}/${orderShortId}/${subPage}`;
Expand All @@ -9,7 +10,7 @@ export const eventHomepagePath = (event: Event) => {
}

export const eventHomepageUrl = (event: Event) => {
return import.meta.env.VITE_FRONTEND_URL + eventHomepagePath(event);
return getConfig('VITE_FRONTEND_URL') + eventHomepagePath(event);
}

export const eventCoverImageUrl = (event: Event) => {
Expand Down

0 comments on commit 5ebcbf2

Please sign in to comment.