Next.js Discord

Discord Forum

Question regarding how Next.js projects are built

Answered
Selkirk Rex posted this in #help-forum
Open in Discord
Selkirk RexOP
If I have a file in /app/utils/client.js which uses a SECRET client that uses secret keys that should not be exposed to the client, and I use that client in Layout ran on the server, will this client.js file also be compiled into the client JS bundle? Or are files only included in client bundle if they're actually used on the client.

Basically, just because I have a function in my /app/utils/client.js, it doesn't mean it will be included in the client bundle and exposed to the client right? Unless I use it a client environment
Answered by Dwarf Crocodile
If the Layout doesn't have a "use client" then it should be fine. If you want to be really sure you can import the server-only package in your client.js which will give a compile-time error if it was imported in a client component.

https://www.npmjs.com/package/server-only

import "server-only"
View full answer

3 Replies

Dwarf Crocodile
If the Layout doesn't have a "use client" then it should be fine. If you want to be really sure you can import the server-only package in your client.js which will give a compile-time error if it was imported in a client component.

https://www.npmjs.com/package/server-only

import "server-only"
Answer
Dwarf Crocodile
np