-
-
Notifications
You must be signed in to change notification settings - Fork 736
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
erral
wants to merge
5
commits into
main
Choose a base branch
from
erral-hreflang-links
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+161
−0
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Provide language alternate links @erral |
23 changes: 23 additions & 0 deletions
23
packages/volto/src/components/theme/AlternateHrefLangs/AlternateHrefLangs.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
href={item['@id']} | ||
/> | ||
); | ||
})} | ||
</Helmet> | ||
); | ||
}; | ||
|
||
export { AlternateHrefLangs }; |
135 changes: 135 additions & 0 deletions
135
packages/volto/src/components/theme/AlternateHrefLangs/AlternateHrefLangs.test.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
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.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mmmm, this may be something similar to
class
vsclassName
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.
There was a problem hiding this comment.
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!