Replies: 9 comments
-
I have the following use case in Next.js app: With hooks this is impossible to do as hooks can only be used inside React components and my environment variable (secret token) would have to be exposed in browser that way, so the only place to do this would be on server (but I can't use hooks there). |
Beta Was this translation helpful? Give feedback.
-
Hello @blistok. Just to understand better. Is this 3rd party API a REST endpoint? Because you can still use the fetch api passing the API key as credentials and retrieve the data. For example:
Hooks are just a nicer way to perform side effects in the UI using react function components and they tend to replace class components. However there is nothing stopping you from using regular promises or libraries that handle side effects in the server side to perform requests to private APIs. |
Beta Was this translation helpful? Give feedback.
-
@theodesp Thank you for your response. Apologies as I should have been clearer in describing my use case. So, these are the steps I want to do:
I'm clear on steps 2 and 3, but I don't know how to do step 1. The only way I see described for fetching data from WP is through hooks, but I can't use hooks inside |
Beta Was this translation helpful? Give feedback.
-
@blistok are you able to use the |
Beta Was this translation helpful? Give feedback.
-
@theodesp It's returning undefined both for that and for Also, any query inside client.client.query.posts().nodes[0].title is undefined. |
Beta Was this translation helpful? Give feedback.
-
Hi @blistok, In terms of fetching data on the server ( As for fetching authenticated data server side: this is currently not possible as our access token fetcher only operates clent side. I'll create a story to log this feature! |
Beta Was this translation helpful? Give feedback.
-
Thank you @blakewilson. For my specific use case I'll use api routes for now, but I think being able to fetch data on the server (both public and authenticated) would be a great feature to have. |
Beta Was this translation helpful? Give feedback.
-
Hey @blistok, Just wanted to let you know we have this feature request tracked in our internal backlog: I'll post here once there is an update 👍 |
Beta Was this translation helpful? Give feedback.
-
Hey @blakewilson , is this feature available today ? |
Beta Was this translation helpful? Give feedback.
-
This is maybe tangentially related to #661 , but more specific question is whether it's possible to fetch data (authenticated and non-authenticated) in places outside of hooks. For example inside
getserverSideProps
orgetStaticProps
?Beta Was this translation helpful? Give feedback.
All reactions