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

feat: provide an Alternate language content links #6603

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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 packages/volto/news/6602.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Provide language alternate links @erral
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import config from '@plone/volto/registry';
import Helmet from '@plone/volto/helpers/Helmet/Helmet';

const AlternateHrefLangs = (props) => {
const { content } = props;
return (
<Helmet>
{config.settings.isMultilingual &&
content['@components']?.translations?.items.map((item, key) => {
return (
<link
key={key}
rel="alternate"
hreflang={item.language}
Copy link
Member

Choose a reason for hiding this comment

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

@erral It does work on the SSR but I see this warning in client

Warning: Invalid DOM property `hreflang`. Did you mean `hrefLang`?
    at link
    at head
    at html
    at Html     

if we use hrefLang, I think the browser will handle the conversion. On the server we get it as hrefLang, does it cause issues in SEO ? I think the HTML attributes are case insensitive, the search engines should parse it as hreflang.

Copy link
Member Author

@erral erral Jan 22, 2025

Choose a reason for hiding this comment

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

@erral It does work on the SSR but I see this warning in client

Warning: Invalid DOM property `hreflang`. Did you mean `hrefLang`?
    at link
    at head
    at html
    at Html     

if we use hrefLang, I think the browser will handle the conversion. On the server we get it as hrefLang, does it cause issues in SEO ? I think the HTML attributes are case insensitive, the search engines should parse it as hreflang.

mmmm, this may be something similar to class vs className when rendering the react component.

I need to check this with SSR and the failing tests also, I will tackle this when I have some time later this week.

Copy link
Member

Choose a reason for hiding this comment

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

@nileshgulia1 @erral HTML attributes are not case sensitive, but in React they are!

href={item['@id']}
/>
);
})}
</Helmet>
);
};

export { AlternateHrefLangs };
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import React from 'react';
import Helmet from '@plone/volto/helpers/Helmet/Helmet';

import renderer from 'react-test-renderer';
import configureStore from 'redux-mock-store';
import { Provider } from 'react-intl-redux';
import config from '@plone/volto/registry';

import { AlternateHrefLangs } from './AlternateHrefLangs';

const mockStore = configureStore();

describe('AlternateHrefLangs', () => {
beforeEach(() => {});
it('non multilingual site, renders nothing', () => {
config.settings.isMultilingual = false;
const content = {
'@id': '/',
'@components': {},
};
const store = mockStore({
intl: {
locale: 'en',
messages: {},
},
});
// We need to force the component rendering
// to fill the Helmet
renderer.create(
<Provider store={store}>
<AlternateHrefLangs content={content} />
</Provider>,
);

const helmetLinks = Helmet.peek().linkTags;
expect(helmetLinks.length).toBe(0);
});
it('multilingual site, with some translations', () => {
config.settings.isMultilingual = true;
config.settings.supportedLanguages = ['en', 'es', 'eu'];

const content = {
'@components': {
translations: {
items: [
{ '@id': '/en', language: 'en' },
{ '@id': '/es', language: 'es' },
],
},
},
};

const store = mockStore({
intl: {
locale: 'en',
messages: {},
},
});

// We need to force the component rendering
// to fill the Helmet
renderer.create(
<Provider store={store}>
<>
<AlternateHrefLangs content={content} />
</>
</Provider>,
);
const helmetLinks = Helmet.peek().linkTags;

expect(helmetLinks.length).toBe(2);

expect(helmetLinks).toContainEqual({
rel: 'alternate',
href: '/es',
hreflang: 'es',
});
expect(helmetLinks).toContainEqual({
rel: 'alternate',
href: '/en',
hreflang: 'en',
});
});
it('multilingual site, with all available translations', () => {
config.settings.isMultilingual = true;
config.settings.supportedLanguages = ['en', 'es', 'eu'];
const store = mockStore({
intl: {
locale: 'en',
messages: {},
},
});

const content = {
'@components': {
translations: {
items: [
{ '@id': '/en', language: 'en' },
{ '@id': '/eu', language: 'eu' },
{ '@id': '/es', language: 'es' },
],
},
},
};

// We need to force the component rendering
// to fill the Helmet
renderer.create(
<Provider store={store}>
<AlternateHrefLangs content={content} />
</Provider>,
);

const helmetLinks = Helmet.peek().linkTags;

// We expect having 3 links
expect(helmetLinks.length).toBe(3);

expect(helmetLinks).toContainEqual({
rel: 'alternate',
href: '/eu',
hreflang: 'eu',
});
expect(helmetLinks).toContainEqual({
rel: 'alternate',
href: '/es',
hreflang: 'es',
});
expect(helmetLinks).toContainEqual({
rel: 'alternate',
href: '/en',
hreflang: 'en',
});
});
});
2 changes: 2 additions & 0 deletions packages/volto/src/components/theme/View/View.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import BodyClass from '@plone/volto/helpers/BodyClass/BodyClass';
import { getBaseUrl, flattenToAppURL } from '@plone/volto/helpers/Url/Url';
import { getLayoutFieldname } from '@plone/volto/helpers/Content/Content';
import { hasApiExpander } from '@plone/volto/helpers/Utils/Utils';
import { AlternateHrefLangs } from '@plone/volto/components/theme/AlternateHrefLangs/AlternateHrefLangs';

import config from '@plone/volto/registry';
import SlotRenderer from '../SlotRenderer/SlotRenderer';
Expand Down Expand Up @@ -234,6 +235,7 @@ class View extends Component {
return (
<div id="view" tabIndex="-1">
<ContentMetadataTags content={this.props.content} />
<AlternateHrefLangs content={this.props.content} />
{/* Body class if displayName in component is set */}
<BodyClass
className={
Expand Down
Loading