Lars.
Server side pages without server directory for api calls
From a
.vue
page I use this api from a method in a pinia store and it runs server side:
I'm using unsecureFetch
(based on ofetch
) because $fetch
uses a default Agent that can't be overridden to ignore a certificate. However, this isn't the core issue here but it shows that Pinia methods can run server side. The problem seems to lie in how useAsyncData
is being used. Hope this helps a bit.9 replies
Private API to fetch data from server on page load or navigation without exposing api to the client
Using
useAsyncData
with server: true
, the data fetching will only happen during the initial page load on the server. When the user navigates away from the page and then returns, the data will not be re-fetched from the server. That seems expected behavior of Nuxt.
I think you have to wrap () => $fetch("external api")
in an api in ~/server/routes
to make it private. API routes are for exposing data to the client, while server routes are useful for internal server-side logic.
And then manually trigger a re-fetch of the data using the refresh or execute methods provided by useAsyncData
:
Let me know what you think because I'm not totally sure.3 replies