sanity-io / next-sanity

Sanity.io toolkit for Next.js

Home Page:https://www.sanity.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Sanity packages leaking into production?

marcobarbosa opened this issue · comments

When I check the staging of my site I can see some JS packages, related to Sanity, being loaded. I'm not in preview-mode or browsing the studio. Just checking the network tab of the landing page. How can we avoid this to happen?

image

File 1, File 2

Using:

"sanity": "^3.14.0",
"next": "13.4.12",
"next-sanity": "5.4.6",

It's possible to completely avoid this.
Typically this happens because other parts of the codebase might be importing something from a root 'sanity.config.ts' (for example some grabbed the projectId and dataset for a client config from there) or from the sanity package somewhere else in your app and are unaware of how difficult it is for bundlers to properly tree-shake the studio 😅

Depending on your setup the solution is typically to put shared things like env vars in a separate, minimal, file that everything else imports from. And depending on how you possibly embed the studio into Next.js you might need to create a intermediary file that re-exports the Studio component in a way that makes it easy to lazy load it using next/dynamic.

If that's not enough feel free to DM me on our slack and I'd be happy to take a look at your codebase to see what's causing this 🙌

@stipsan Thank you for your insights ⭐

We just found out what was causing this. It works properly now 🤷‍♂️

As for your comments, we are already separating the constants as suggested by the documentation and the Next.js + Sanity templates out there. I think we have a pretty neat separation of concerns so that's why I thought this was confusing.

Thanks a lot for the help!!

Happy I could be of help, and, awesome you figured it out ✨