@quintype/seo 1.40.1 → 1.40.2-ogImgFix.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs.js CHANGED
@@ -367,23 +367,47 @@ function getAttribution(story) {
367
367
  return story["hero-image-attribution"] || story.summary || lodash.get(story, ["alternative", "home", "default", "headline"]) || story.headline;
368
368
  }
369
369
 
370
- function pickImageFromStory(story) {
371
- function getAlternateProperties(type, key) {
372
- return lodash.get(story, ["alternative", `${type}`, "default", "hero-image", `${key}`]);
370
+ /**
371
+ * priority:
372
+ * 1. alternate social image
373
+ * 2. alternate hero image
374
+ * 3. hero image
375
+ * 4. "fallbackSocialImage" from seo config
376
+ * 5. logo_url from /api/v1/config > theme-attributes
377
+ * 5. logo from /api/v1/config > theme-attributes
378
+ * 6. undefined (meta tag won't get created)
379
+ */
380
+ function pickImageFromStory({ story, config, seoConfig }) {
381
+ function getAlt(type, key, fallback) {
382
+ return lodash.get(story, ["alternative", `${type}`, "default", "hero-image", `${key}`], fallback);
373
383
  }
374
384
 
375
- const alternateSocialMetadata = getAlternateProperties("social", "hero-image-metadata");
376
- const alternateHomeMetadata = getAlternateProperties("home", "hero-image-metadata");
377
- const alternateHomeS3Key = getAlternateProperties("home", "hero-image-s3-key");
378
- const alternateSocialS3Key = getAlternateProperties("social", "hero-image-s3-key");
379
-
380
- const socialAlternateHeroImageS3Metadata = (alternateSocialMetadata ? alternateSocialMetadata : alternateHomeMetadata) || story["hero-image-metadata"];
381
-
382
- const socialAlternateHeroImageS3Key = (alternateSocialS3Key ? alternateSocialS3Key : alternateHomeS3Key) || story["hero-image-s3-key"];
383
-
384
385
  const alt = getAttribution(story);
385
-
386
- return { image: new quintypeJs.FocusedImage(socialAlternateHeroImageS3Key, socialAlternateHeroImageS3Metadata || {}), alt };
386
+ const fallbackSocialImage = lodash.get(seoConfig, ["fallbackSocialImage"]);
387
+ const altHeroImg = getAlt("home", "hero-image-s3-key", null);
388
+ const altSocialHeroImg = getAlt("social", "hero-image-s3-key", null);
389
+ const storyHeroImage = lodash.get(story, ["hero-image-s3-key"]);
390
+ const logo_url = lodash.get(config, ["theme-attributes", "logo_url"]);
391
+ const logo = lodash.get(config, ["theme-attributes", "logo"]);
392
+
393
+ if (altSocialHeroImg) {
394
+ const metadata = getAlt("social", "hero-image-metadata", {});
395
+ return { image: new quintypeJs.FocusedImage(altSocialHeroImg, metadata), alt };
396
+ } else if (altHeroImg) {
397
+ const metadata = getAlt("home", "hero-image-metadata", {});
398
+ return { image: new quintypeJs.FocusedImage(altHeroImg, metadata), alt };
399
+ } else if (storyHeroImage) {
400
+ const metadata = lodash.get(story, ["hero-image-metadata"], {});
401
+ return { image: new quintypeJs.FocusedImage(storyHeroImage, metadata), alt };
402
+ } else if (fallbackSocialImage) {
403
+ return { image: fallbackSocialImage, alt, includesHost: true };
404
+ } else if (logo_url) {
405
+ return { image: logo_url, alt, includesHost: true };
406
+ } else if (logo) {
407
+ return { image: logo, alt, includesHost: true };
408
+ } else {
409
+ return { image: undefined, alt: undefined };
410
+ }
387
411
  }
388
412
 
389
413
  function pickImageFromCollection(collection) {
@@ -394,19 +418,19 @@ function pickImageFromCollection(collection) {
394
418
  }
395
419
 
396
420
  // The image is grabbed from the story, else from from the collection
397
- function pickImage(pageType, data, url) {
421
+ function pickImage({ pageType, config, seoConfig, data, url }) {
398
422
  if (pageType === "story-page" && url.query && url.query.cardId) {
399
423
  const story = lodash.get(data, ["data", "story"]) || {};
400
- return pickImageFromCard(story, url.query.cardId) || pickImageFromStory(story);
424
+ return pickImageFromCard(story, url.query.cardId) || pickImageFromStory({ story, seoConfig, config });
401
425
  } else if (pageType === "visual-story" && url.query && url.query.cardId) {
402
426
  const story = lodash.get(data, ["story"]) || {};
403
- return pickImageFromCard(story, url.query.cardId) || pickImageFromStory(story);
427
+ return pickImageFromCard(story, url.query.cardId) || pickImageFromStory({ story, seoConfig, config });
404
428
  } else if (pageType === "story-page" || pageType === "story-page-amp") {
405
429
  const story = lodash.get(data, ["data", "story"]) || {};
406
- return pickImageFromStory(story);
430
+ return pickImageFromStory({ story, seoConfig, config });
407
431
  } else if (pageType === "visual-story") {
408
432
  const story = lodash.get(data, ["story"]) || {};
409
- return pickImageFromStory(story);
433
+ return pickImageFromStory({ story, seoConfig, config });
410
434
  } else if (lodash.get(data, ["data", "collection"])) {
411
435
  return pickImageFromCollection(lodash.get(data, ["data", "collection"]));
412
436
  } else {
@@ -428,7 +452,7 @@ function pickImage(pageType, data, url) {
428
452
  * @param {...*} params See {@link Generator} for other Parameters
429
453
  */
430
454
  function ImageTags(seoConfig, config, pageType, data, { url = {} }) {
431
- const { image, alt } = pickImage(pageType, data, url);
455
+ const { image, alt, includesHost = false } = pickImage({ pageType, data, url, seoConfig, config });
432
456
 
433
457
  if (!image) {
434
458
  return [];
@@ -439,7 +463,7 @@ function ImageTags(seoConfig, config, pageType, data, { url = {} }) {
439
463
  if (seoConfig.enableTwitterCards) {
440
464
  tags.push({
441
465
  name: "twitter:image",
442
- content: `https://${config["cdn-image"]}/${image.path([16, 9], {
466
+ content: includesHost ? image : `https://${config["cdn-image"]}/${image.path([16, 9], {
443
467
  w: 1200,
444
468
  auto: "format,compress",
445
469
  ogImage: true
@@ -451,7 +475,7 @@ function ImageTags(seoConfig, config, pageType, data, { url = {} }) {
451
475
  if (seoConfig.enableOgTags) {
452
476
  tags.push({
453
477
  property: "og:image",
454
- content: `https://${config["cdn-image"]}/${image.path([40, 21], {
478
+ content: includesHost ? image : `https://${config["cdn-image"]}/${image.path([40, 21], {
455
479
  w: 1200,
456
480
  auto: "format,compress",
457
481
  ogImage: true
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quintype/seo",
3
- "version": "1.40.1",
3
+ "version": "1.40.2-ogImgFix.0",
4
4
  "description": "SEO Modules for Quintype",
5
5
  "main": "dist/index.cjs.js",
6
6
  "repository": "https://github.com/quintype/quintype-node-seo",
package/src/image-tags.js CHANGED
@@ -25,25 +25,47 @@ function getAttribution(story) {
25
25
  );
26
26
  }
27
27
 
28
- function pickImageFromStory(story) {
29
- function getAlternateProperties(type, key) {
30
- return get(story, ["alternative", `${type}`, "default", "hero-image", `${key}`]);
28
+ /**
29
+ * priority:
30
+ * 1. alternate social image
31
+ * 2. alternate hero image
32
+ * 3. hero image
33
+ * 4. "fallbackSocialImage" from seo config
34
+ * 5. logo_url from /api/v1/config > theme-attributes
35
+ * 5. logo from /api/v1/config > theme-attributes
36
+ * 6. undefined (meta tag won't get created)
37
+ */
38
+ function pickImageFromStory({ story, config, seoConfig }) {
39
+ function getAlt(type, key, fallback) {
40
+ return get(story, ["alternative", `${type}`, "default", "hero-image", `${key}`], fallback);
31
41
  }
32
42
 
33
- const alternateSocialMetadata = getAlternateProperties("social", "hero-image-metadata");
34
- const alternateHomeMetadata = getAlternateProperties("home", "hero-image-metadata");
35
- const alternateHomeS3Key = getAlternateProperties("home", "hero-image-s3-key");
36
- const alternateSocialS3Key = getAlternateProperties("social", "hero-image-s3-key");
37
-
38
- const socialAlternateHeroImageS3Metadata =
39
- (alternateSocialMetadata ? alternateSocialMetadata : alternateHomeMetadata) || story["hero-image-metadata"];
40
-
41
- const socialAlternateHeroImageS3Key =
42
- (alternateSocialS3Key ? alternateSocialS3Key : alternateHomeS3Key) || story["hero-image-s3-key"];
43
-
44
43
  const alt = getAttribution(story);
45
-
46
- return { image: new FocusedImage(socialAlternateHeroImageS3Key, socialAlternateHeroImageS3Metadata || {}), alt };
44
+ const fallbackSocialImage = get(seoConfig, ["fallbackSocialImage"]);
45
+ const altHeroImg = getAlt("home", "hero-image-s3-key", null);
46
+ const altSocialHeroImg = getAlt("social", "hero-image-s3-key", null);
47
+ const storyHeroImage = get(story, ["hero-image-s3-key"]);
48
+ const logo_url = get(config, ["theme-attributes", "logo_url"]);
49
+ const logo = get(config, ["theme-attributes", "logo"]);
50
+
51
+ if (altSocialHeroImg) {
52
+ const metadata = getAlt("social", "hero-image-metadata", {});
53
+ return { image: new FocusedImage(altSocialHeroImg, metadata), alt };
54
+ } else if (altHeroImg) {
55
+ const metadata = getAlt("home", "hero-image-metadata", {});
56
+ return { image: new FocusedImage(altHeroImg, metadata), alt };
57
+ } else if (storyHeroImage) {
58
+ const metadata = get(story, ["hero-image-metadata"], {});
59
+ return { image: new FocusedImage(storyHeroImage, metadata), alt };
60
+ } else if (fallbackSocialImage) {
61
+ return { image: fallbackSocialImage, alt, includesHost: true };
62
+ } else if (logo_url) {
63
+ return { image: logo_url, alt, includesHost: true };
64
+ } else if (logo) {
65
+ return { image: logo, alt, includesHost: true };
66
+ } else {
67
+ return { image: undefined, alt: undefined };
68
+ }
47
69
  }
48
70
 
49
71
  function pickImageFromCollection(collection) {
@@ -54,19 +76,19 @@ function pickImageFromCollection(collection) {
54
76
  }
55
77
 
56
78
  // The image is grabbed from the story, else from from the collection
57
- function pickImage(pageType, data, url) {
79
+ function pickImage({ pageType, config, seoConfig, data, url }) {
58
80
  if (pageType === "story-page" && url.query && url.query.cardId) {
59
81
  const story = get(data, ["data", "story"]) || {};
60
- return pickImageFromCard(story, url.query.cardId) || pickImageFromStory(story);
82
+ return pickImageFromCard(story, url.query.cardId) || pickImageFromStory({ story, seoConfig, config });
61
83
  } else if (pageType === "visual-story" && url.query && url.query.cardId) {
62
84
  const story = get(data, ["story"]) || {};
63
- return pickImageFromCard(story, url.query.cardId) || pickImageFromStory(story);
85
+ return pickImageFromCard(story, url.query.cardId) || pickImageFromStory({ story, seoConfig, config });
64
86
  } else if (pageType === "story-page" || pageType === "story-page-amp") {
65
87
  const story = get(data, ["data", "story"]) || {};
66
- return pickImageFromStory(story);
88
+ return pickImageFromStory({ story, seoConfig, config });
67
89
  } else if (pageType === "visual-story") {
68
90
  const story = get(data, ["story"]) || {};
69
- return pickImageFromStory(story);
91
+ return pickImageFromStory({ story, seoConfig, config });
70
92
  } else if (get(data, ["data", "collection"])) {
71
93
  return pickImageFromCollection(get(data, ["data", "collection"]));
72
94
  } else {
@@ -88,7 +110,7 @@ function pickImage(pageType, data, url) {
88
110
  * @param {...*} params See {@link Generator} for other Parameters
89
111
  */
90
112
  export function ImageTags(seoConfig, config, pageType, data, { url = {} }) {
91
- const { image, alt } = pickImage(pageType, data, url);
113
+ const { image, alt, includesHost = false } = pickImage({ pageType, data, url, seoConfig, config });
92
114
 
93
115
  if (!image) {
94
116
  return [];
@@ -99,11 +121,13 @@ export function ImageTags(seoConfig, config, pageType, data, { url = {} }) {
99
121
  if (seoConfig.enableTwitterCards) {
100
122
  tags.push({
101
123
  name: "twitter:image",
102
- content: `https://${config["cdn-image"]}/${image.path([16, 9], {
103
- w: 1200,
104
- auto: "format,compress",
105
- ogImage: true,
106
- })}`,
124
+ content: includesHost
125
+ ? image
126
+ : `https://${config["cdn-image"]}/${image.path([16, 9], {
127
+ w: 1200,
128
+ auto: "format,compress",
129
+ ogImage: true,
130
+ })}`,
107
131
  });
108
132
  alt && tags.push({ property: "twitter:image:alt", content: alt });
109
133
  }
@@ -111,11 +135,13 @@ export function ImageTags(seoConfig, config, pageType, data, { url = {} }) {
111
135
  if (seoConfig.enableOgTags) {
112
136
  tags.push({
113
137
  property: "og:image",
114
- content: `https://${config["cdn-image"]}/${image.path([40, 21], {
115
- w: 1200,
116
- auto: "format,compress",
117
- ogImage: true,
118
- })}`,
138
+ content: includesHost
139
+ ? image
140
+ : `https://${config["cdn-image"]}/${image.path([40, 21], {
141
+ w: 1200,
142
+ auto: "format,compress",
143
+ ogImage: true,
144
+ })}`,
119
145
  });
120
146
  tags.push({ property: "og:image:width", content: 1200 });
121
147
  if (get(image, ["metadata", "focus-point"])) {