Skip to content
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

Stop landing pages from rendering at the dynamic route #11354

Merged
merged 7 commits into from
Nov 5, 2024
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
25 changes: 15 additions & 10 deletions common/services/prismic/link-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ type Props = {
type: string;
siteSection?: SiteSection;
};

// Untransformed data
type DataProps = {
uid?: string;
type: string;
Expand All @@ -20,7 +22,8 @@ type DataProps = {
};

function linkResolver(doc: Props | DataProps): string {
// this is mostly useful for scenarios like rendering in Page Builder
// This is mostly useful for scenarios like rendering in Page Builder
// which doesn't necessarily have access to all data
if (!doc) return '/';

const { uid, type } = doc;
Expand Down Expand Up @@ -52,18 +55,20 @@ function linkResolver(doc: Props | DataProps): string {
}

if (type === 'pages') {
if ('siteSection' in doc) {
return `${doc.siteSection}/${uid}`;
} else if ('tags' in doc) {
// Needed for Prismic previews
const docSiteSection = doc.tags.find(t => isSiteSection(t));
let siteSection: SiteSection | undefined;

const isLandingPage = docSiteSection === uid;
if (isLandingPage) return `/${uid}`;
if ('siteSection' in doc) {
siteSection = doc.siteSection;
}

return `${docSiteSection ? '/' + docSiteSection : ''}/${uid}`;
// Prismic previews come through here.
if ('tags' in doc) {
Copy link
Contributor

Choose a reason for hiding this comment

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

does this mean that if there is a sitesection tag, it would take precedence over doc.siteSection – and if so is that ok?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I guess so yes, and if so it should still be the correct siteSection, so I'd say it's ok!

siteSection = doc.tags.find(t => isSiteSection(t));
}
return `/${uid}`;

const isLandingPage = siteSection === uid;

return isLandingPage || !siteSection ? `/${uid}` : `/${siteSection}/${uid}`;
}

if (isContentType(type)) {
Expand Down
13 changes: 5 additions & 8 deletions content/webapp/pages/events/[eventId]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,11 @@ const EventPage: NextPage<EventProps> = ({
url: '/events',
text: 'Events',
},
...event.series.map(series => {
console.log(series);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just removed this log (I could see some logs in Kibana, unsure which ones they are but thought I'd remove the ones we don't need).

return {
url: linkResolver(series),
text: series.title || '',
prefix: 'Part of',
};
}),
...event.series.map(series => ({
url: linkResolver(series),
text: series.title || '',
prefix: 'Part of',
})),
scheduledIn
? {
url: linkResolver(scheduledIn),
Expand Down
16 changes: 14 additions & 2 deletions content/webapp/pages/pages/[pageId].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,13 @@ export const getServerSideProps: GetServerSideProps<
const { pageId } = context.query;
const siteSection = toMaybeString(context.params?.siteSection);

if (!looksLikePrismicId(pageId)) {
// We don't allow e.g. /visit-us/visit-us as it's a landing page
// Should only display on /visit-us
const isLandingPageRenderingAsSubPage =
pageId === siteSection &&
context.resolvedUrl.indexOf(`/${siteSection}/${pageId}`) === 0;
Comment on lines +113 to +115
Copy link
Contributor Author

Choose a reason for hiding this comment

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

A little bit verbose to my taste, will take ideas on cleverer ways of checking this.


if (!looksLikePrismicId(pageId) || isLandingPageRenderingAsSubPage) {
return { notFound: true };
}

Expand All @@ -123,10 +129,16 @@ export const getServerSideProps: GetServerSideProps<
const basicDocSiteSection = basicDocument.tags.find(t =>
isSiteSection(t)
);
const isLandingPage = basicDocSiteSection === pageId;

const redirectUrl =
isLandingPage || !basicDocSiteSection
? `/${pageId}`
: `/${basicDocSiteSection}/${pageId}`;

return {
redirect: {
destination: `${basicDocSiteSection ? '/' + basicDocSiteSection : ''}/${basicDocument.uid}`,
destination: redirectUrl,
permanent: false,
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,6 @@ describe('transformExhibitionGuide', () => {
it('returns a set of components', () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const exhibition = transformExhibitionGuide(exhibitionGuidesDoc as any);
console.log(exhibition.components.length, 'the exhibition');
expect(exhibition.components.length).toBe(2);
});
});
Expand Down
4 changes: 1 addition & 3 deletions playwright/test/helpers/contexts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ const stageApiToggleCookie = createCookie({
value: 'true',
});

// TODO: context.addCookies should run for the first test of a suite (even on beforeAll/beforeEach)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This TODO is 3 yrs old, and we don't want all scenarios to have cookies added, so removing.


const requiredCookies = useStageApis
export const requiredCookies = useStageApis
? [acceptCookieCookie, stageApiToggleCookie]
: [acceptCookieCookie];

Expand Down
32 changes: 32 additions & 0 deletions playwright/test/pages.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { expect, test } from '@playwright/test';

import { gotoWithoutCache, requiredCookies } from './helpers/contexts';
import { baseUrl } from './helpers/utils';

test.describe('Server-side redirection logic works as expected', () => {
test('/pages/ route redirects to relevant URL', async ({ context, page }) => {
await context.addCookies(requiredCookies);

// Visit Us site section
await gotoWithoutCache(`${baseUrl}/pages/accessibility`, page);
await expect(page.url()).toEqual(`${baseUrl}/visit-us/accessibility`);

// Orphan page
await gotoWithoutCache(`${baseUrl}/pages/contact-us`, page);
await expect(page.url()).toEqual(`${baseUrl}/contact-us`);
});

test("Landing pages don't have a parent route", async ({ context, page }) => {
await context.addCookies(requiredCookies);

await gotoWithoutCache(`${baseUrl}/visit-us/visit-us`, page);
await expect(
page
.getByRole('heading')
.getByText('Not the Wellcome you were expecting?')
).toBeVisible();

await gotoWithoutCache(`${baseUrl}/pages/visit-us`, page);
await expect(page.url()).toEqual(`${baseUrl}/visit-us`);
});
});