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

Include canonical <link> for user review pages #13127

Merged
merged 4 commits into from
Jul 25, 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
1 change: 1 addition & 0 deletions src/amo/components/RatingsByStar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ export class RatingsByStarBase extends React.Component<InternalProps> {
<Link
className="RatingsByStar-row"
key={star}
rel="nofollow"
title={getLinkTitles(star, starCount) || ''}
to={reviewListURL({
addonSlug: addon.slug,
Expand Down
2 changes: 2 additions & 0 deletions src/amo/pages/AddonReviewList/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { DEFAULT_API_PAGE_SIZE } from 'amo/api';
import AddonReviewCard from 'amo/components/AddonReviewCard';
import AddonSummaryCard from 'amo/components/AddonSummaryCard';
import FeaturedAddonReview from 'amo/components/FeaturedAddonReview';
import HeadLinks from 'amo/components/HeadLinks';
import Page from 'amo/components/Page';
import { fetchReviewPermissions, fetchReviews } from 'amo/actions/reviews';
import { setViewContext } from 'amo/actions/viewContext';
Expand Down Expand Up @@ -332,6 +333,7 @@ export class AddonReviewListBase extends React.Component<InternalProps> {
{reviewId && <meta name="robots" content="noindex, follow" />}
</Helmet>
)}
{addon && !reviewId && <HeadLinks />}

{errorHandler.renderErrorIfPresent()}

Expand Down
1 change: 1 addition & 0 deletions tests/unit/amo/components/TestRatingsByStar.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ describe(__filename, () => {
'href',
`/en-US/android${reviewListURL({ addonSlug: addon.slug, score })}`,
);
expect(link).toHaveAttribute('rel', 'nofollow');
expect(within(link).getByText(count)).toBeInTheDocument();
}

Expand Down
52 changes: 52 additions & 0 deletions tests/unit/amo/pages/TestAddonReviewList.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import config from 'config';
import { waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';

Expand All @@ -17,6 +18,7 @@ import {
CLIENT_APP_FIREFOX,
SET_VIEW_CONTEXT,
} from 'amo/constants';
import { hrefLangs } from 'amo/languages';
import { fetchAddon, loadAddon } from 'amo/reducers/addons';
import {
changeLocation,
Expand Down Expand Up @@ -700,6 +702,56 @@ describe(__filename, () => {
expect(getElements('meta[name="robots"]')).toHaveLength(0);
});

it('renders a canonical link for the list page with alternates', async () => {
renderWithAddonAndReviews();

const getExpectedURL = (locale, app) => {
return `${config.get(
'baseURL',
)}/${locale}/${app}/addon/${defaultSlug}/reviews/`;
};

await waitFor(() =>
expect(getElement('link[rel="canonical"]')).toHaveAttribute(
'href',
getExpectedURL('en-US', 'firefox'),
),
);

await waitFor(() =>
expect(getElement('link[rel="alternate"]')).toBeInTheDocument(),
);

expect(getElements('link[rel="alternate"]')).toHaveLength(
hrefLangs.length,
);

const hrefLangsMap = config.get('hrefLangsMap');
hrefLangs.forEach((hrefLang) => {
const locale = hrefLangsMap[hrefLang] || hrefLang;
expect(
getElement(`link[rel="alternate"][hreflang="${hrefLang}"]`),
).toHaveAttribute('href', getExpectedURL(locale, 'firefox'));
});
});

it('renders a canonical with no query string', async () => {
renderWithAddonAndReviews({ score: 5 });

const getExpectedURL = (locale, app) => {
return `${config.get(
'baseURL',
)}/${locale}/${app}/addon/${defaultSlug}/reviews/`;
};

await waitFor(() =>
expect(getElement('link[rel="canonical"]')).toHaveAttribute(
'href',
getExpectedURL('en-US', 'firefox'),
),
);
});

it('renders a robots meta tag when there is a featured review', async () => {
const reviews = [
{ ...fakeReview, id: 1, score: 1 },
Expand Down