Access data at request time
Answered
Kanni posted this in #help-forum
KanniOP
I want to figure out how to best access data at request time.
I'm aware that I can use the parameters for the fetch function to determine this eg. { cache: 'force-cache' } but what If I'm not using fetch but accessing data from a db or local files? wont this be exposed on the client aswell or how does nextjs determine to only include this on the server.
I tried the docs and found the react cache function but I'm really not sure if this is what I'm looking for or how to really tell when my code is executed at request time vs build time.
I'm aware that I can use the parameters for the fetch function to determine this eg. { cache: 'force-cache' } but what If I'm not using fetch but accessing data from a db or local files? wont this be exposed on the client aswell or how does nextjs determine to only include this on the server.
I tried the docs and found the react cache function but I'm really not sure if this is what I'm looking for or how to really tell when my code is executed at request time vs build time.
Answered by DirtyCajunRice | AppDir
if you want data to be server side only you need to import "server-only"
7 Replies
if you want data to be server side only you need to import "server-only"
Answer
KanniOP
using server components is not enough?
@Kanni using server components is not enough?
not if you import that into a client component. will expose it. to be 100% sure, you put import "server-only" anywhere something sensitive is referenced, like an api key etc
KanniOP
alright got it, what if I put it alongside my server actions with the 'use server' directive at the top, will that be safe? or bad practice?
I feel like i have my actions figured out but get requests are missing in my picture
KanniOP
alright thank you very much