fetch not firing
Answered
Brouillard posted this in #help-forum
I don't understand why the fetch function is not fired at all, I checked in network on my browser inspector:
import React from 'react'
import { getServerSession } from 'next-auth/next'
import { redirect } from 'next/navigation'
import React from 'react'
import { getServerSession } from 'next-auth/next'
import { redirect } from 'next/navigation'
const AllEvents = async () => {
const session = await getServerSession()
if (!session?.user?.email) redirect("/login")
const allTheEvents = await fetch(`${process.env.NEXTAUTH_URL}/api/events`, {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
})
return (
<div>{ JSON.stringify(allTheEvents) }</div>
)
}
export default AllEventsAnswered by riský
that looks like server component, and the fetch is done server side
3 Replies
@Brouillard I don't understand why the fetch function is not fired at all, I checked in network on my browser inspector:
import React from 'react'
import { getServerSession } from 'next-auth/next'
import { redirect } from 'next/navigation'
ts
const AllEvents = async () => {
const session = await getServerSession()
if (!session?.user?.email) redirect("/login")
const allTheEvents = await fetch(`${process.env.NEXTAUTH_URL}/api/events`, {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
})
return (
<div>{ JSON.stringify(allTheEvents) }</div>
)
}
export default AllEvents
that looks like server component, and the fetch is done server side
Answer
oh yeah you are right, sorry for the beginner mistake
@Brouillard oh yeah you are right, sorry for the beginner mistake
no worries, i too had a fun time learning about app dir