Next.js Discord

Discord Forum

Will this data be fetched on the client side?

Answered
Transvaal lion posted this in #help-forum
Open in Discord
Transvaal lionOP
async function getData() {
  const res = await fetch('https://api.example.com/...')
  // The return value is *not* serialized
  // You can return Date, Map, Set, etc.
 
  if (!res.ok) {
    // This will activate the closest `error.js` Error Boundary
    throw new Error('Failed to fetch data')
  }
 
  return res.json()
}
 
export default async function Page() {
  const data = await getData()
 
  return <main></main>
}
Answered by riský
no, that is all done on the server
View full answer

9 Replies

no, that is all done on the server
Answer
only the result of render (effectively html elements) will be sent
Transvaal lionOP
How can I fetch the data on the client?
is there a reason why you want that? usaly its better to do server side
Transvaal lionOP
yes I have a page with events and they get updated on runtime
If I server render I need to restart the app each time right now
but effectivly, make a client component and use either setState and useEffect to fetch and render or someting like SWR
@riský but effectivly, make a client component and use either setState and useEffect to fetch and render or someting like SWR
Transvaal lionOP
Ok thanks, I will look into how to do that, appreciate the help