Docs
Launch GraphOS Studio

Reducing bundle size

Squeeze those last few bytes out of your production build


Two quick configuration changes can help you reduce your bundle size: turning off 's development mode and picking a consistent style for your imports. Every byte counts in optimizing your app's performance.

Turning off development mode

By default, Apollo Client is in "development mode". That means Apollo Client performs additional checks and the browser console displays warnings. You can disable development mode by setting globalThis.__DEV__ to false. Doing so turns off those checks, but they will still end up in your production bundle—you have to tell your bundler to eliminate those checks at build time!

You can expect eliminating checks to result in a reduction of 1-3 KB in your production bundle.

Bundler-specific configuration

Here are some bundler-specific suggestions for configuring your bundler to remove globalThis.__DEV__ on build time.

Vite

Next.js

create-react-app

With create-react-app, you need to use a third-party package like

to modify the bundler configuration.

esbuild

Webpack

Rollup

SWC

Why use globalThis.__DEV__ instead of process.env.NODE_ENV?

Apollo Client uses the __DEV__ instead of process.env.NODE_ENV because the latter is not available in non-Node.js environments. Apollo Client can be used from a CDN with an ESM import. Usage of the process variable within the library in this scenario would cause a crash in your application.

It's impossible for Apollo Client to use a guard statement like typeof process !== "undefined" && process.env.NODE_ENV !== 'production' because some bundlers, such as esbuild, cannot replace a statement like typeof process at build time. The development-only code would remain after the build, resulting in no bundle size reduction.

In contrast, checking for globalThis.__DEV__ does not crash the browser and can be eliminated by every bundler.

Importing Apollo Client

Depending on your bundler's capabilities and settings, you can use multiple styles of importing from Apollo Client.

Picking an import style

Apollo Client offers these two styles of imports:

// "main entrypoint import" style
import { ApolloClient, InMemoryCache, useQuery } from "@apollo/client";
// "deep entrypoint import" style
import { ApolloClient, InMemoryCache } from "@apollo/client/core";
import { useQuery } from "@apollo/client/react/hooks";

With many modern bundlers, it should not matter which of these styles you choose.
It is important to keep in mind though that bundlers are complex and it might make a difference - especially when your bundler picks up the CommonJS artifacts instead of the ESM artifacts.
Every bundling setup is different and we cannot guarantee which style results in the smallest bundle size. We recommend trying out these styles in a small setup to determine which results in the best outcome in your environment.

Note: some entry points are not part of the "main" entrypoint '@apollo/client' and can only be imported directly (e.g. from '@apollo/client/link/batch'). It's perfectly fine to use these, even when using the "main" entrypoint.

Previous
Mocking schema capabilities
Next
Improving performance
Edit on GitHubEditForumsDiscord

© 2024 Apollo Graph Inc.

Privacy Policy

Company