Skip to content

Commit 1217a9d

Browse files
authored
Reduce fetching during ssr (#1304)
* Reduce fetching during ssr * Add a couple more withNoSsr calls
1 parent d7a95a7 commit 1217a9d

File tree

4 files changed

+32
-5
lines changed

4 files changed

+32
-5
lines changed

frontend/lib/with-apollo-client/get-apollo.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ function create(
7575
initialState: any,
7676
originalAccessToken?: string,
7777
locale?: string,
78+
skipSsrFetch?: boolean,
7879
) {
7980
const authLink = setContext((_, { headers }) => {
8081
// Always get the current access token from cookies in case it has changed
@@ -206,6 +207,13 @@ function create(
206207
})
207208
})
208209

210+
const skipSsrFetchLink = new ApolloLink(() => {
211+
return new Observable((observer) => {
212+
observer.next({ data: null })
213+
observer.complete()
214+
})
215+
})
216+
209217
const cache = createCache()
210218

211219
return new ApolloClient<NormalizedCacheObject>({
@@ -215,6 +223,8 @@ function create(
215223
isDefinedAndNotEmpty,
216224
),
217225
)
226+
: skipSsrFetch
227+
? skipSsrFetchLink
218228
: ApolloLink.from(
219229
[
220230
development ? metricsLink : undefined,
@@ -246,6 +256,7 @@ export default function getApollo(
246256
initialState: any,
247257
accessToken?: string,
248258
locale?: string,
259+
skipSsrFetch?: boolean,
249260
) {
250261
const userChanged = accessToken !== previousAccessToken
251262
const localeChanged = locale !== previousLocale
@@ -256,7 +267,7 @@ export default function getApollo(
256267
const _apolloClient =
257268
!userChanged && !localeChanged && isBrowser
258269
? apolloClient ?? create(initialState, accessToken, locale)
259-
: create(undefined, accessToken, locale)
270+
: create(undefined, accessToken, locale, skipSsrFetch)
260271

261272
previousAccessToken = accessToken
262273
previousLocale = locale

frontend/lib/with-apollo-client/index.tsx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ interface Props {
1515
apolloState: any
1616
accessToken?: string
1717
router?: Router
18+
skipSsrFetch?: boolean
1819
}
1920

2021
const isAppContext = (ctx: AppContext | NextPageContext): ctx is AppContext => {
@@ -27,12 +28,18 @@ const withApolloClient = (App: any) => {
2728
apollo,
2829
apolloState,
2930
accessToken,
31+
skipSsrFetch,
3032
...pageProps
3133
}: Props) => {
3234
// eslint-disable-next-line react-hooks/rules-of-hooks
3335
const router = useRouter()
3436
const locale = router.locale ?? pageProps?.router?.locale
35-
const apolloClient = getApollo(apolloState, accessToken, locale)
37+
const apolloClient = getApollo(
38+
apolloState,
39+
accessToken,
40+
locale,
41+
skipSsrFetch,
42+
)
3643

3744
return (
3845
<ApolloProvider client={apolloClient}>
@@ -62,7 +69,13 @@ const withApolloClient = (App: any) => {
6269
: ({} as any)
6370

6471
if (!signedIn) {
65-
const apollo = getApollo(apolloState, accessToken, ctx.locale)
72+
const skipSsrFetch = typeof window === "undefined"
73+
const apollo = getApollo(
74+
apolloState,
75+
accessToken,
76+
ctx.locale,
77+
skipSsrFetch,
78+
)
6679

6780
if (!pageProps.pageProps) {
6881
pageProps.pageProps = {}
@@ -79,6 +92,7 @@ const withApolloClient = (App: any) => {
7992
accessToken,
8093
apolloState,
8194
apollo,
95+
skipSsrFetch,
8296
}
8397
}
8498

frontend/pages/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Hype from "/components/NewLayout/Frontpage/Hype"
55
import { ModuleNavigation } from "/components/NewLayout/Frontpage/Modules"
66
// import News from "/components/NewLayout/Frontpage/News"
77
import SelectedCourses from "/components/NewLayout/Frontpage/SelectedCourses"
8+
import withNoSsr from "/util/withNoSsr"
89

910
// import UkraineInfo from "/components/NewLayout/Frontpage/UkraineInfo"
1011

@@ -32,4 +33,4 @@ const Home = () => {
3233
)
3334
}
3435

35-
export default Home
36+
export default withNoSsr(Home)

frontend/pages/study-modules/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { StudyModuleList } from "/components/NewLayout/Modules"
22
import { useBreadcrumbs } from "/hooks/useBreadcrumbs"
3+
import withNoSsr from "/util/withNoSsr"
34

45
function StudyModules() {
56
useBreadcrumbs([
@@ -16,4 +17,4 @@ function StudyModules() {
1617
)
1718
}
1819

19-
export default StudyModules
20+
export default withNoSsr(StudyModules)

0 commit comments

Comments
 (0)