apollographql / apollo-client-nextjs

Apollo Client support for the Next.js App Router
https://www.npmjs.com/package/@apollo/experimental-nextjs-app-support
MIT License
358 stars 25 forks source link

RSC example does not work with runtime = "edge"; #113

Closed martior closed 2 months ago

martior commented 7 months ago

importing

import { ApolloClient, HttpLink, InMemoryCache } from "@apollo/client";
import { registerApolloClient } from "@apollo/experimental-nextjs-app-support/rsc";
export const { getClient } = registerApolloClient(() => {
  return new ApolloClient({
    cache: new InMemoryCache(),
    link: new HttpLink({
      // this needs to be an absolute url, as relative urls cannot be used in SSR
      uri: process.env.API_URL,
      // you can disable result caching here if you want to
      // (this does not work if you are rendering your page with `export const dynamic = "force-static"`)
      // fetchOptions: { cache: "no-store" },
    }),
  });
});

from a app/page.tsx with export const runtime = "edge"; gives the following error:

../../node_modules/@apollo/client/react/context/ApolloContext.js
Attempted import error: 'createContext' is not exported from 'react' (imported as 'React').

It works if you remove export const runtime = "edge"

phryneas commented 7 months ago

Hi @martior, this is (unfortunately) a known issue. This seems at this point more like an accident on the Next.js side. A similar issue has recently been resolved from their side in https://github.com/vercel/next.js/pull/56501, but runtime = "edge" is still being investigated by them.

huozhi commented 6 months ago

Checked with webpack team that exports checking needs to be applied to package. There seems no way to partially disable the exports checking for certain package (like react) in certain runtime (such as edge runtime). As apollo-client in edge runtime might still access some unexisted property for running conditional code, one solution I got from webpack team is to use Object(React).<property> e.g. Object(React).createContext to get rid of the check. So it could also use that to do it.

phryneas commented 6 months ago

@huozhi Thank you for the update! At this point we will probably consider using a wrapper package around React to deal with that - we can't be sure if a hack that works with one bundler today will break with the next bundler tomorrow.

bashaus commented 5 months ago

@phryneas are there any updates on this? sorry to chase .. !

phryneas commented 5 months ago

@bashaus actually, there are! You should be able to use the Apollo Client 3.9 alphas in the Edge runtime.

phryneas commented 2 months ago

I'm doing some housekeeping so I'm closing some older issues that haven't seen activity in a while. If this is still relevant, please feel free to reopen the issue.

github-actions[bot] commented 2 months ago

Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo Client usage and allow us to serve you better.