runtimeConfig not working when website deployed to netlify
I have a server route called
api/youtube-video
.env
nuxt.config.ts
This is working fine on local.
But, when i deploy the website to netlify. The api doesn't return any data..
https://www.baljeetsingh.in/videos
I have added YOUTUBE_API_KEY
in the netlify environment variables too.
am i missing something?17 Replies
Have you tried
NUXT_YOUTUBE_API_KEY
?do i have to prefix all env variables in netlify with
NUXT_
Just tried, it didn't work
I also tried NUXT_ENV, still no (Looks like it was requirement for nuxt 2)Then I don't know, sorry.
Thanks, np
Alexander Lichter
YouTube
Nuxt's runtimeConfig - The most common mistake
š¤Æ Throughout my projects, consultancies and code reviews I came across lots of interesting findings - also with regards to Nuxt's runtimeConfig feature. Repeatedly I noticed ONE big mistake though which you might do at this very moment in your project. In this video, explain what it is, why you shouldn't do it and how to use runtimeConfig correc...
Hi @manniL / TheAlexLichter
I watched the video and updated the environement variable to
NUXT_YOUTUBE_API_KEY
on netlify.
This looks to be working.
Thanks
Also, I have set the video route to prerender, but it seems to be hitting the api again and again.
Am i doing something wrong?
I just want to prerender everything if possible.
On local I did npm run generate
and then used serve on .output/public
directory.
But, when i use this build command and directory on netlify, the build fails.
Currently, it deploy is only working with npm run build
and dist
. Then, I'm not sure which routes are prerendered.Currently, it deploy is only working with npm run build and dist . Then, I'm not sure which routes are prerendered.It should tell you in the build log
the log says it is prerendered
But, as it is calling the server route, something feels wrong there
So, setting all the routes to
prerender: true
is the correct way?
Don't we need to use npm run generate
on the server?
btw, I had previously added useYoutubeVideos
composable and called the youtube api from there. I think then it wasn't calling the api (not certain).
But, it felt like creating server route is the right way to hide the key.Let me ask a different question: How do you fetch the YouTube videos?
only useState, useAsyncData and useFetch are actually "persisted" during prerendering
Using server route and youtube api
how do you call that in your frontend I mean @Baljeet Singh
<script setup>
const videos = ref([]);
onMounted(async () => {
const { items } = await $fetch('/api/youtube-videos');
videos.value = items;
});
</script>
For nuxt content I'm using useAsyncData. I assume this is why it pretenders.
<script setup lang="ts">
const { data: courses } = await useAsyncData('courses', () =>
queryContent('/courses')
.without('body')
.where({ published: { $eq: true } })
.sort({ publishedAt: -1 })
.find()
);
</script>
I'll try to replace $fetch in videos too
š
yeah, the
$fetch
call will always happen (on client side)
that's the problem šThanks @manniL / TheAlexLichter
it worked š
I have one final issue, hope you don't mind, haha
I have now set the blog routes to prerender
It works well in general. There is a minor issue with the pagination. I'm using query string params. It works well if i don't prerender routes. But, doesn't work as expected when i prerender (works on local though).
If i just use the prev and next buttons it is ok.
But, If i'm on a route, let's say
https://www.baljeetsingh.in/blog?page=2
and then i try to refresh. It shows the content of the first page always. I assume this is the expected behavior with prerender.
Do you have any insights on how you would go about solving this issue.because query params don't work with prerendering š