Any good practice for fetching server-side ?
Answered
Vespid wasp posted this in #help-forum
Vespid waspOP
I'm currently building an app where NextJS just fetches data on the server from an external API built in NestJS, is there any file structure good practices that are interesting to learn ? I would like to not put every fetch directly in server components if possible.
Answered by Crème D’Argent
You can check things about server-only here : https://nextjs.org/blog/security-nextjs-server-components-actions#server-only
Data access layer here :
https://nextjs.org/blog/security-nextjs-server-components-actions#data-access-layer
Data access layer here :
https://nextjs.org/blog/security-nextjs-server-components-actions#data-access-layer
4 Replies
Crème D’Argent
Hey ! 👋
Maybe the best solution is to create API services file by feature. If you want to be sure that these calls are made server side only, a good thing could be to put « server-only » directive on the top of the files with server-only package.
Maybe the best solution is to create API services file by feature. If you want to be sure that these calls are made server side only, a good thing could be to put « server-only » directive on the top of the files with server-only package.
Sedge Wren
Just to add to @Crème D’Argent , here is what you can refer https://nextjs.org/blog/security-nextjs-server-components-actions#data-access-layer
@Crème D’Argent Hey ! 👋
Maybe the best solution is to create API services file by feature. If you want to be sure that these calls are made server side only, a good thing could be to put « server-only » directive on the top of the files with server-only package.
Vespid waspOP
Thanks ! Could you provide examples of this way of doing things ? Would love to see how it's done
Crème D’Argent
You can check things about server-only here : https://nextjs.org/blog/security-nextjs-server-components-actions#server-only
Data access layer here :
https://nextjs.org/blog/security-nextjs-server-components-actions#data-access-layer
Data access layer here :
https://nextjs.org/blog/security-nextjs-server-components-actions#data-access-layer
Answer