Skip to content

Commit 08f15f3

Browse files
authored
Merge pull request #3 from monkeyjp/main
feat: add BackendURL component and improve env variable handling
2 parents 66bdd8d + 68d5095 commit 08f15f3

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

.env.example

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# This file includes global variables that will be available inside your project
2-
# 1. In the front end code you can access this variables like this: console.log(process.env.VARIABLE_NAME)
2+
# 1. In the front end code you can access this variables like this: console.log(import.meta.env.VARIABLE_NAME)
33
# 1. In the back end code you access the variable by importing os and then typing print(os.getenv('VARIABLE_NAME'))
44

55
# Back-End Variables
@@ -10,5 +10,5 @@ FLASK_DEBUG=1
1010
DEBUG=TRUE
1111

1212
# Front-End Variables
13-
BASENAME=/
14-
#BACKEND_URL=
13+
VITE_BASENAME=/
14+
#VITE_BACKEND_URL=

render.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ services:
1111
plan: free # optional; defaults to starter
1212
numInstances: 1
1313
envVars:
14-
- key: BASENAME # Imported from Heroku app
14+
- key: VITE_BASENAME # Imported from Heroku app
1515
value: /
1616
- key: FLASK_APP # Imported from Heroku app
1717
value: src/app.py

src/front/components/BackendURL.jsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import React, { Component } from "react";
2+
import envFile from "../../../docs/assets/env-file.png"
3+
4+
const Dark = ({children}) => <span className="bg-dark text-white px-1 rounded">{children}</span>;
5+
export const BackendURL = () => (
6+
<div className="mt-5 pt-5 w-50 mx-auto">
7+
<h2>Missing BACKEND_URL env variable</h2>
8+
<p>Here's a video tutorial on <a target="_blank" href="https://www.awesomescreenshot.com/video/16498567?key=72dbf905fe4fa6d3224783d02a8b1b9c">how to update your backend URL environment variable.</a></p>
9+
<p>There's a file called <Dark>.env</Dark> that contains the environmental variables for your project.</p>
10+
<p>There's one variable called <Dark>BACKEND_URL</Dark> that needs to be manually set by yourself.</p>
11+
<ol>
12+
<li>Make sure you backend is running on port 3001.</li>
13+
<li>Open your API and copy the API host.</li>
14+
<li>Open the .env file (do not open the .env.example)</li>
15+
<li>Add a new variable BACKEND_URL=<Dark>your api host</Dark></li>
16+
<li>Replace <Dark>your api host</Dark> with the public API URL of your flask backend sever running at port 3001</li>
17+
</ol>
18+
<img src={envFile} />
19+
<p>Note: If you are publishing your website to Heroku, Render.com or any other hosting you probably need to follow other steps.</p>
20+
</div>
21+
);

src/front/main.jsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,16 @@ import './index.css' // Global styles for your application
44
import { RouterProvider } from "react-router-dom"; // Import RouterProvider to use the router
55
import { router } from "./routes"; // Import the router configuration
66
import { StoreProvider } from './hooks/useGlobalReducer'; // Import the StoreProvider for global state management
7+
import { BackendURL } from './components/BackendURL';
78

89
const Main = () => {
10+
console.log(import.meta.env.VITE_BACKEND_URL);
11+
12+
if(! import.meta.env.VITE_BACKEND_URL || import.meta.env.VITE_BACKEND_URL == "") return (
13+
<React.StrictMode>
14+
<BackendURL/ >
15+
</React.StrictMode>
16+
);
917
return (
1018
<React.StrictMode>
1119
{/* Provide global state to all components */}

0 commit comments

Comments
 (0)