#Blocked on nested routes
1 messages · Page 1 of 1 (latest)
I tried to implement the ideas described above and ended up with a folder structure like this:
pages
accounts
index.tsx
[accountId]
holdings.tsx
orders.tsx
Note that I have not implemented the order-details path yet, but does this look right so far?
The only concern I have is that the deeper I go, the pages are repeating the code from the shallower pages. For example,
/account/index.tsxfetches accounts and renders them in the sidebar./account/[accountId]/holdings.tsxalso fetches accounts and renders them in the sidebar (in additon to fetching holdings and rendering them).
Is there a better way?
you can use client-side perPage layout, the layout loads the data, you simply need to set it up for each page. Did you try that already?
I know this part of Next is confusing, but remember that an user can reach each page directly via URL, so they kinda behave as their own, independant website (that's what make Next.js fast). The price to pay is that you need to configure the layout for each page currently, but you can still have 99% of the code reused using either client-side layout, or a reusable helper to implement your getServerSideProps
Is this related to this question?
https://stackoverflow.com/questions/70211088/next-js-shows-index-page-content-on-refresh-dynamic-page-url
Thanks for your response @candid wagon. I haven't tried layouts yet, will try it. I don't see any examples of layouts loading data, so the question is if they can have getServerSideProps() of their own. If yes, I can implement the sidebar as a layout, otherwise I will have to make it a regular react component and pass data to it.
@slender parcel, I looked at the SO question. I don't think it is related to what I am asking.
@candid wagon, also reading the docs, I am not seeing the benefits of per page layout (https://nextjs.org/docs/basic-features/layouts#per-page-layouts). What does the getLayout() function buy me? I might as well add the layout to the page markup directly! I must be missing something 🤔
The layout won't remount on page change => won't trigger a request everytime + you put duplicate code there
check the very last section for data fetching: https://nextjs.org/docs/basic-features/layouts#data-fetching
You'd want to check this excellent read, dating from before layouts were introduced in Next.js: https://adamwathan.me/2019/10/17/persistent-layout-patterns-in-nextjs/
the "persistent" part is the key here, that's the difference with just adding the layout to page markup
Thank you again for this reference, @candid wagon. The Next docs do not describe what "persistence" means in this context. I was thinking persistence as in component state persistence!!