Skip to content

Commit

Permalink
WIP: Dependency upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
juyrjola committed Nov 1, 2023
1 parent 82af8d5 commit a93a31d
Show file tree
Hide file tree
Showing 11 changed files with 36,229 additions and 26,791 deletions.
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20.6.1
18
65 changes: 34 additions & 31 deletions common/apollo.js → common/apollo.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
import withApollo from 'next-with-apollo';
import { HttpLink, ApolloLink } from '@apollo/client';
import {
ApolloClient,
HttpLink,
ApolloLink,
ApolloProvider,
} from '@apollo/client';
import { getDataFromTree } from '@apollo/client/react/ssr';
import { InMemoryCache } from '@apollo/client/cache';
NextSSRInMemoryCache,
NextSSRApolloClient,
} from '@apollo/experimental-nextjs-app-support/ssr';
import { onError } from '@apollo/client/link/error';
import getConfig from 'next/config';

import { captureException, Sentry } from 'common/sentry';
import { getI18n } from 'common/i18n';
import possibleTypes from 'common/__generated__/possible_types.json';
import { NextPageContext } from 'next';

const { publicRuntimeConfig } = getConfig();

const GRAPHQL_ENDPOINT_URI = `${publicRuntimeConfig.aplansApiBaseURL}/graphql/`;

let globalRequestContext;
let globalPlanIdentifier;
let globalRequestContext: NextPageContext | undefined;
let globalPlanIdentifier: string | undefined;

const localeMiddleware = new ApolloLink((operation, forward) => {
// Inject @locale directive into the query root object
Expand Down Expand Up @@ -150,18 +147,22 @@ const httpLink = new HttpLink({

let globalApolloClient;

export function initializeApolloClient(opts) {
const { ctx, initialState, planIdentifier } = opts;
type WatchApolloClientOpts = {
planIdentifier?: string;
ctx?: NextPageContext;
};

function _initializeApolloClient(opts: WatchApolloClientOpts) {
const { planIdentifier } = opts;
const isServer = typeof window === 'undefined';

if (planIdentifier) {
globalPlanIdentifier = planIdentifier;
} else if (ctx?.req?.planIdentifier) {
globalPlanIdentifier = ctx.req.planIdentifier;
} else if (opts.planIdentifier) {
globalPlanIdentifier = opts.planIdentifier;
}
if (ctx) globalRequestContext = ctx;
if (opts.ctx) globalRequestContext = opts.ctx;

if (globalApolloClient && !isServer) return globalApolloClient;
const clientOpts = {
ssrMode: isServer,
link: ApolloLink.from([
Expand All @@ -172,24 +173,26 @@ export function initializeApolloClient(opts) {
sentryErrorLink,
httpLink,
]),
cache: new InMemoryCache({
cache: new NextSSRInMemoryCache({
// https://www.apollographql.com/docs/react/data/fragments/#defining-possibletypes-manually
possibleTypes: possibleTypes.possibleTypes,
}).restore(initialState || {}),
}),
};
const apolloClient = new ApolloClient(clientOpts);

if (!isServer) globalApolloClient = apolloClient;
const apolloClient = new NextSSRApolloClient(clientOpts);
return apolloClient;
}

export default withApollo((opts) => initializeApolloClient(opts), {
render: ({ Page, props }) => {
return (
<ApolloProvider client={props.apollo}>
<Page {...props} />
</ApolloProvider>
);
},
getDataFromTree,
});
export function initializeApolloClient(opts: WatchApolloClientOpts) {
if (!process.browser) {
const {
registerApolloClient,
} = require('@apollo/experimental-nextjs-app-support/rsc');

const ret = registerApolloClient(() => {
return _initializeApolloClient(opts);
});
return ret.getClient();
} else {
return _initializeApolloClient(opts);
}
}
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const customJestConfig = {
'^context/(.*)$': '<rootDir>/context/$1',
},
testEnvironment: 'jest-environment-jsdom',
testMatch: ['**/tests/**/*.test.[jt]s'],
};

// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async
Expand Down
8 changes: 3 additions & 5 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,9 @@ let config = {
styledComponents: true,
},
swcMinify: true,
experimental: {
modularizeImports: {
lodash: {
transform: 'lodash/{{member}}',
},
modularizeImports: {
lodash: {
transform: 'lodash/{{member}}',
},
},
async rewrites() {
Expand Down
Loading

0 comments on commit a93a31d

Please sign in to comment.