Help for deployment
Unanswered
Sun bear posted this in #help-forum
Sun bearOP
Hello, so I try do deploy a NextJs app in Vercel, but i have an error.
In the fetches() that I do , I put a "BASE_API_URL" that come from my ".env" like this :
So when I deploy in Vercel, I give all my environnement variable except this "BASE_API_URL", and this because i need the URL of my site to put it in my environnement variable later as the BASE_API_URL.
But even when I do this, it don't work and I really don't know why.
This is my 1st deployment in Vercel, so maybe it is trivial but eventhough I have some difficulties haha.
I really wanna thanks you by advance !
This is the link to my github repo in case : https://github.com/CallMeAl3x/Contact-App
And this is the link to clone the project in case : https://github.com/CallMeAl3x/Contact-App.git
In the fetches() that I do , I put a "BASE_API_URL" that come from my ".env" like this :
So when I deploy in Vercel, I give all my environnement variable except this "BASE_API_URL", and this because i need the URL of my site to put it in my environnement variable later as the BASE_API_URL.
But even when I do this, it don't work and I really don't know why.
This is my 1st deployment in Vercel, so maybe it is trivial but eventhough I have some difficulties haha.
I really wanna thanks you by advance !
This is the link to my github repo in case : https://github.com/CallMeAl3x/Contact-App
And this is the link to clone the project in case : https://github.com/CallMeAl3x/Contact-App.git
124 Replies
Toyger
why do you need this env url at all? your api is inside your site domain, so instead this:
use relative url:
fetch(`${process.env.BASE_API_URL}/api/Users/${id}`) use relative url:
fetch(`/api/Users/${id}`)Sun bearOP
I agree with you, but when I try with your relative url, it doesn't work anymore
@Sun bear I agree with you, but when I try with your relative url, it doesn't work anymore
Toyger
oh it's on server side, then you can use vercel variable:
fetch(`https://${process.env.VERCEL_URL}/api/Users/${id}`)@Toyger oh it's on server side, then you can use vercel variable:
js
fetch(`https://${process.env.VERCEL_URL}/api/Users/${id}`)
Broad-snouted Caiman
I've replaced everything and seems like it dosen't change a thing, same error, when deploying (check the screenshot) maybe it's cause I have a next-auth? I'm kinda out of ideas...
Broad-snouted Caiman
[Error: An error occurred in the Server Components render. The specific message is omitted in production builds to avoid leaking sensitive details. A digest property is included on this error instance which may provide additional details about the nature of the error.] {
digest: '1941507617'
}this is what i get
@Broad-snouted Caiman
[Error: An error occurred in the Server Components render. The specific message is omitted in production builds to avoid leaking sensitive details. A digest property is included on this error instance which may provide additional details about the nature of the error.] {
digest: '1941507617'
}
Toyger
try to run locally with https://vercel.com/docs/cli/dev and check will it show error
Broad-snouted Caiman
This is the log on vercel
@Toyger try to run locally with https://vercel.com/docs/cli/dev and check will it show error
Broad-snouted Caiman
i'll check this out tonight thanks for the advice
@Broad-snouted Caiman Click to see attachment
theres something below it?
@ᴉuɐpɹɐɐ theres something below it?
Broad-snouted Caiman
That's the whole log, the other message are basically that they don't find the "contact" wich I'm fetching
@Broad-snouted Caiman That's the whole log, the other message are basically that they don't find the "contact" wich I'm fetching
Toyger
actually you have syntaxerror with unexpected token on screenshot, it's critical error, what exactly it says?
@Toyger actually you have syntaxerror with unexpected token on screenshot, it's critical error, what exactly it says?
Broad-snouted Caiman
Talking about this ?
@Broad-snouted Caiman Talking about this ?
Toyger
yeah so it shows that it fetch something but result isn't json, maybe url is wrong, you can try to console.log url itself and it probably should show here in logs to understand what wrong
Broad-snouted Caiman
so i added the line console.log, then I have to push on github right ?
Toyger
yeah
Broad-snouted Caiman
alr i'm waiting i'll let you know how it goes
Broad-snouted Caiman
seems like i don't have more logs ):
@Broad-snouted Caiman Click to see attachment
Toyger
are you using it in page.tsx? vercel have some problems with them.
Try to make some basic server component
and then add this component to page. it should create log entry
Try to make some basic server component
"use server";
export default async function SomeComponent() {
console.log(JSON.stringify(YOUR_URL_SRTING_HERE));
return <div>test1</div>;
}and then add this component to page. it should create log entry
Broad-snouted Caiman
like this ? And yeh i added the previous code in a page.jsx
Toyger
yeah
Broad-snouted Caiman
alr i'm pushing
nope, seems like he dosen't want me to have logs xDD
Toyger
hmm, I'll check it out on my deployments
@Broad-snouted Caiman Click to see attachment
Toyger
so couple things, in pages and server components logs are not working, from github issues looks like it's often problem and vercel fix it from time to time.
so where you can make logs is in route handler, console.log,console.warn works perfectly.
but if you want just to output process.env on page you can make next page.tsx
and it should show url from VERCEL_URL
but it's just to check where you send request.
to check request itselft you'll better put it into GET route handler and open route.
and then check logs of what url is, what response is.
Also you can try to run url with postman/insomnia directly to check what it'll show to you
so where you can make logs is in route handler, console.log,console.warn works perfectly.
but if you want just to output process.env on page you can make next page.tsx
export default function Home() {
const url = process.env.VERCEL_URL;
return <div>url: {url}</div>;
}and it should show url from VERCEL_URL
but it's just to check where you send request.
to check request itselft you'll better put it into GET route handler and open route.
and then check logs of what url is, what response is.
Also you can try to run url with postman/insomnia directly to check what it'll show to you
Broad-snouted Caiman
Since i can't display anything i should console.log right?
And it won't work in localhost since it's not in a server
Nvm you said console log doesn't work
Toyger
in route handlers you can also display
and route handlers work at localhost.
console.log will work in route handler
export async function GET(req: Request) {
return new Response("WHATEVER_INFO_TO_DISPLAY", {
status: 200,
});
}and route handlers work at localhost.
console.log will work in route handler
Broad-snouted Caiman
Yeh but i mean since I've put vercel_url in the fetch I will get an error ?
Toyger
you can before fetch run console.log with url itself at least to understand what url to test with postman.
and theoretically
shouldn't return error by itself, until you check result.
and theoretically
const res = await fetch('YOUR_URL',{cache:'no-store'});shouldn't return error by itself, until you check result.
Broad-snouted Caiman
Sorry I'm a noob I'm just trying to understand
Broad-snouted Caiman
I'll try it in like 1h
@Broad-snouted Caiman I'll try it in like 1h
Toyger
ping me when you'll back
Broad-snouted Caiman
Alr will check in 30 mins
Broad-snouted Caiman
So I need to put what exactly in the "your url"?
@Broad-snouted Caiman So I need to put what exactly in the "your url"?
Toyger
first of all immidiately change your repo to private
on github
Broad-snouted Caiman
Hummm, why?
@Broad-snouted Caiman Hummm, why?
Toyger
because your .env file with all access credentials inside repo open for everyone
Broad-snouted Caiman
alr
change in like 10 mins i have something to do rn
@Toyger because your .env file with all access credentials inside repo open for everyone
Broad-snouted Caiman
i'm back, it's now private
@Toyger because your .env file with all access credentials inside repo open for everyone
Broad-snouted Caiman
what's next ? i'm kinda lost
@Toyger are you using it in page.tsx? vercel have some problems with them.
Try to make some basic server component
js
"use server";
export default async function SomeComponent() {
console.log(JSON.stringify(YOUR_URL_SRTING_HERE));
return <div>test1</div>;
}
and then add this component to page. it should create log entry
thats not how you create a server component. dont use "use server" there
So when I deploy in Vercel, I give all my environnement variable except this "BASE_API_URL", and this because i need the URL of my site to put it in my environnement variable later as the BASE_API_URL.try to use "VERCEL_URL" ?? "BASE_API_URL"
eitherway you shouldnt be hitting fetch to your own server @Broad-snouted Caiman
Broad-snouted Caiman
nah i've changed it since he asked me to
@Toyger first of all immidiately change your repo to private
Broad-snouted Caiman
.
@Broad-snouted Caiman .
you just need to put
.env to your gitignoreafter that you can set it to public again
but you need to reset the content of your .env file because it was public at one point
make sure you dont push
.env to the repository by adding it to .gitignoreBroad-snouted Caiman
so I add the line .env in my .gitignore and push with the .env ?
It's confusing
and push the
.gitignorethen delete .env from your GitHub
then change the local .env with something new
like GOOGLE_KEY or AWS_CLIENT_KEY, those better be new
Broad-snouted Caiman
I don't have local.env
i never said local.env
Broad-snouted Caiman
you've said local .env
yeah, with space. I was referring to the
.env on your computer, not on GitHub (delete the one on GitHub first)Broad-snouted Caiman
So if i'm getting it right, i'm deleting .env and I add the line .env in the gitignore and i create a local .env with everything that was in the .env ?
yes a local .env, .env on your computer. Not .env.local
make sure when you push, it doesn't say .env on your tracked changes.
make sure when you commit,
.env is not getting created or changedBroad-snouted Caiman
alr i'll do it step by step
As for the issue, please explain what happened, the code and the context of the code, and the error message. Its a bit scattered all over the place
Broad-snouted Caiman
It's like that ?
I see that you have a fetch related issue.
so please provide
where are you trying to fetch?
whats the code?
where is the code used?
when does the error occur?
what is the log? (try to build locally first and see if theres error, and see it on vercel)
in vercel you can see the log in the "Logs" tab in there, try to send the error logs that are relevant to your action.
If it occurs when you do
so please provide
where are you trying to fetch?
whats the code?
where is the code used?
when does the error occur?
what is the log? (try to build locally first and see if theres error, and see it on vercel)
in vercel you can see the log in the "Logs" tab in there, try to send the error logs that are relevant to your action.
If it occurs when you do
/ then only send logs that appears after you access /@Broad-snouted Caiman It's like that ?
push the .gitignore first (uncheck the .env)
then commit
oh it says deletion
yeah
Broad-snouted Caiman
yeh
so i keep it right?
idk, just make sure the end result is: whenever you change .env, it doesn't get detected by GitHub Desktop.
and it shouldnt be in the remote github repository (on the website)
and it shouldnt be in the remote github repository (on the website)
Broad-snouted Caiman
but i need to delete the .env in my project and push it in order to make it disapear in github repository no ?
i suppose so, i dont know the exact step
Broad-snouted Caiman
alr
let's try
alr, i've pushed it and now there's no .env anymore
let's go back to the original problem then
@ᴉuɐpɹɐɐ I see that you have a fetch related issue.
so please provide
where are you trying to fetch?
whats the code?
where is the code used?
when does the error occur?
what is the log? (try to build locally first and see if theres error, and see it on vercel)
in vercel you can see the log in the "Logs" tab in there, try to send the error logs that are relevant to your action.
If it occurs when you do `/` then only send logs that appears after you access `/`
Broad-snouted Caiman
My website is a contact app, basically you have user that you can create, edit, remove, update i'm using moogo db for the back and nextjs for the api routes, there is a good chance that the problem comes from the fetch, usually in developpement i'm using localhost:3000/ in order to fetch my data (I tried to simply fetch my route for example /api/User/${id} but seems like it's not working so I tried to fetch the website url i'm hosting the website on (wich is Vercel here) and i'm getting a lot of log and particulary one saying "[Error: An error occurred in the Server Components render. The specific message is omitted in production builds to avoid leaking sensitive details. A digest property is included on this error instance which may provide additional details about the nature of the error.] {
digest: '1941507617'
}"
digest: '1941507617'
}"
(works perfectly on developpement and not when i'm trying to deploy it on Vercel)
Would you like me to set the privacy of my repository to public?
npm run buildToyger
I checked your repo, first of all you just should try open in browser http://YOUR_URL/api/Contancts
and check what it shows, it show error in your logs. My guess that it is just get timeout error because you are trying to fetch all contacts from your database, in which you store also base64 encoded images, which is bad.
And it just don't have time to download whole content.
And you need to change you logic, why do you need to fetch all contacts database on main page?
Data should be fetched portionally.
and check what it shows, it show error in your logs. My guess that it is just get timeout error because you are trying to fetch all contacts from your database, in which you store also base64 encoded images, which is bad.
And it just don't have time to download whole content.
And you need to change you logic, why do you need to fetch all contacts database on main page?
Data should be fetched portionally.
@ᴉuɐpɹɐɐ did you build it locally
Broad-snouted Caiman
Nope but seems like it's something else since I'm not getting error when the project is building but afterwards, and yeh I've tried with a fetch in localhost and it works perfect
@Toyger I checked your repo, first of all you just should try open in browser http://YOUR_URL/api/Contancts
and check what it shows, it show error in your logs. My guess that it is just get timeout error because you are trying to fetch all contacts from your database, in which you store also base64 encoded images, which is bad.
And it just don't have time to download whole content.
And you need to change you logic, why do you need to fetch all contacts database on main page?
Data should be fetched portionally.
Broad-snouted Caiman
Alright that's a good hint I'll try this when I'm home
@ᴉuɐpɹɐɐ did you build it locally
Broad-snouted Caiman
All i'm coming back after reviewing my code etc, so basically what I have understand is that i must fetch data with an url since it's a server components, what I don't understand is why it's working in developpement and not when i'm deploying, here is my code for fetching data in a server component :
const getContacts = async () => {
const baseUrl =
process.env.NODE_ENV === "production"
? `https://${process.env.VERCEL_URL}`
: "http://localhost:3000";
try {
const res = await fetch(`${baseUrl}/api/Contacts`, {
cache: "no-store",
});
return res.json();
} catch (err) {
console.log("failed to get ticket");
}
};Toyger
did you tried to open /api/Contacts url directly in browser with your vercel domain?
Broad-snouted Caiman
Alr I'll try after eating
i got another error
So how can I display all my contact without fetching it all in once?
Toyger
yep as you see now you need to solve this, all requests doesn't matter while you can't even fetch it.
question is why do you need all contacts data publicly for everyone on main page? it doesn't make sense.
question is why do you need all contacts data publicly for everyone on main page? it doesn't make sense.
Broad-snouted Caiman
Alright thanks for the help, basically we need in the homepage to display the contact that correspond to the person who's logged in (logged with next-auth). We tried to delete contact so the response is not too big, and the website works perfectly until the db have 14 contacts
so ig we will need to adapt our code
thanks you both @Toyger @ᴉuɐpɹɐɐ
Toyger
for that you need to fetch only single record from db.
and problem that you save users avatars in db, it take a lot of space, ideally you need to offload them to some file storage.
and problem that you save users avatars in db, it take a lot of space, ideally you need to offload them to some file storage.
Broad-snouted Caiman
Do you know some free file storage ?
Toyger
https://supabase.com/ or https://edgestore.dev/pricing probably, they have 1gb file storage free.
Broad-snouted Caiman
is there a way i can optimize the images before sending it to the dabase and so it takes less storage
Toyger
by default no, ideally you should limit filesize for user upload.
probably you can use something like https://github.com/wuwhs/js-image-compressor but not sure will it work with nextjs serverless.
probably you can use something like https://github.com/wuwhs/js-image-compressor but not sure will it work with nextjs serverless.
Broad-snouted Caiman
alr thanks bro you're a lifesaver
@Toyger by default no, ideally you should limit filesize for user upload.
probably you can use something like https://github.com/wuwhs/js-image-compressor but not sure will it work with nextjs serverless.
Broad-snouted Caiman
Do you think that converting every input image to webp using this module would be a possibility ? using a file storage seems more complex
Asian black bear
@Broad-snouted Caiman In general calling your own endpoints from server side code in Vercel is an anti-pattern and should be avoided. At build time during SSR they are not guaranteed to exist even if you do the trick of finding the URL. Also, as you see fetching them as JSON leads to cryptic errors then there is only am HTML 404 at the endpoint. Generally the best strategy in these cases is to encapsulate the API logic into separate files (with
import server-only of course) and use them directly in the server components.It avoids making extra requests for no reason, and allows your API code to see the actual original request instead of the request from yourself (with cookies and stuff)
@Broad-snouted Caiman Do you think that converting every input image to webp using this module would be a possibility ? using a file storage seems more complex
Toyger
maybe, you should try, nextjs itself uses sharp for their image optimizations.
@Broad-snouted Caiman Do you think that converting every input image to webp using this module would be a possibility ? using a file storage seems more complex
Asian black bear
Yes, you could definitely use sharp in a node function to resize/reencode images before shipping them to a database. This is kind of risky to put untrusted user content unless you are making sure to keep sharp 100% updated at all times. It is a binary library with a strong history of serious security problems https://snyk.io/advisor/npm-package/sharp
Another strategy would be to have the submitting browser resize the images and simply limit the filesize on your backend or use
Another strategy would be to have the submitting browser resize the images and simply limit the filesize on your backend or use
image-size@2.0.0-beta.2 to only check the dimensions. On the browser side people sometimes use the canvas element to perform image manipulation and encoding.citation for using canvas to make webp https://stackoverflow.com/questions/74303661/return-canvas-as-webp-image
Sun bearOP
@Asian black bear @Toyger @ᴉuɐpɹɐɐ We really wanted to thanks you for your help, your help was awesome and we learned a lot of things ! We hope the better for you guys