-
Notifications
You must be signed in to change notification settings - Fork 269
WS-203 - Most Read pages on Next.js #13838
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
6713641
WS-203 - MostRead on Next.js
amoore108 e822265
Add rewrite to support variant routes whilst migrating
amoore108 26e26f2
Update next.config.js
amoore108 d5d85ad
Pass `id` as `mostReadTopic`
amoore108 c86a502
Update 5xx error
amoore108 65dcfae
Update [[...variant]].page.tsx
amoore108 ceaace6
Move `id` out of `context.query`
amoore108 8dc950e
Add gSSP tests
amoore108 0261ddb
Update [[...variant]].page.tsx
amoore108 fd225cb
Update `constructPageFetchUrl` to account for `mostReadTopic` ID
amoore108 b336c88
Move Most Read Topic into local data `/topics` folder
amoore108 b190e91
Move script switch e2es out of Express app
amoore108 dfdb300
Update index.cy.ts
amoore108 ce5c278
Revert "Update index.cy.ts"
amoore108 26380ef
Revert "Move script switch e2es out of Express app"
amoore108 6373264
Revert "Move Most Read Topic into local data `/topics` folder"
amoore108 79a8812
Revert "Update `constructPageFetchUrl` to account for `mostReadTopic`…
amoore108 207a72e
Fix `constructPageFetchUrl`
amoore108 a489e03
Fix import
amoore108 e2158e5
Fix `constructPageFetchUrl` check + add tests
amoore108 134b11f
Remove unneeded import
amoore108 9a9645d
Remove `*` wildcard from rewrite
amoore108 02be6f2
Normalise `id` path check
amoore108 9d62f36
Fix leading slash in tests
amoore108 630544d
Merge branch 'latest' into WS-203-most-read-nextjs
amoore108 b5a02dd
Merge branch 'latest' into WS-203-most-read-nextjs
eagerterrier c995f73
Update `cache-control` header to existing Most Read page
amoore108 b75053d
Support `.lite` extension in route
amoore108 fd0a2a3
Merge branch 'latest' into WS-203-most-read-nextjs
amoore108 223339a
Merge branch 'latest' into WS-203-most-read-nextjs
amoore108 39eb95f
Merge branch 'latest' into WS-203-most-read-nextjs
amoore108 1fd7828
Add `MOST_READ_PAGE` to `derivePageType` function
amoore108 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
ws-nextjs-app/pages/[service]/popular/[read]/[[...variant]].page.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| import { GetServerSidePropsContext } from 'next'; | ||
| import { MOST_READ_PAGE } from '#app/routes/utils/pageTypes'; | ||
| import pidginMostReadTopicFixture from '#data/pidgin/topics/mostReadTopic.json'; | ||
| import * as getPageDataModule from '../../../../utilities/pageRequests/getPageData'; | ||
| import { getServerSideProps as handleMostReadRoute } from './[[...variant]].page'; | ||
|
|
||
| jest.mock('../../../../utilities/pageRequests/getPageData'); | ||
|
|
||
| describe('handleMostReadRoute', () => { | ||
| const mockSetHeader = jest.fn(); | ||
| const mockGetServerSidePropsContext = { | ||
| req: { | ||
| headers: {}, | ||
| } as unknown as GetServerSidePropsContext['req'], | ||
| res: { | ||
| setHeader: mockSetHeader, | ||
| removeHeader: jest.fn(), | ||
| on: jest.fn(), | ||
| } as unknown as GetServerSidePropsContext['res'], | ||
| resolvedUrl: '/pidgin/popular/read', | ||
| query: { service: 'pidgin' }, | ||
| } satisfies GetServerSidePropsContext; | ||
|
|
||
| beforeEach(() => { | ||
| jest.restoreAllMocks(); | ||
| jest.clearAllMocks(); | ||
| jest.spyOn(getPageDataModule, 'default').mockResolvedValue({ | ||
| data: { | ||
| pageData: pidginMostReadTopicFixture.data, | ||
| status: 200, | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it('returns expected props if data fetch succeeds', async () => { | ||
| jest.spyOn(Date, 'now').mockImplementation(() => 1234567890000); | ||
|
|
||
| const result = await handleMostReadRoute(mockGetServerSidePropsContext); | ||
|
|
||
| expect(result.props.status).toEqual(200); | ||
| expect(result.props.pageType).toEqual(MOST_READ_PAGE); | ||
| }); | ||
|
|
||
| it('returns error props if data fetch returns 500', async () => { | ||
| jest.spyOn(getPageDataModule, 'default').mockResolvedValue({ | ||
| data: { | ||
| pageData: pidginMostReadTopicFixture.data, | ||
| status: 500, | ||
| }, | ||
| }); | ||
|
|
||
| jest.spyOn(Date, 'now').mockImplementation(() => 1234567890000); | ||
|
|
||
| const result = await handleMostReadRoute(mockGetServerSidePropsContext); | ||
|
|
||
| expect(result).toEqual({ | ||
| props: expect.objectContaining({ | ||
| status: 500, | ||
| pageType: MOST_READ_PAGE, | ||
| pathname: '/pidgin/popular/read', | ||
| }), | ||
| }); | ||
| }); | ||
|
|
||
| it('returns error props if data fetch returns 404', async () => { | ||
| jest.spyOn(getPageDataModule, 'default').mockResolvedValue({ | ||
| data: { | ||
| pageData: pidginMostReadTopicFixture.data, | ||
| status: 404, | ||
| }, | ||
| }); | ||
|
|
||
| jest.spyOn(Date, 'now').mockImplementation(() => 1234567890000); | ||
|
|
||
| const result = await handleMostReadRoute(mockGetServerSidePropsContext); | ||
|
|
||
| expect(result).toEqual({ | ||
| props: expect.objectContaining({ | ||
| status: 404, | ||
| pageType: MOST_READ_PAGE, | ||
| pathname: '/pidgin/popular/read', | ||
| }), | ||
| }); | ||
| }); | ||
|
|
||
| it('throws if pageData is missing', async () => { | ||
| jest.spyOn(getPageDataModule, 'default').mockResolvedValue({ | ||
| data: { pageData: null, status: 200 }, | ||
| }); | ||
|
|
||
| await expect( | ||
| handleMostReadRoute(mockGetServerSidePropsContext), | ||
| ).rejects.toThrow('MostReadPage data is malformed'); | ||
| }); | ||
|
|
||
| it('sets correct cache-control header', async () => { | ||
| await handleMostReadRoute(mockGetServerSidePropsContext); | ||
|
|
||
| expect(mockSetHeader).toHaveBeenCalledWith( | ||
| 'Cache-Control', | ||
| expect.stringContaining('max-age=30'), | ||
| ); | ||
| }); | ||
| }); |
117 changes: 117 additions & 0 deletions
117
ws-nextjs-app/pages/[service]/popular/[read]/[[...variant]].page.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| import { GetServerSidePropsContext } from 'next'; | ||
| import dynamic from 'next/dynamic'; | ||
| import { TOPIC_PAGE, MOST_READ_PAGE } from '#app/routes/utils/pageTypes'; | ||
| import nodeLogger from '#lib/logger.node'; | ||
| import logResponseTime from '#server/utilities/logResponseTime'; | ||
| import { ROUTING_INFORMATION } from '#app/lib/logger.const'; | ||
| import { OK } from '#app/lib/statusCodes.const'; | ||
| import PageDataParams from '#app/models/types/pageDataParams'; | ||
| import deriveVariant from '#nextjs/utilities/deriveVariant'; | ||
| import isTest from '#app/lib/utilities/isTest'; | ||
| import handleError from '#app/routes/utils/handleError'; | ||
| import getPageData from '#nextjs/utilities/pageRequests/getPageData'; | ||
|
|
||
| const MostReadAsTopicPage = dynamic( | ||
| () => import('#app/pages/TopicPage/TopicPage'), | ||
| ); | ||
|
|
||
| const logger = nodeLogger(__filename); | ||
|
|
||
| export const getServerSideProps = async ( | ||
| context: GetServerSidePropsContext, | ||
| ) => { | ||
| const { resolvedUrl } = context; | ||
|
|
||
| logResponseTime({ path: resolvedUrl }, context.res, () => null); | ||
|
|
||
| const { | ||
| service, | ||
| variant: variantFromUrl, | ||
| renderer_env: rendererEnvFromQuery, | ||
| page, | ||
| } = context.query as PageDataParams; | ||
|
|
||
| const variant = deriveVariant(variantFromUrl); | ||
|
|
||
| const rendererEnv = | ||
| isTest() && !rendererEnvFromQuery ? 'live' : rendererEnvFromQuery; | ||
|
|
||
| const resolvedUrlWithoutQuery = resolvedUrl.split('?')?.[0]; | ||
|
|
||
| const id = 'mostReadTopic'; | ||
|
|
||
| const { data } = await getPageData({ | ||
| id, | ||
| page, | ||
| service, | ||
| variant, | ||
| rendererEnv, | ||
| resolvedUrl: resolvedUrlWithoutQuery, | ||
| pageType: TOPIC_PAGE, | ||
| }); | ||
|
|
||
| const { status } = data; | ||
|
|
||
| context.res.statusCode = status; | ||
|
|
||
| let routingInfoLogger = logger.debug; | ||
|
|
||
| if (status !== OK) { | ||
| routingInfoLogger = logger.error; | ||
|
|
||
| routingInfoLogger(ROUTING_INFORMATION, { | ||
| url: resolvedUrlWithoutQuery, | ||
| status, | ||
| pageType: MOST_READ_PAGE, | ||
| }); | ||
|
|
||
| return { | ||
| props: { | ||
| service, | ||
| status, | ||
| timeOnServer: Date.now(), | ||
| variant, | ||
| pageType: MOST_READ_PAGE, | ||
| pathname: resolvedUrlWithoutQuery, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| if (!data?.pageData) { | ||
| throw handleError('MostReadPage data is malformed', 500); | ||
| } | ||
|
|
||
| context.res.setHeader( | ||
| 'Cache-Control', | ||
| 'public, stale-if-error=300, stale-while-revalidate=120, max-age=30', | ||
| ); | ||
|
|
||
| routingInfoLogger(ROUTING_INFORMATION, { | ||
| url: resolvedUrlWithoutQuery, | ||
| status: data.status, | ||
| pageType: MOST_READ_PAGE, | ||
| }); | ||
|
|
||
| return { | ||
| props: { | ||
| error: data?.error || null, | ||
| id, | ||
| page: page || null, | ||
| pageData: { | ||
| ...data.pageData, | ||
| metadata: { | ||
| ...data.pageData.metadata, | ||
| type: MOST_READ_PAGE, | ||
| }, | ||
| }, | ||
| pageType: MOST_READ_PAGE, | ||
| pathname: resolvedUrlWithoutQuery, | ||
| service, | ||
| status: data.status, | ||
| timeOnServer: Date.now(), | ||
| variant, | ||
| }, | ||
| }; | ||
| }; | ||
|
|
||
| export default MostReadAsTopicPage; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, just wanted to ask where this fixture data came from? Was it FABL?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea we added some logic in the API to return a topic page if
id='mostReadTopic'is being requested: https://github.com/bbc/fabl-modules/pull/11664Just seemed to make sense to re-use the Topics API since it handles all the fetching and data transformation for us already.