Skip to content

Commit d688528

Browse files
jonkaftonmbertrand
authored andcommitted
Revert "Server rendering for homepage, units and topics listing pages (#1822)" (#1838)
This reverts commit c09f9b5.
1 parent 2173201 commit d688528

File tree

29 files changed

+173
-280
lines changed

29 files changed

+173
-280
lines changed

frontends/api/src/hooks/channels/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,5 @@ export {
5555
useChannelsList,
5656
useChannelPartialUpdate,
5757
useChannelCounts,
58-
channels,
58+
channels as channelsKeyFactory,
5959
}

frontends/api/src/hooks/learningResources/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,5 +531,5 @@ export {
531531
useListItemMove,
532532
usePlatformsList,
533533
useSchoolsList,
534-
learningResources,
534+
learningResources as learningResourcesKeyFactory,
535535
}

frontends/api/src/hooks/learningResources/keyFactory.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@ import type {
3333

3434
import { createQueryKeys } from "@lukemorales/query-key-factory"
3535

36-
/* TODO we can add this back in once resource user list membership is implemented in the front end
37-
* Currently we fetch on the server for public resources and then refetch on the client for the
38-
* user lists parents. Randomizing therefore causes a content flicker as the client loaded
39-
* sequence replaces the server rendered sequence.
40-
4136
const shuffle = ([...arr]) => {
4237
let m = arr.length
4338
while (m) {
@@ -66,7 +61,6 @@ const randomizeResults = ([...results]) => {
6661
})
6762
return randomizedResults
6863
}
69-
*/
7064

7165
const learningResources = createQueryKeys("learningResources", {
7266
detail: (id: number) => ({
@@ -87,7 +81,7 @@ const learningResources = createQueryKeys("learningResources", {
8781
queryKey: [params],
8882
queryFn: () => {
8983
return featuredApi.featuredList(params).then((res) => {
90-
// res.data.results = randomizeResults(res.data?.results)
84+
res.data.results = randomizeResults(res.data?.results)
9185
return res.data
9286
})
9387
},

frontends/api/src/hooks/newsEvents/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ export {
1919
useNewsEventsList,
2020
useNewsEventsDetail,
2121
NewsEventsListFeedTypeEnum,
22-
newsEvents,
22+
newsEvents as newsEventsKeyFactory,
2323
}

frontends/api/src/hooks/testimonials/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,8 @@ const useTestimonialDetail = (id: number | undefined) => {
2323
})
2424
}
2525

26-
export { useTestimonialDetail, useTestimonialList, testimonials }
26+
export {
27+
useTestimonialDetail,
28+
useTestimonialList,
29+
testimonials as testimonialsKeyFactory,
30+
}

frontends/api/src/ssr/prefetch.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
import { QueryClient, dehydrate } from "@tanstack/react-query"
22
import type { Query } from "@tanstack/react-query"
33

4-
/* Utility to avoid repetition in server components
5-
* Optionally pass the queryClient returned from a previous prefetch
6-
* where queries are dependent on previous results
7-
*/
8-
export const prefetch = async (
9-
queries: (Query | unknown)[],
10-
queryClient?: QueryClient,
11-
) => {
12-
queryClient = queryClient || new QueryClient()
4+
// Utility to avoid repetition in server components
5+
export const prefetch = async (queries: (Query | unknown)[]) => {
6+
const queryClient = new QueryClient()
137

148
await Promise.all(
159
queries.map((query) => queryClient.prefetchQuery(query as Query)),
1610
)
1711

18-
return { dehydratedState: dehydrate(queryClient), queryClient }
12+
return dehydrate(queryClient)
1913
}

frontends/api/src/ssr/usePrefetchWarnings.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { usePrefetchWarnings } from "./usePrefetchWarnings"
44
import { setupReactQueryTest } from "../hooks/test-utils"
55
import { urls, factories, setMockResponse } from "../test-utils"
66
import {
7-
learningResources,
7+
learningResourcesKeyFactory,
88
useLearningResourcesDetail,
99
} from "../hooks/learningResources"
1010

@@ -45,7 +45,7 @@ describe("SSR prefetch warnings", () => {
4545
expect.objectContaining({
4646
disabled: false,
4747
initialStatus: "loading",
48-
key: learningResources.detail(1).queryKey,
48+
key: learningResourcesKeyFactory.detail(1).queryKey,
4949
observerCount: 1,
5050
}),
5151
],
@@ -65,7 +65,7 @@ describe("SSR prefetch warnings", () => {
6565
wrapper,
6666
initialProps: {
6767
queryClient,
68-
exemptions: [learningResources.detail(1).queryKey],
68+
exemptions: [learningResourcesKeyFactory.detail(1).queryKey],
6969
},
7070
})
7171

@@ -83,7 +83,7 @@ describe("SSR prefetch warnings", () => {
8383
const { unmount } = renderHook(
8484
() =>
8585
useQuery({
86-
...learningResources.detail(1),
86+
...learningResourcesKeyFactory.detail(1),
8787
initialData: data,
8888
}),
8989
{ wrapper },
@@ -105,9 +105,9 @@ describe("SSR prefetch warnings", () => {
105105
[
106106
{
107107
disabled: false,
108-
hash: JSON.stringify(learningResources.detail(1).queryKey),
108+
hash: JSON.stringify(learningResourcesKeyFactory.detail(1).queryKey),
109109
initialStatus: "success",
110-
key: learningResources.detail(1).queryKey,
110+
key: learningResourcesKeyFactory.detail(1).queryKey,
111111
observerCount: 0,
112112
status: "success",
113113
},

frontends/api/src/ssr/usePrefetchWarnings.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,6 @@ export const usePrefetchWarnings = ({
4545
*/
4646
useEffect(
4747
() => {
48-
/* Invalidate all learning resource queries so they are re-fetched in the client
49-
* for session specific data (user list memberships).
50-
* This is interim, until the memberships endpoint is implemented in the frontend
51-
* TODO remove once complete (see https://github.com/mitodl/hq/issues/5159)
52-
*/
53-
queryClient.invalidateQueries(["learningResources"])
54-
5548
if (process.env.NODE_ENV === "production") {
5649
return
5750
}

frontends/api/src/test-utils/factories/channels.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ const _channelShared = (): Partial<Omit<Channel, "channel_type">> => {
148148
key: faker.lorem.slug(),
149149
value: faker.lorem.slug(),
150150
}),
151-
channel_url: `${faker.internet.url({ appendSlash: false })}${faker.system.directoryPath()}`,
152151
}
153152
}
154153

frontends/api/src/test-utils/urls.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import type {
99
NewsEventsApiNewsEventsListRequest,
1010
TestimonialsApi,
11-
ChannelsApi,
1211
} from "../generated/v0"
1312
import type {
1413
LearningResourcesApi as LRApi,
@@ -180,8 +179,6 @@ const channels = {
180179
details: (channelType: string, name: string) =>
181180
`${API_BASE_URL}/api/v0/channels/type/${channelType}/${name}/`,
182181
patch: (id: number) => `${API_BASE_URL}/api/v0/channels/${id}/`,
183-
list: (params?: Paramsv0<ChannelsApi, "channelsList">) =>
184-
`${API_BASE_URL}/api/v0/channels/${query(params)}`,
185182
}
186183

187184
const widgetLists = {

frontends/main/src/app-pages/UnitsListingPage/UnitCard.tsx

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import React from "react"
2-
import type { OfferedByEnum } from "api"
3-
import type { UnitChannel } from "api/v0"
2+
import { LearningResourceOfferorDetail, OfferedByEnum } from "api"
43
import {
54
Card,
65
Skeleton,
@@ -9,6 +8,7 @@ import {
98
theme,
109
UnitLogo,
1110
} from "ol-components"
11+
import { useChannelDetail } from "api/hooks/channels"
1212
import Link from "next/link"
1313

1414
const CardStyled = styled(Card)({
@@ -102,23 +102,25 @@ const CountsText = styled(Typography)(({ theme }) => ({
102102
}))
103103

104104
interface UnitCardsProps {
105-
channels: UnitChannel[] | undefined
105+
units: LearningResourceOfferorDetail[] | undefined
106106
courseCounts: Record<string, number>
107107
programCounts: Record<string, number>
108108
}
109109

110110
interface UnitCardProps {
111-
channel: UnitChannel
111+
unit: LearningResourceOfferorDetail
112112
courseCount: number
113113
programCount: number
114114
}
115115

116116
const UnitCard: React.FC<UnitCardProps> = (props) => {
117-
const { channel, courseCount, programCount } = props
118-
const unit = channel.unit_detail.unit
117+
const { unit, courseCount, programCount } = props
118+
const channelDetailQuery = useChannelDetail("unit", unit.code)
119+
const channelDetail = channelDetailQuery.data
120+
const unitUrl = channelDetail?.channel_url
119121

120-
if (!channel.channel_url) return null
121-
const href = new URL(channel.channel_url).pathname
122+
if (!unitUrl) return null
123+
const href = unitUrl && new URL(unitUrl).pathname
122124

123125
return (
124126
<CardStyled forwardClicksToLink data-testid={`unit-card-${unit.code}`}>
@@ -132,7 +134,9 @@ const UnitCard: React.FC<UnitCardProps> = (props) => {
132134
</LogoContainer>
133135
<CardBottom>
134136
<ValuePropContainer>
135-
<HeadingText>{channel?.configuration?.heading}</HeadingText>
137+
<HeadingText>
138+
{channelDetail?.configuration?.heading}
139+
</HeadingText>
136140
</ValuePropContainer>
137141
<CountsTextContainer>
138142
<CountsText data-testid={`course-count-${unit.code}`}>
@@ -170,18 +174,17 @@ export const UnitCardLoading = () => {
170174
}
171175

172176
export const UnitCards: React.FC<UnitCardsProps> = (props) => {
173-
const { channels, courseCounts, programCounts } = props
177+
const { units, courseCounts, programCounts } = props
174178
return (
175179
<>
176-
{channels?.map((channel) => {
177-
const unit = channel.unit_detail.unit
180+
{units?.map((unit) => {
178181
const courseCount = courseCounts[unit.code] || 0
179182
const programCount = programCounts[unit.code] || 0
180183

181184
return unit.value_prop ? (
182185
<UnitCard
183186
key={unit.code}
184-
channel={channel}
187+
unit={unit}
185188
courseCount={courseCount}
186189
programCount={programCount}
187190
/>

0 commit comments

Comments
 (0)