This repository has been archived by the owner on Oct 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(metatags): added metatags support and example in Article's page
- Loading branch information
Showing
5 changed files
with
139 additions
and
19 deletions.
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
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,8 @@ | ||
# Metatags | ||
|
||
Use Metatags from Drupal on pages. | ||
|
||
**Requires to install:** | ||
|
||
- [Drupal Metatag module](https://www.drupal.org/project/metatag) | ||
- [Drupal GraphQL Metatag module](https://www.drupal.org/project/graphql_metatag) |
95 changes: 95 additions & 0 deletions
95
src/client/modules/drupal/modules/metatags/containers/Metatags/index.js
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,95 @@ | ||
import React from 'react' | ||
import { Query } from 'react-apollo' | ||
import { array, string } from 'prop-types' | ||
import { | ||
pipe, | ||
find, | ||
propEq, | ||
map, | ||
propOr, | ||
lensProp, | ||
allPass, | ||
path, | ||
over, | ||
replace, | ||
when | ||
} from 'ramda' | ||
import Head from 'next/head' | ||
import env from '~source/env' | ||
|
||
import query from './query.gql' | ||
|
||
export const META_PROPERTY = 'MetaProperty' | ||
const META_VALUE = 'MetaValue' | ||
export const META_LINK = 'MetaLink' | ||
|
||
const onlyMetaProperty = propEq('__typename', META_PROPERTY) | ||
const onlyMetaValue = propEq('__typename', META_VALUE) | ||
const onlyMetaLink = allPass([ | ||
propEq('__typename', META_LINK), | ||
propEq('key', 'canonical') | ||
]) | ||
|
||
const setAppHost = when( | ||
onlyMetaLink, | ||
over( | ||
lensProp('value'), | ||
replace(/^https?:\/\/[^#?/]+/, env('APP_HOST', 'http://localhost:3000')) | ||
) | ||
) | ||
|
||
const normalizeMetatags = pipe( | ||
path(['route', 'entity', 'entityMetatags']), | ||
map(setAppHost) | ||
) | ||
|
||
const FALLBACK_TITLE = 'Next on Drupal' | ||
const getTitle = pipe( | ||
find(propEq('key', 'title')), | ||
propOr(FALLBACK_TITLE, 'value') | ||
) | ||
|
||
const Metatags = ({ metatags }) => { | ||
const pageTitle = getTitle(metatags) | ||
|
||
return ( | ||
<Head> | ||
{ metatags.filter(onlyMetaLink).map(({ key, value }) => ( | ||
<link rel={ key } key={ key } href={ value } /> | ||
)) } | ||
|
||
{ metatags.filter(onlyMetaProperty).map(({ key, value }) => ( | ||
<meta key={ key } property={ key } content={ value } /> | ||
)) } | ||
|
||
{ metatags.filter(onlyMetaValue).map(({ key, value }) => ( | ||
<meta key={ key } name={ key } content={ value } /> | ||
)) } | ||
|
||
{ pageTitle && <title key='title'>{ pageTitle }</title> } | ||
</Head> | ||
) | ||
} | ||
|
||
Metatags.propTypes = { | ||
metatags: array | ||
} | ||
|
||
const MetatagsContainer = ({ path: urlPath }) => ( | ||
<Query query={ query } variables={ { path: urlPath } }> | ||
{ ({ error, data, loading }) => { | ||
if (error) throw error | ||
if (loading) return null | ||
|
||
const metatags = normalizeMetatags(data) | ||
|
||
return <Metatags metatags={ metatags } /> | ||
} } | ||
</Query> | ||
) | ||
|
||
MetatagsContainer.propTypes = { | ||
path: string.isRequired | ||
} | ||
|
||
export default MetatagsContainer |
12 changes: 12 additions & 0 deletions
12
src/client/modules/drupal/modules/metatags/containers/Metatags/query.gql
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,12 @@ | ||
query CurrentPageMetatags($path: String!) { | ||
route(path: $path) { | ||
... on EntityCanonicalUrl { | ||
entity { | ||
entityMetatags { | ||
key | ||
value | ||
} | ||
} | ||
} | ||
} | ||
} |
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