react-metatags-hook
Advanced tools
Comparing version 1.2.0 to 1.3.0
@@ -0,1 +1,8 @@ | ||
## [1.3.0] - 2020-04-12 | ||
### Added | ||
* Added resetMetaTags function to clear tags definitions stored in the library. | ||
### Changed | ||
* Fixed how meta tags definitions are merged (only defined values overwrites previously set values). | ||
* Upgraded all dependencies. | ||
## [1.2.0] - 2020-04-12 | ||
@@ -2,0 +9,0 @@ ### Changed |
@@ -15,3 +15,3 @@ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("react"),e=function(){return(e=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var u in e=arguments[n])Object.prototype.hasOwnProperty.call(e,u)&&(t[u]=e[u]);return t}).apply(this,arguments)}; | ||
and limitations under the License. | ||
***************************************************************************** */function n(){for(var t=0,e=0,n=arguments.length;e<n;e++)t+=arguments[e].length;var r=Array(t),u=0;for(e=0;e<n;e++)for(var a=arguments[e],o=0,i=a.length;o<i;o++,u++)r[u]=a[o];return r}var r,u,a={meta:["charset","name","property","http-equiv"],link:["rel","sizes"]},o=function(t){return function(e){var n=a[t].concat("id").map((function(t){return e.hasOwnProperty(t)?{key:t,value:e[t]}:void 0})).filter(Boolean),r=Object.keys(e).map((function(t){return{key:t,value:e[t]}}));return{tag:t,query:n.length?n:r,attributes:e}}},i=o("meta"),c=o("link"),f=new Set,s=new Set,l=function(t){return Array.from(t).map((function(t){return t.current})).reduce((function(t,n){return e(e(e({},t),n),{tags:e(e({},t.tags),n.tags)})}),{tags:{}})},d=function(t){s.forEach((function(e){return e(t)}))},p=function(t){return Object.keys(t.tags||{}).map((function(e){return t.tags[e]}))},m=function(t,e){var n=e.reduce((function(t,e){var n=e.key,r=e.value;return t+"["+n+(r?'="'+r+'"':"")+"]"}),"");return document.head.querySelector(t+n)},y=function(t){var e,n,r=t.tag,u=t.query,a=t.attributes,o=m(r,u)||(e=r,n=document.createElement(e),document.head.appendChild(n),n);a&&Object.keys(a).forEach((function(t){o.setAttribute(t,a[t]||"")}))},v=function(t){var e=t.tag,n=t.query,r=m(e,n);r&&document.head.removeChild(r)},g={tags:{}},h=function(t,e){"undefined"!=typeof window&&(window.clearTimeout(r),r=setTimeout((function(){var e,n,r=function(t,e){return Object.keys(e.tags||{}).filter((function(e){return!t.tags[e]})).map((function(t){return e.tags[t]}))}(t,g),u=p(t);g=t,t.title&&(e=t.title,document.title=e),t.lang&&(n=t.lang,document.documentElement.lang=n),u.forEach(y),r.forEach(v)}),e))};u=function(t){return h(t,50)},s.add(u);exports.default=function(r,u){var a=t.useRef(),o=t.useMemo((function(){return function(t){var r=t.title,u=t.description,a=t.lang,o=t.charset,f=t.metas,s=void 0===f?[]:f,l=t.links,d=void 0===l?[]:l,p=t.openGraph,m=void 0===p?{}:p,y=t.twitter,v=void 0===y?{}:y;return{title:r,lang:a,tags:n([!!u&&{tag:"meta",query:[{key:"name",value:"description"}],attributes:{name:"description",content:u}},!!o&&{tag:"meta",query:[{key:"charset"}],attributes:{charset:o}}],s.map(i),d.map(c),Object.keys(m).map((function(t){return i({property:"og:"+t,content:m[t]})})),Object.keys(v).map((function(t){return i({property:"twitter:"+t,content:v[t]})}))).filter((function(t){return t&&t.query&&Object.keys(t.query).length})).reduce((function(t,n){var r,u=n.query.map((function(t){var e=t.key,n=void 0===e?"":e,r=t.value;return n+"="+(void 0===r?"":r)})).join("~"),a=n.tag+"_"+u;return e(e({},t),((r={})[a]=n,r))}),{})}}(r)}),u);t.useEffect((function(){return function(){a.current&&function(t){f.delete(t),d(l(f))}(a)}}),[]),a.current!==o&&(a.current=o,a.current&&function(t){!f.has(t)&&f.add(t),d(l(f))}(a))},exports.generateStaticHtml=function(){return function(t){return n([t.title?"<title>"+t.title+"</title>":""],p(t).map((function(t){var e=t.attributes;return"<"+t.tag+" "+Object.keys(e).map((function(t){return""+t+(e[t]?'="'+e[t]+'"':"")})).join(" ")+" />"}))).join("")}(l(f))}; | ||
***************************************************************************** */function n(){for(var t=0,e=0,n=arguments.length;e<n;e++)t+=arguments[e].length;var r=Array(t),u=0;for(e=0;e<n;e++)for(var a=arguments[e],i=0,o=a.length;i<o;i++,u++)r[u]=a[i];return r}var r,u,a={meta:["charset","name","property","http-equiv"],link:["rel","sizes"]},i=function(t){return function(e){var n=a[t].concat("id").map((function(t){return e.hasOwnProperty(t)?{key:t,value:e[t]}:void 0})).filter(Boolean),r=Object.keys(e).map((function(t){return{key:t,value:e[t]}}));return{tag:t,query:n.length?n:r,attributes:e}}},o=i("meta"),c=i("link"),f=new Set,l=new Set,s=function(t){return Array.from(t).map((function(t){return t.current})).reduce((function(t,n){var r,u;return{title:null!==(r=n.title)&&void 0!==r?r:t.title,lang:null!==(u=n.lang)&&void 0!==u?u:t.lang,tags:e(e({},t.tags),n.tags)}}),{tags:{}})},d=function(t){l.forEach((function(e){return e(t)}))},p=function(t){return Object.keys(t.tags||{}).map((function(e){return t.tags[e]}))},g=function(t,e){var n=e.reduce((function(t,e){var n=e.key,r=e.value;return t+"["+n+(r?'="'+r+'"':"")+"]"}),"");return document.head.querySelector(t+n)},v=function(t){var e,n,r=t.tag,u=t.query,a=t.attributes,i=g(r,u)||(e=r,n=document.createElement(e),document.head.appendChild(n),n);a&&Object.keys(a).forEach((function(t){i.setAttribute(t,a[t]||"")}))},m=function(t){var e=t.tag,n=t.query,r=g(e,n);r&&document.head.removeChild(r)},y={tags:{}},h=function(t,e){"undefined"!=typeof window&&(window.clearTimeout(r),r=setTimeout((function(){var e,n,r=function(t,e){return Object.keys(e.tags||{}).filter((function(e){return!t.tags[e]})).map((function(t){return e.tags[t]}))}(t,y),u=p(t);y=t,t.title&&(e=t.title,document.title=e),t.lang&&(n=t.lang,document.documentElement.lang=n),u.forEach(v),r.forEach(m)}),e))};u=function(t){return h(t,50)},l.add(u);exports.default=function(r,u){var a=t.useRef(),i=t.useMemo((function(){return function(t){var r=t.title,u=t.description,a=t.lang,i=t.charset,f=t.metas,l=void 0===f?[]:f,s=t.links,d=void 0===s?[]:s,p=t.openGraph,g=void 0===p?{}:p,v=t.twitter,m=void 0===v?{}:v;return{title:r,lang:a,tags:n([!!u&&{tag:"meta",query:[{key:"name",value:"description"}],attributes:{name:"description",content:u}},!!i&&{tag:"meta",query:[{key:"charset"}],attributes:{charset:i}}],l.map(o),d.map(c),Object.keys(g).map((function(t){return o({property:"og:"+t,content:g[t]})})),Object.keys(m).map((function(t){return o({property:"twitter:"+t,content:m[t]})}))).filter((function(t){return t&&t.query&&Object.keys(t.query).length})).reduce((function(t,n){var r,u=n.query.map((function(t){var e=t.key,n=void 0===e?"":e,r=t.value;return n+"="+(void 0===r?"":r)})).join("~"),a=n.tag+"_"+u;return e(e({},t),((r={})[a]=n,r))}),{})}}(r)}),u);t.useEffect((function(){return function(){a.current&&function(t){f.delete(t),d(s(f))}(a)}}),[]),a.current!==i&&(a.current=i,a.current&&function(t){!f.has(t)&&f.add(t),d(s(f))}(a))},exports.generateStaticHtml=function(){return function(t){return n([t.title?"<title>"+t.title+"</title>":""],p(t).map((function(t){var e=t.attributes;return"<"+t.tag+" "+Object.keys(e).map((function(t){return""+t+(e[t]?'="'+e[t]+'"':"")})).join(" ")+" />"}))).join("")}(s(f))},exports.resetMetaTags=function(){f.clear(),d(s(f))}; | ||
//# sourceMappingURL=index.cjs.js.map |
@@ -1,3 +0,2 @@ | ||
import useMetaTags, { generateStaticHtml } from './use-meta-tags'; | ||
export default useMetaTags; | ||
export { generateStaticHtml }; | ||
export { default } from './use-meta-tags'; | ||
export { generateStaticHtml, resetMetaTags } from './generate-static-html'; |
import { MetaTagsConfig } from './types'; | ||
declare const generateStaticHtml: () => string; | ||
declare const useMetaTags: (config: MetaTagsConfig, dependsOn?: any[] | undefined) => void; | ||
export default useMetaTags; | ||
export { generateStaticHtml }; |
{ | ||
"name": "react-metatags-hook", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "React Hook to manage html meta tags", | ||
@@ -40,10 +40,12 @@ "main": "./dist/index.cjs.js", | ||
"@types/react": "^16.9.34", | ||
"concurrently": "^5.1.0", | ||
"jest": "^25.3.0", | ||
"majestic": "^1.6.2", | ||
"@types/react-dom": "^16.9.7", | ||
"concurrently": "^5.2.0", | ||
"jest": "^26.0.1", | ||
"majestic": "^1.7.0", | ||
"pre-commit": "^1.2.2", | ||
"react": "^16.13.1", | ||
"react-dom": "^16.13.1", | ||
"react-hooks-testing-library": "^0.6.0", | ||
"react-test-renderer": "^16.13.1", | ||
"rollup": "^2.6.0", | ||
"rollup": "^2.8.2", | ||
"rollup-plugin-clear": "^2.0.7", | ||
@@ -53,9 +55,9 @@ "rollup-plugin-commonjs": "^10.1.0", | ||
"rollup-plugin-node-resolve": "^5.2.0", | ||
"rollup-plugin-sourcemaps": "^0.5.0", | ||
"rollup-plugin-sourcemaps": "^0.6.2", | ||
"rollup-plugin-terser": "^5.3.0", | ||
"rollup-plugin-typescript2": "^0.27.0", | ||
"ts-jest": "^25.3.1", | ||
"tslint": "^6.1.1", | ||
"ts-jest": "^25.5.0", | ||
"tslint": "^6.1.2", | ||
"tslint-config-standard": "^9.0.0", | ||
"tslint-react": "^4.2.0", | ||
"tslint-react": "^5.0.0", | ||
"tslint-react-hooks": "^2.2.2", | ||
@@ -62,0 +64,0 @@ "typescript": "^3.8.3" |
@@ -174,5 +174,19 @@ ![npm](https://img.shields.io/npm/v/react-metatags-hook) | ||
import { generateStaticHtml } from 'react-metatags-hook' | ||
... | ||
ReactDOMServer.renderToString(<App />); | ||
ReactDOMServer.renderToString(<App />) | ||
const metaHTML = generateStaticHtml() | ||
``` | ||
Is important to point out that the meta tags definitions are stored inside the library instance. This means that when different components are rendered into string in the same application instance (i.e. node server), the meta tags of the second render will add up to the definitions generated by the first render. | ||
If this behavior is not the desired one, you can reset the meta tags definitions stored in the library instance using the `resetMetaTags` function. | ||
```javascript | ||
import { generateStaticHtml, resetMetaTags } from 'react-metatags-hook' | ||
ReactDOMServer.renderToString(<App />) | ||
const metaHTML = generateStaticHtml() | ||
resetMetaTags() | ||
ReactDOMServer.renderToString(<AnotherApp />) | ||
const anotherMetaHTML = generateStaticHtml() | ||
``` |
Sorry, the diff of this file is not supported yet
42054
25
192
25