Skip to content

Commit f9176e9

Browse files
authored
Add canonical / alternate links for mozilla collections (and only those) (#13129)
* Add canonical / alternate links for mozilla collections (and only those) Non-mozilla collections are disallowed in robots.txt
1 parent 4c8e73c commit f9176e9

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

src/amo/pages/Collection/index.js

+9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* @flow */
2+
import config from 'config';
23
import deepEqual from 'deep-eql';
34
import invariant from 'invariant';
45
import * as React from 'react';
@@ -11,6 +12,7 @@ import Button from 'amo/components/Button';
1112
import CollectionAddAddon from 'amo/components/CollectionAddAddon';
1213
import CollectionControls from 'amo/components/CollectionControls';
1314
import CollectionDetailsCard from 'amo/components/CollectionDetailsCard';
15+
import HeadLinks from 'amo/components/HeadLinks';
1416
import NotFoundPage from 'amo/pages/ErrorPages/NotFoundPage';
1517
import Link from 'amo/components/Link';
1618
import Page from 'amo/components/Page';
@@ -586,6 +588,13 @@ export class CollectionBase extends React.Component<InternalProps> {
586588
<meta name="description" content={this.getPageDescription()} />
587589
</Helmet>
588590
)}
591+
{
592+
/* Only include canonical/alternate links if this is a mozilla collection */
593+
collection &&
594+
collection.authorId === config.get('mozillaUserId') && (
595+
<HeadLinks />
596+
)
597+
}
589598

590599
{errorHandler.renderErrorIfPresent()}
591600

tests/unit/amo/pages/TestCollection.js

+45
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
INSTALL_SOURCE_SUGGESTIONS,
2020
MOZILLA_COLLECTIONS_EDIT,
2121
} from 'amo/constants';
22+
import { hrefLangs } from 'amo/languages';
2223
import {
2324
FETCH_CURRENT_COLLECTION,
2425
FETCH_CURRENT_COLLECTION_PAGE,
@@ -63,6 +64,7 @@ import {
6364
fakeI18n,
6465
fakePreview,
6566
getElement,
67+
getElements,
6668
getMockConfig,
6769
renderPage as defaultRender,
6870
screen,
@@ -1147,6 +1149,49 @@ describe(__filename, () => {
11471149
);
11481150
});
11491151

1152+
it('renders canonical and alternate links for mozilla collections', async () => {
1153+
renderWithCollection({
1154+
detailProps: { authorId: mozillaUserId },
1155+
userId: mozillaUserId,
1156+
});
1157+
1158+
const getExpectedURL = (locale, app) => {
1159+
return `${config.get(
1160+
'baseURL',
1161+
)}/${locale}/${app}/collections/${mozillaUserId}/${defaultSlug}/`;
1162+
};
1163+
1164+
await waitFor(() =>
1165+
expect(getElement('link[rel="canonical"]')).toHaveAttribute(
1166+
'href',
1167+
getExpectedURL('en-US', 'firefox'),
1168+
),
1169+
);
1170+
1171+
await waitFor(() =>
1172+
expect(getElement('link[rel="alternate"]')).toBeInTheDocument(),
1173+
);
1174+
1175+
expect(getElements('link[rel="alternate"]')).toHaveLength(hrefLangs.length);
1176+
1177+
const hrefLangsMap = config.get('hrefLangsMap');
1178+
hrefLangs.forEach((hrefLang) => {
1179+
const locale = hrefLangsMap[hrefLang] || hrefLang;
1180+
expect(
1181+
getElement(`link[rel="alternate"][hreflang="${hrefLang}"]`),
1182+
).toHaveAttribute('href', getExpectedURL(locale, 'firefox'));
1183+
});
1184+
});
1185+
1186+
it('does not render canonical and alternate links for non-mozilla collections', async () => {
1187+
renderWithCollection();
1188+
1189+
await waitFor(() =>
1190+
expect(getElement('meta[name="description"]')).toBeInTheDocument(),
1191+
);
1192+
expect(getElements('link[rel="canonical"]')).toHaveLength(0);
1193+
});
1194+
11501195
describe('errorHandler - extractId', () => {
11511196
it('returns a unique ID based on params', () => {
11521197
const props = {

0 commit comments

Comments
 (0)