Next.js Discord

Discord Forum

Difference between app and page dir

Answered
Spectacled bear posted this in #help-forum
Open in Discord
Spectacled bearOP
What is major difference between app and page directory. Anyone explain me.

68 Replies

Answer
@@ts-ignore https://nextjs.org/docs/pages/building-your-application/upgrading/app-router-migration#migrating-from-pages-to-app
Spectacled bearOP
One more thing is it possible to use nextauth in app dir
@Spectacled bear One more thing is it possible to use nextauth in app dir
idk about next auth but authjs is possible ig
@@ts-ignore idk about next auth but authjs is possible ig
Spectacled bearOP
can we make 1 layouts and use it for all page in app diflr?
just create that layout at app/layout.tsx and it will be reused througout the app
Spectacled bearOP
Of index.tsx not work instead of page.tsx
@@ts-ignore yes
Spectacled bearOP
One last why we need to make
app/shop/cart.tsx
We don't do app/cart.tsx??
you need to make app/cart/page.tsx if you need a route at /cart
that's because those folders can have so many files like loading.tsx, layout.tsx
@@ts-ignore you need to make `app/cart/page.tsx` if you need a route at `/cart`
Spectacled bearOP
Oh like this
app
Index.tsx
(shop)
cart.tsx
list.tsx
Spectacled bearOP
(shop) = group???
app
  page.tsx
  (shop)
      page.tsx
      list
        page.tsx
Spectacled bearOP
For https://domain.example we just need to do app/index.tsx??
app dir removed the concept of index.tsx
Spectacled bearOP
Only page.tsx???
@Spectacled bear Only page.tsx???
to create a route yes
Spectacled bearOP
We can't make <cutom name>.tsx
Spectacled bearOP
Oh so like for every app/page

We need to make dir for it
Spectacled bearOP
Like I have 2 page like premium and status how can we get it in domain/here
@Spectacled bear Like I have 2 page like premium and status how can we get it in domain/here
app/premium/page.tsx and app/status/page.tsx then these will be accessible at domain.com/premium and domain.com/status
Spectacled bearOP
Okay thanks
All confusion solve
paras?
Spectacled bearOP
app/status/[Id]/list
Spectacled bearOP
Is getserverprops work I'm app directory
Spectacled bearOP
Then,m
everything is in docs bruh
Spectacled bearOP
Hmmk
Can't find it
This is issue
@Spectacled bear Can't find it
if your problem is resolved, mark this as solved
Spectacled bearOP
I don't know name of new functions
@@ts-ignore if your problem is resolved, mark this as solved
Spectacled bearOP
Sure
@@ts-ignore if your problem is resolved, mark this as solved
Spectacled bearOP
Is app dir fetch api from server side?
@Spectacled bear Is app dir fetch api from server side?
yes, you fetch data serverside and then use it in the same page or pass it down as a prop to other components
@@ts-ignore sorry what?
Spectacled bearOP
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>
}
read the docs I sent you above
they have everything
@@ts-ignore they have everything
Spectacled bearOP
Is that function work like this
export default async function Page() {
  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()
}
 
  return <main></main>
}
Is this work under export page
In server side
Or it work for client side
@@ts-ignore just a quick question. Do you know js?
Spectacled bearOP
Yes
I am cs student
Basically I use vue
First time nextjs
@Spectacled bear Yes
the code in the docs and the code you sent are the same, it doesn't matter where you define the function. As long as it is correct it will work
@Spectacled bear First time nextjs
then go through React First
you can't just jump directly to next without react
@Spectacled bear I am cs student
Being a CS student doesn't mean you know JS until you learn and use it
Spectacled bearOP
Okay
Thanks