-
Notifications
You must be signed in to change notification settings - Fork 262
WS-1943-Create route in Simorgh to render Topic pages in Next.js app #13593
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
Open
Nabeel1276
wants to merge
17
commits into
latest
Choose a base branch
from
WS-1942-create-route-in-simorgh-to-render-topic-pages
base: latest
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+281
−48
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
f6f35fb
updated constructPageFetchUrl to point to nextJS and created new file…
Nabeel1276 77241ac
added fetching for nextJS
Nabeel1276 fbf595a
Merge branch 'latest' into WS-1942-create-route-in-simorgh-to-render-…
Nabeel1276 9a6623c
updated simorgh release info file with correct local topic url for pi…
Nabeel1276 3727890
Merge branch 'latest' into WS-1942-create-route-in-simorgh-to-render-…
Nabeel1276 b2c4693
Retain legacy renderer_env override logic for topics
Nabeel1276 e798b86
Updated all references of port 7080 to 7081 and added test file
Nabeel1276 a4f2e96
added unit tests topics but are not passing
Nabeel1276 f96fb60
passed all tests except one
Nabeel1276 d740a20
updated all references of 7080 to 7081
Nabeel1276 38767ef
removed unneccessary test
Nabeel1276 0d2d2c5
reinsated log: jest.fn(),
Nabeel1276 a82b7a9
added topic page derive page type
Nabeel1276 efc4814
Merge branch 'latest' into WS-1942-create-route-in-simorgh-to-render-…
Nabeel1276 b6e011a
added unit test for derive topic page
Nabeel1276 02236f5
reordered layout of logic
Nabeel1276 773beac
removed repeated context.res.statusCode
Nabeel1276 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
109 changes: 109 additions & 0 deletions
109
ws-nextjs-app/pages/[service]/topics/[id]/[[...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,109 @@ | ||
| import { GetServerSidePropsContext } from 'next'; | ||
| import pidginTopicFixtureData from '#data/pidgin/topics/c95y35941vrt.json'; | ||
| import * as shouldRender from '../../../../utilities/shouldRender'; | ||
| import * as getPageDataModule from '../../../../utilities/pageRequests/getPageData'; | ||
| import { getServerSideProps as handleTopicRoute } from './[[...variant]].page'; | ||
|
|
||
| jest.mock('../../../../utilities/pageRequests/getPageData'); | ||
| jest.mock('../../../../utilities/shouldRender', () => { | ||
| const originalModule = jest.requireActual( | ||
| '../../../../utilities/shouldRender', | ||
| ); | ||
| return { | ||
| __esModule: true, | ||
| ...originalModule, | ||
| }; | ||
| }); | ||
|
|
||
| describe('handleTopicRoute', () => { | ||
| 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/topics/c95y35941vrt', | ||
| query: { service: 'pidgin', id: 'c95y35941vrt' }, | ||
| } satisfies GetServerSidePropsContext; | ||
|
|
||
| beforeEach(() => { | ||
| jest.restoreAllMocks(); | ||
| jest.clearAllMocks(); | ||
| jest.spyOn(getPageDataModule, 'default').mockResolvedValue({ | ||
| data: { | ||
| pageData: pidginTopicFixtureData.data, | ||
| status: 200, | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it('returns expected props if shouldRender succeeds', async () => { | ||
| jest.spyOn(Date, 'now').mockImplementation(() => 1234567890000); | ||
|
|
||
| const result = await handleTopicRoute(mockGetServerSidePropsContext); | ||
|
|
||
| expect(result.props.status).toEqual(200); | ||
| expect(result.props.pageType).toEqual('topic'); | ||
| }); | ||
|
|
||
| it('returns error props if shouldRender fails - 500', async () => { | ||
| jest.spyOn(shouldRender, 'default').mockReturnValue({ | ||
| hasRequestSucceeded: false, | ||
| status: 500, | ||
| }); | ||
|
|
||
| jest.spyOn(Date, 'now').mockImplementation(() => 1234567890000); | ||
|
|
||
| const result = await handleTopicRoute(mockGetServerSidePropsContext); | ||
|
|
||
| expect(result).toEqual({ | ||
| props: expect.objectContaining({ | ||
| status: 500, | ||
| pageType: 'topic', | ||
| pathname: '/pidgin/topics/c95y35941vrt', | ||
| }), | ||
| }); | ||
| }); | ||
|
|
||
| it('returns error props if shouldRender fails - 404', async () => { | ||
| jest.spyOn(shouldRender, 'default').mockReturnValue({ | ||
| hasRequestSucceeded: false, | ||
| status: 404, | ||
| }); | ||
|
|
||
| jest.spyOn(Date, 'now').mockImplementation(() => 1234567890000); | ||
|
|
||
| const result = await handleTopicRoute(mockGetServerSidePropsContext); | ||
|
|
||
| expect(result).toEqual({ | ||
| props: expect.objectContaining({ | ||
| status: 404, | ||
| pageType: 'topic', | ||
| pathname: '/pidgin/topics/c95y35941vrt', | ||
| }), | ||
| }); | ||
| }); | ||
|
|
||
| it('throws if pageData is missing', async () => { | ||
| jest.spyOn(getPageDataModule, 'default').mockResolvedValue({ | ||
| data: { pageData: null, status: 200 }, | ||
| }); | ||
|
|
||
| await expect( | ||
| handleTopicRoute(mockGetServerSidePropsContext), | ||
| ).rejects.toThrow('TopicPage data is malformed'); | ||
| }); | ||
|
|
||
| it('sets correct cache-control header', async () => { | ||
| await handleTopicRoute(mockGetServerSidePropsContext); | ||
|
|
||
| expect(mockSetHeader).toHaveBeenCalledWith( | ||
| 'Cache-Control', | ||
| expect.stringContaining('max-age=240'), | ||
| ); | ||
| }); | ||
| }); |
108 changes: 108 additions & 0 deletions
108
ws-nextjs-app/pages/[service]/topics/[id]/[[...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,108 @@ | ||
| import { GetServerSideProps } from 'next'; | ||
| import dynamic from 'next/dynamic'; | ||
| import { TOPIC_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 shouldRender from '#nextjs/utilities/shouldRender'; | ||
| import handleError from '#app/routes/utils/handleError'; | ||
| import getPageData from '../../../../utilities/pageRequests/getPageData'; | ||
|
|
||
| const TopicPage = dynamic(() => import('#app/pages/TopicPage/TopicPage')); | ||
|
|
||
| const logger = nodeLogger(__filename); | ||
|
|
||
| export const getServerSideProps: GetServerSideProps = async context => { | ||
| logResponseTime({ path: context.resolvedUrl }, context.res, () => null); | ||
|
|
||
| const { | ||
| id, | ||
| service, | ||
| variant: variantFromUrl, | ||
| renderer_env: rendererEnvFromQuery, | ||
| page, | ||
| } = context.query as PageDataParams; | ||
|
|
||
| const variant = deriveVariant(variantFromUrl); | ||
|
|
||
| const rendererEnv = | ||
| isTest() && !rendererEnvFromQuery ? 'live' : rendererEnvFromQuery; | ||
|
|
||
| const { data } = await getPageData({ | ||
| id, | ||
| page, | ||
| service, | ||
| variant, | ||
| rendererEnv, | ||
amoore108 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| resolvedUrl: context.resolvedUrl, | ||
| pageType: TOPIC_PAGE, | ||
| }); | ||
|
|
||
| const { status } = data; | ||
|
|
||
| context.res.statusCode = status; | ||
|
|
||
| let routingInfoLogger = logger.debug; | ||
|
|
||
| const { hasRequestSucceeded, status: renderStatus } = shouldRender( | ||
| { pageData: data.pageData, status: data.status }, | ||
| service, | ||
| ); | ||
|
|
||
| if (!hasRequestSucceeded && renderStatus !== OK) { | ||
| routingInfoLogger = logger.error; | ||
|
|
||
| return { | ||
| props: { | ||
| service, | ||
| status: renderStatus, | ||
| timeOnServer: Date.now(), | ||
| variant, | ||
| pageType: TOPIC_PAGE, | ||
| pathname: context.resolvedUrl, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| if (!data?.pageData) { | ||
| throw handleError('TopicPage data is malformed', 500); | ||
| } | ||
|
|
||
| context.res.setHeader( | ||
| 'Cache-Control', | ||
| 'public, stale-if-error=2400, stale-while-revalidate=960, max-age=240', | ||
| ); | ||
|
|
||
| routingInfoLogger(ROUTING_INFORMATION, { | ||
| url: context.resolvedUrl, | ||
| status: data.status, | ||
| pageType: TOPIC_PAGE, | ||
| }); | ||
|
|
||
| return { | ||
| props: { | ||
| error: data?.error || null, | ||
| id, | ||
| page: page || null, | ||
| pageData: { | ||
| ...data.pageData, | ||
| metadata: { | ||
| ...data.pageData.metadata, | ||
| type: TOPIC_PAGE, | ||
| }, | ||
| }, | ||
| pageType: TOPIC_PAGE, | ||
| pathname: context.resolvedUrl, | ||
| service, | ||
| status: data.status, | ||
| timeOnServer: Date.now(), | ||
| variant, | ||
| }, | ||
| }; | ||
| }; | ||
|
|
||
| export default TopicPage; | ||
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.
Uh oh!
There was an error while loading. Please reload this page.