2022-02-07
3733
#apollo#nextjs
Coner Murphy
91409
Feb 7, 2022 â‹… 13 min read

Why use Next.js with Apollo

Coner Murphy Web developer, content creator, and tech entrepreneur building phytype.com. I post about web dev, tech entrepreneurship, and financial freedom on my Twitter and blog.

Recent posts:

Rxjs Adoption Guide: Overview, Examples, And Alternatives

RxJS adoption guide: Overview, examples, and alternatives

Get to know RxJS features, benefits, and more to help you understand what it is, how it works, and why you should use it.

Emmanuel Odioko
Jul 26, 2024 â‹… 13 min read
Decoupling Monoliths Into Microservices With Feature Flags

Decoupling monoliths into microservices with feature flags

Explore how to effectively break down a monolithic application into microservices using feature flags and Flagsmith.

Kayode Adeniyi
Jul 25, 2024 â‹… 10 min read
Lots of multi-colored blue and purplish rectangles.

Animating dialog and popover elements with CSS @starting-style

Native dialog and popover elements have their own well-defined roles in modern-day frontend web development. Dialog elements are known to […]

Rahul Chhodde
Jul 24, 2024 â‹… 10 min read
Using Llama Index To Add Personal Data To Large Language Models

Using LlamaIndex to add personal data to LLMs

LlamaIndex provides tools for ingesting, processing, and implementing complex query workflows that combine data access with LLM prompting.

Ukeje Goodness
Jul 23, 2024 â‹… 5 min read
View all posts

2 Replies to "Why use Next.js with Apollo"

  1. Thanks for this great tutorial (and the many other great LogRocket tutorials – I keep finding myself coming here when googling for solutions so your SEO + content strategy are working great too 🙂 )

    I am using Nextjs (SSG) + Strapi + Apollo/client for a website and everything is working perfectly. However I am trying to reduce client JS bundle size on Nextjs and apollo/client is the big biggest chunk that I am struggling to reduce/remove/split.

    All my pages are SSG and therefore since apollo/client is only used within getStaticProps, I believed that apollo/client should not be bundled for the client side.

    By a long process of elimination, I can see that even when I remove all references to apollo/client within /pages folder (by commenting out all the getStaticProps functions, it is still bundled and just simply having the following code within `lib/server-api.js` ensures it is bundled for the client-side:

    “`
    import {
    ApolloClient,
    InMemoryCache,
    gql,
    HttpLink,
    concat,
    ApolloLink,
    } from ‘@apollo/client’;

    const httpLink = new HttpLink({
    uri: `${
    process.env.NODE_ENV === ‘development’
    ? ‘http://localhost:1337’
    : process.env.NEXT_PUBLIC_STRAPI_API_URL
    }/graphql`,
    });

    const authMiddleware = new ApolloLink((operation, forward) => {
    operation.setContext(({ headers = {} }) => ({
    headers: {
    …headers,
    authorization: `Bearer ${
    process.env.NODE_ENV === ‘development’
    ? process.env.API_TOKEN_LOCAL
    : process.env.API_TOKEN
    }`,
    },
    }));

    return forward(operation);
    });
    const client = new ApolloClient({
    cache: new InMemoryCache(),
    link: concat(authMiddleware, httpLink),
    });
    “`
    When I comment out the above, it is no longer bundled. But as you can see from above, this code is located in `lib/server-api.js` and nothing is exported.

    So why is it bundling to the client-side?

    Any help greatly appreciated!!

Leave a Reply