Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions frontends/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
"./hooks/*": "./src/hooks/*/index.ts",
"./constants": "./src/common/constants.ts",
"./test-utils/factories": "./src/test-utils/factories/index.ts",
"./test-utils": "./src/test-utils/index.ts",
"./ssr": "./src/ssr.ts"
"./test-utils": "./src/test-utils/index.ts"
},
"peerDependencies": {
"react": "18.3.1"
Expand Down
10 changes: 0 additions & 10 deletions frontends/api/src/ssr.ts

This file was deleted.

1 change: 1 addition & 0 deletions frontends/main/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@emotion/cache": "^11.13.1",
"@mitodl/course-search-utils": "mitodl/course-search-utils.git#jk/5244-support-for-nextjs",
"@remixicon/react": "^4.2.0",
"@tanstack/react-query": "^4.36.1",
"api": "workspace:*",
"formik": "^2.4.6",
"lodash": "^4.17.21",
Expand Down
41 changes: 41 additions & 0 deletions frontends/main/src/app/getQueryClient.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import React from "react"
import { renderHook, waitFor } from "@testing-library/react"
import { allowConsoleErrors } from "ol-test-utilities"
import { QueryClientProvider, useQuery } from "@tanstack/react-query"
import { makeQueryClient } from "./getQueryClient"

const getWrapper = () => {
const queryClient = makeQueryClient()
const wrapper: React.FC<{ children: React.ReactNode }> = ({ children }) => (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
)
return wrapper
}

test.each([
{ status: 408, retries: 3 },
{ status: 429, retries: 3 },
{ status: 502, retries: 3 },
{ status: 503, retries: 3 },
{ status: 504, retries: 3 },
])(
"should retry $status failures $retries times",
async ({ status, retries }) => {
allowConsoleErrors()
const wrapper = getWrapper()
const queryFn = jest.fn().mockRejectedValue({ response: { status } })
const { result } = renderHook(
() =>
useQuery(["test"], {
queryFn,
retryDelay: 0,
}),
{ wrapper },
)

await waitFor(() => {
expect(result.current.isError).toBe(true)
})
expect(queryFn).toHaveBeenCalledTimes(retries + 1)
},
)
30 changes: 27 additions & 3 deletions frontends/main/src/app/getQueryClient.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,35 @@
// Based on https://tanstack.com/query/v5/docs/framework/react/guides/advanced-ssr

import { QueryClient, isServer } from "api/ssr"
import { QueryClient, isServer } from "@tanstack/react-query"

function makeQueryClient() {
type MaybeHasStatus = {
response?: {
status?: number
}
}

const RETRY_STATUS_CODES = [408, 429, 502, 503, 504]
const MAX_RETRIES = 3

const makeQueryClient = (): QueryClient => {
return new QueryClient({
defaultOptions: {
queries: {},
queries: {
refetchOnWindowFocus: false,
staleTime: Infinity,
retry: (failureCount, error) => {
const status = (error as MaybeHasStatus)?.response?.status
/**
* React Query's default behavior is to retry all failed queries 3
* times. Many things (e.g., 403, 404) are not worth retrying. Let's
* just retry some explicit whitelist of status codes.
*/
if (status !== undefined && RETRY_STATUS_CODES.includes(status)) {
return failureCount < MAX_RETRIES
}
return false
},
},
},
})
}
Expand Down
25 changes: 1 addition & 24 deletions frontends/main/src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import React from "react"
import type { Metadata } from "next"
import { dehydrate, Hydrate } from "api/ssr"
import HomePage from "@/app-pages/HomePage/HomePage"
import * as carousels from "@/app-pages/HomePage/carousels"
import { learningResourcesKeyFactory } from "api/hooks/learningResources"
import { FeaturedApiFeaturedListRequest } from "api/v1"
import { getQueryClient } from "./getQueryClient"
import { getMetadataAsync } from "@/common/metadata"

export async function generateMetadata({
Expand All @@ -22,25 +17,7 @@ export async function generateMetadata({
}

const Page: React.FC = async () => {
const queryClient = getQueryClient()

/*
* Prefetch the first tab (All) of the carousel
*/
await queryClient.prefetchQuery(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Getting rid of this because with a nonzero staletime, the featured course bookmarks are problematic: the resources are user-dependent, so they can't really be fetched on the server, unless they are immediately refetched, which we don't want.

We can re-enable this once https://github.com/mitodl/hq/issues/5159 is done

learningResourcesKeyFactory.featured(
carousels.FEATURED_RESOURCES_CAROUSEL[0].data
.params as FeaturedApiFeaturedListRequest,
),
)

const dehydratedState = dehydrate(queryClient)

return (
<Hydrate state={JSON.parse(JSON.stringify(dehydratedState))}>
<HomePage />
</Hydrate>
)
return <HomePage />
}

export default Page
2 changes: 1 addition & 1 deletion frontends/main/src/app/providers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import React from "react"
import { getQueryClient } from "./getQueryClient"
import { QueryClientProvider } from "api/ssr"
import { QueryClientProvider } from "@tanstack/react-query"
import { ThemeProvider, NextJsAppRouterCacheProvider } from "ol-components"
import { Provider as NiceModalProvider } from "@ebay/nice-modal-react"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ import {
import type { TabConfig } from "./types"
import { LearningResource, PaginatedLearningResourceList } from "api"
import { ResourceCard } from "../ResourceCard/ResourceCard"
import { useQueries, UseQueryResult, UseQueryOptions } from "api/ssr"
import {
useQueries,
UseQueryResult,
UseQueryOptions,
} from "@tanstack/react-query"

const StyledCarousel = styled(Carousel)({
/**
Expand Down
4 changes: 2 additions & 2 deletions frontends/main/src/test-utils/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* eslint-disable import/no-extraneous-dependencies */
import React from "react"
import { QueryClientProvider } from "api/ssr"
import { QueryClientProvider } from "@tanstack/react-query"
import { ThemeProvider } from "ol-components"
import { Provider as NiceModalProvider } from "@ebay/nice-modal-react"
import type { QueryClient } from "api/ssr"
import type { QueryClient } from "@tanstack/react-query"

import { makeQueryClient } from "@/app/getQueryClient"
import { render } from "@testing-library/react"
Expand Down
1 change: 1 addition & 0 deletions yarn.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.