frontend web app making requests to wrong endpoint
On my frontend web app I have a env variable configured to be the endpoint of my application but when I inspect the network, it is making a request to itself, project id: 2203d68c-a5c8-4058-b60a-2ecd1a124ad4
64 Replies
Project ID:
N/A
Could you share your vite.config.js and package.json perhaps ? Also wondering: What's the output of a 'console.log(import.meta.env.VITE_API_URL)' for testing purposes? Any differences between localhost/dev and prod hosted on railway?
got it. For furhter documentation you have to create a .env.local and use VITE_ prefix to expose the variable
so you now have a .env.local file within your github-repo ?
yeah
not the best but ok
Well it's up to you of course, but imho you really shouldn't do that. .env-files don't belong into any version-control (like git with github) as they contain almost in all cases sensitive values. You also don't need to do this for using vars in railway
I tried to use env vars in railway but with no success
We can try to get it work if you want 😉
how should I do it then? Ik .env in production is bad but this is just for a test
I assume you use vite right?
yes
So vite uses dotenv under the hood, you can read more about it here https://vitejs.dev/guide/env-and-mode.html#env-files
What imho should always work:
1. For local Deployment create an .env in your projects root-folder (don't forget to add it to your .gitignore file if you haven't yet) and add your vars prefixed with, as you already mentioned, 'VITE'
2. On railway create the same vars within the variable Tab (also prefixed with VITE)
3. Access them within your javascript-code with: 'import.meta.env.VITE_API_URL'
Vite
Next Generation Frontend Tooling
already tried that
i get undefined
can you show me your package.json and vite.config.js ?
i had to create .env.local and add to gitignore
sure ill paste the code
package and vite respectively
this looks ok
I do not know sveltekit but seems there is an extra step with sveltekit, see this https://stackoverflow.com/a/70711383
Stack Overflow
How to load environment variables from .env file using Vite
I want to load environment variables from the .env file using Vite
I used the import.meta.env object as mentioned in Docs
.env file:
TEST_VAR=123F
when trying to access this variable via the impor...
There is an example how you need to tweak your vite.config.js so .env file gets loaded:
// Extends 'process.env.' with VITE_-variables from '.env.(mode=production|development)'
process.env = {...process.env, ...loadEnv(mode, process.cwd())};
Once done that, vars from .env should be loaded 🙂
let me try
how do I solve this?
I tried to put any before mode but it didnt work
are you building with a dockerfile?
yes
send it please
whats your start command set to in the service settings
i dont get it
Go into the settings of your service on railway, and scroll down to the section "deploy", there you will see a row called "Start Command"
i dont have a start command
okay lol
delete the dockerfile please
delete and commit?
delete the file locally and from the repo
can i ask why? cuz from what i get it uses dockerfile to build the app right?
yes but its doing more harm then good, we will be using nixpacks going forward
i need to run it with docker
i get bonus points
it is for a test
can we keep it?
it is doing more harm than good
railway runs with docker regardless
sure then. Deleted it
now what kind of vite app is this? react, vue? what we talking?
svelte, close enough
sveltekit
copy the nixpack.toml and caddyfiles fropm this repo into yours
https://github.com/brody192/vue-3-template
in the meantime: Brody is def. built different🏗️
i know thats vue and not sveltekit, but it should be universal to most vite csr spa apps
done
push the changes and lets see what happens
also im getting this while trying to connect to my backend on railway, it looks like cors
okay but does that mean the frontend is working?
it is working with the .env.load solution provided above
not ideal but it is working
im runninb now with caddy
lets see if it works
failed
remove the .env.local from the repo and locally
sent the frontend domain and the backend domain please
https://gabrielhk97-todolist-frontend.up.railway.app/
https://gabrielhk97-todolist-backend.up.railway.app/
still failed after deleting .env.local
show me the frontend's service variable for the url backend thing please
it used to be on the .env.local
now it is just on the variables in railway as VITE_API_URL = gabrielhk97-todolist-backend.up.railway.app
isn't the protocol missing (https)? or do you add this in your code manually?
but anyway it shouldn't output undefined if the vars would be loading, so that won't be the root cause here
i know the cause
show me a screenshot of your entire project
wdym
a screenshot
of your
railway project
?
yep
one sec
in your frontend service variables set
VITE_API_URL
to https://${{ todolist-backend.RAILWAY_PUBLIC_DOMAIN }}
got it working
thanks
how do i close?
was that the variable that worked?
no, I had to rollback all the way to using .env.local
ugly solution, but hey it works
what didn't work about the variable I gave you
the project was not deploying
i was getting failure
please add the variable back and send me the error