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 | /**
|
12 | Utility method that updates the page's open graph and Twitter card metadata.
|
13 | It takes an object as a parameter with the following properties:
|
14 | title | description | url | image.
|
15 |
|
16 | If the `url` is not specified, `window.location.href` will be used; for
|
17 | all other properties, if they aren't specified, then that metadata field will not
|
18 | be set.
|
19 |
|
20 | Example (in your top level element or document, or in the router callback):
|
21 |
|
22 | import { updateMetadata } from 'pwa-helpers/metadata.js';
|
23 |
|
24 | updateMetadata({
|
25 | title: 'My App - view 1',
|
26 | description: 'This is my sample app',
|
27 | url: window.location.href,
|
28 | image: '/assets/view1-hero.png'
|
29 | });
|
30 |
|
31 | */
|
32 | export const updateMetadata = ({
|
33 | title,
|
34 | description,
|
35 | url,
|
36 | image,
|
37 | imageAlt
|
38 | }: {
|
39 | title?: string,
|
40 | description?: string,
|
41 | url?: string,
|
42 | image?: string,
|
43 | imageAlt?: string
|
44 | }) => {
|
45 | if (title) {
|
46 | document.title = title;
|
47 | setMetaTag('property', 'og:title', title);
|
48 | }
|
49 |
|
50 | if (description) {
|
51 | setMetaTag('name', 'description', description);
|
52 | setMetaTag('property', 'og:description', description);
|
53 | }
|
54 |
|
55 | if (image) {
|
56 | setMetaTag('property', 'og:image', image);
|
57 | }
|
58 |
|
59 | if (imageAlt) {
|
60 | setMetaTag('property', 'og:image:alt', imageAlt);
|
61 | }
|
62 |
|
63 | url = url || window.location.href;
|
64 | setMetaTag('property', 'og:url', url);
|
65 | }
|
66 |
|
67 | /**
|
68 | Utility method to create or update the content of a meta tag based on an
|
69 | attribute name/value pair.
|
70 |
|
71 | Example (in your top level element or document, or in the router callback):
|
72 |
|
73 | import { setMetaTag } from 'pwa-helpers/metadata.js';
|
74 |
|
75 | setMetaTag('name', 'twitter:card', 'summary');
|
76 |
|
77 | This would create the following meta tag in the head of the document (or
|
78 | update the content attribute if a meta tag with name="twitter:card" exists):
|
79 |
|
80 | <meta name="twitter:card" content="summary">
|
81 |
|
82 | */
|
83 | export function setMetaTag(attrName:string, attrValue:string, content:string) {
|
84 | let element = document.head!.querySelector(`meta[${attrName}="${attrValue}"]`);
|
85 | if (!element) {
|
86 | element = document.createElement('meta');
|
87 | element.setAttribute(attrName, attrValue);
|
88 | document.head!.appendChild(element);
|
89 | }
|
90 | element.setAttribute('content', content || '');
|
91 | }
|