next-sanity icon indicating copy to clipboard operation
next-sanity copied to clipboard

Optimizing Bundle Size

Open bline opened this issue 1 year ago • 2 comments

I'm currently trying to optimize my bundle size and this module has become a sticking point. I've follow the instructions in the README but @sanity/client is still being included in the bundle.

If I remove the import import { createPreviewSubscriptionHook } from "next-sanity"; from my sanity.ts and add

export const usePreviewSubscription = (query: string, options: PreviewOptions) => { //createPreviewSubscriptionHook(config);
  return {
    data: options.initialData,
  };
}

in place of export const usePreviewSubscription = createPreviewSubscriptionHook(config);

I no longer see @sanity/client included in the bundle.

I'm testing this with nextjs-12.2.3-canary.10 using webpack5 configuration, unmodified from the default and @next/bundle-analyzer-12.1.6

bline avatar Jul 20 '22 18:07 bline

I am also experiencing this.

llibdude avatar Aug 09 '22 18:08 llibdude

Hi, we're working on a new version of preview mode that is easier to keep out of your bundle until it's actually needed. It's an early version so we haven't added the code-splitting we want to yet (it loads a hefty eventsource polyfill that's really only needed if Safari fails to use cookies with the groq subscription).

Feel free to share your thoughts on what the API should look like etc 🙂

stipsan avatar Aug 12 '22 17:08 stipsan

We've just released v2.0.0 which encourages code-splitting out of the box by using React.lazy. You shouldn't see @sanity/client in your bundle anymore, unless it's actually used 😌

stipsan avatar Nov 16 '22 16:11 stipsan