Skip to content

Commit

Permalink
WSTEAM1-1367: Update AMP .co.uk references for canonical functionality (
Browse files Browse the repository at this point in the history
#12061)

* Add F1 canonical URL logic for UK

* Correct capitalisation of variable

* Make prettier happy

* Add another space to make prettier happier

* Check pathname exists

* WSTEAM1-1367: Add list

* WSTEAM1-1367: Add links

* WSTEAM1-1367: Update

* WSTEAM1-1367: Update

---------

Co-authored-by: Jim Johnson-Rollings <[email protected]>
Co-authored-by: Toby Cox <[email protected]>
  • Loading branch information
3 people authored Oct 22, 2024
1 parent 80dbf96 commit 5b64752
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
50 changes: 50 additions & 0 deletions src/app/components/Metadata/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ interface MetadataWithContextProps extends MetadataProps {
pageType: PageTypes;
id?: string | null;
pathname: string;
isUK?: boolean;
}

const MetadataWithContext = ({
Expand All @@ -79,6 +80,7 @@ const MetadataWithContext = ({
mentionsTags,
hasAppleItunesAppBanner,
hasAmpPage,
isUK = false,
}: MetadataWithContextProps) => (
<ServiceContextProvider service={service} pageLang={lang}>
<RequestContextProvider
Expand All @@ -90,6 +92,7 @@ const MetadataWithContext = ({
pathname={pathname}
service={service}
statusCode={200}
isUK={isUK}
>
<MetadataContainer
title={title}
Expand Down Expand Up @@ -208,6 +211,53 @@ it('should render the alternate links for article page', async () => {
});
});

it(`should render the canonical link's top level domain as .co.uk for UK article pages`, async () => {
render(
<MetadataWithContext
service="sport"
bbcOrigin={dotCoDotUKOrigin}
platform="canonical"
id="c0000000001o"
pageType={ARTICLE_PAGE}
pathname="/sport/cricket/articles/c0000000001o"
isUK
{...newsArticleMetadataProps}
/>,
);

await waitFor(() => {
const actual = document
.querySelector('head > link[rel="canonical"]')
?.getAttribute('href');

expect(actual).toEqual(
'https://www.bbc.co.uk/sport/cricket/articles/c0000000001o',
);
});
});

it(`should render the canonical link's top level domain as .com for WS article pages`, async () => {
render(
<MetadataWithContext
service="mundo"
bbcOrigin={dotCoDotUKOrigin}
platform="canonical"
id="c0000000001o"
pageType={ARTICLE_PAGE}
pathname="/mundo/c0000000001o"
{...newsArticleMetadataProps}
/>,
);

await waitFor(() => {
const actual = document
.querySelector('head > link[rel="canonical"]')
?.getAttribute('href');

expect(actual).toEqual('https://www.bbc.com/mundo/c0000000001o');
});
});

it.each`
service | pathName
${'news'} | ${'/news/56427710'}
Expand Down
16 changes: 15 additions & 1 deletion src/app/components/Metadata/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const MetadataContainer = ({
canonicalNonUkLink,
ampNonUkLink,
pathname,
isUK,
} = useContext(RequestContext);

const {
Expand Down Expand Up @@ -102,6 +103,19 @@ const MetadataContainer = ({
},
];

const pathsForUkLink = [
'/sport/formula1',
'/sport/cricket/articles',
'/sport/rugby_union/articles',
'/sport/rugby_league/articles',
];

const isSport = pathsForUkLink.some(
path => pathname && pathname.startsWith(path),
);

const canonicalToUse = isUK && isSport ? canonicalUkLink : canonicalNonUkLink;

const htmlAttributes = {
dir,
lang,
Expand Down Expand Up @@ -134,7 +148,7 @@ const MetadataContainer = ({
content="width=device-width, initial-scale=1, minimum-scale=1"
/>
<title>{pageTitle}</title>
<link rel="canonical" href={canonicalNonUkLink} />
<link rel="canonical" href={canonicalToUse} />
{isEnglishService && alternateLinksEnglishSites.map(renderAlternateLinks)}
{isoLang &&
!isEnglishService &&
Expand Down

0 comments on commit 5b64752

Please sign in to comment.