Reducing bundle size

Squeeze those last few bytes out of your production build


Every byte counts in optimizing your app's performance. This page covers some configuration changes that can help reduce Apollo Client's bundle size:

  • Turn off Apollo Client's development mode

  • Pick a consistent style for your imports

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

Click to expand suggested configuration
JavaScript
vite.config.js
1export default defineConfig({
2  // ...
3  define: {
4    "globalThis.__DEV__": JSON.stringify(false),
5  },
6});

Next.js

Click to expand suggested configuration
JavaScript
next.config.js
1// ...
2/** @type {import('next').NextConfig} */
3const nextConfig = {
4  webpack(config, { webpack }) {
5    config.plugins.push(
6      new webpack.DefinePlugin({
7        "globalThis.__DEV__": false,
8      })
9    );
10    return config;
11  },
12};
13
14module.exports = nextConfig;

create-react-app

With create-react-app, you need to use a third-party package like craco to modify the bundler configuration.

Click to expand suggested configuration
JavaScript
craco.config.js
1const webpack = require("webpack");
2module.exports = {
3  webpack: {
4    plugins: [
5      new webpack.DefinePlugin({
6        "globalThis.__DEV__": false,
7      }),
8    ],
9  },
10};

esbuild

Click to expand suggested configuration
JSON
1{
2  "define": {
3    "globalThis.__DEV__": "false"
4  }
5}

Webpack

Click to expand suggested configuration
JavaScript
1config.plugins.push(
2  new webpack.DefinePlugin({
3    "globalThis.__DEV__": false,
4  })
5);

Rollup

Click to expand suggested configuration
JavaScript
1export default [
2  {
3    // ... input, output, etc.
4    plugins: [
5      minify({
6        mangle: {
7          toplevel: true,
8        },
9        compress: {
10          toplevel: true,
11          global_defs: {
12            "@globalThis.__DEV__": "false",
13          },
14        },
15      }),
16    ],
17  },
18];

SWC

Click to expand suggested configuration
JSON
.swcrc
1{
2  "jsc": {
3    "transform": {
4      "optimizer": {
5        "globals": {
6          "vars": {
7            "globalThis.__DEV__": "false"
8          }
9        }
10      }
11    }
12  }
13}

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

Apollo Client uses the __DEV__ variable 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:

JavaScript
1// "main entrypoint import" style
2import { ApolloClient, InMemoryCache, useQuery } from "@apollo/client";
3
4// "deep entrypoint import" style
5import { ApolloClient, InMemoryCache } from "@apollo/client/core";
6import { 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" entry point '@apollo/client' and can only be imported directly, for example, from '@apollo/client/link/batch'. It's fine to use these, even when using the "main" entry point.

Why have a larger library in the first place?

Apollo Client is more than a data fetcher. It's a normalized request and response cache, state manager, and React component integration, including React testing utilities.

To build a similar experience with other libraries, you need to write custom logic, libraries, and component wrappers. Scaling this custom code to all your app components often creates a bundle larger than Apollo Client's.

A custom implementation also means extra maintenance work for your team. By using Apollo Client, you hand off that ownership to a team specializing in GraphQL clients.

Legacy support

Apollo Client v3 supports legacy JavaScript syntax. It also includes support for some now deprecated features. Similarly, v3 offers broad browser compatibility, extending back to IE11.

The next major version will prioritize contemporary browser support. This transition will streamline the client by removing unnecessary polyfills and phasing out support for legacy syntax and features. Ultimately, these changes reduce bundle size but require a major version release.

Feedback

Edit on GitHub

Forums