1 | /**
|
2 | @license
|
3 | Copyright (c) 2018 The Polymer Project Authors. All rights reserved.
|
4 | This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
|
5 | The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
|
6 | The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
|
7 | Code distributed by Google as part of the polymer project is also
|
8 | subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
|
9 | */
|
10 | /**
|
11 | Utility method that updates the page's open graph and Twitter card metadata.
|
12 | It takes an object as a parameter with the following properties:
|
13 | title | description | url | image.
|
14 |
|
15 | If the `url` is not specified, `window.location.href` will be used; for
|
16 | all other properties, if they aren't specified, then that metadata field will not
|
17 | be set.
|
18 |
|
19 | Example (in your top level element or document, or in the router callback):
|
20 |
|
21 | import { updateMetadata } from 'pwa-helpers/metadata.js';
|
22 |
|
23 | updateMetadata({
|
24 | title: 'My App - view 1',
|
25 | description: 'This is my sample app',
|
26 | url: window.location.href,
|
27 | image: '/assets/view1-hero.png'
|
28 | });
|
29 |
|
30 | */
|
31 | export const updateMetadata = ({ title, description, url, image, imageAlt }) => {
|
32 | if (title) {
|
33 | document.title = title;
|
34 | setMetaTag('property', 'og:title', title);
|
35 | }
|
36 | if (description) {
|
37 | setMetaTag('name', 'description', description);
|
38 | setMetaTag('property', 'og:description', description);
|
39 | }
|
40 | if (image) {
|
41 | setMetaTag('property', 'og:image', image);
|
42 | }
|
43 | if (imageAlt) {
|
44 | setMetaTag('property', 'og:image:alt', imageAlt);
|
45 | }
|
46 | url = url || window.location.href;
|
47 | setMetaTag('property', 'og:url', url);
|
48 | };
|
49 | /**
|
50 | Utility method to create or update the content of a meta tag based on an
|
51 | attribute name/value pair.
|
52 |
|
53 | Example (in your top level element or document, or in the router callback):
|
54 |
|
55 | import { setMetaTag } from 'pwa-helpers/metadata.js';
|
56 |
|
57 | setMetaTag('name', 'twitter:card', 'summary');
|
58 |
|
59 | This would create the following meta tag in the head of the document (or
|
60 | update the content attribute if a meta tag with name="twitter:card" exists):
|
61 |
|
62 | <meta name="twitter:card" content="summary">
|
63 |
|
64 | */
|
65 | export function setMetaTag(attrName, attrValue, content) {
|
66 | let element = document.head.querySelector(`meta[${attrName}="${attrValue}"]`);
|
67 | if (!element) {
|
68 | element = document.createElement('meta');
|
69 | element.setAttribute(attrName, attrValue);
|
70 | document.head.appendChild(element);
|
71 | }
|
72 | element.setAttribute('content', content || '');
|
73 | }
|
74 | //# sourceMappingURL=metadata.js.map |
\ | No newline at end of file |