@webround/core 0.3.9 → 0.3.10

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.
Files changed (2) hide show
  1. package/dist/seo-react.js +33 -4
  2. package/package.json +1 -1
package/dist/seo-react.js CHANGED
@@ -1,8 +1,23 @@
1
1
  import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
2
- import { useEffect, useSyncExternalStore } from 'react';
2
+ import { useEffect, useRef, useSyncExternalStore } from 'react';
3
3
  import { wrSeoPageOf, wrSeoTags } from './seo.js';
4
4
  import { wrBuildLangPath } from './routing.js';
5
5
  const PROP = { hreflang: 'hrefLang' };
6
+ // What marks a tag as this component's: the `<title>` the placeholder of `index.html` is told from, and the tags a snapshot was saved with
7
+ const SEO_MARK = 'data-wr-seo';
8
+ // What a tag is, apart from what it says: the title, a `<meta>` by its name or property, a `<link>` by its `rel` (and language). Structured data has no name: it is told by what it says (null)
9
+ function tagKey(tag) {
10
+ const name = tag.tagName.toLowerCase();
11
+ if (name === 'title')
12
+ return 'title';
13
+ if (name === 'meta') {
14
+ const key = tag.getAttribute('name') ?? tag.getAttribute('property');
15
+ return key ? `meta:${key}` : null;
16
+ }
17
+ if (name === 'link')
18
+ return `link:${tag.getAttribute('rel')}:${tag.getAttribute('hreflang') ?? ''}`;
19
+ return null;
20
+ }
6
21
  // What a page whose texts come from data (a product) says about itself, on top of what its JSON says. One page is shown at a time, so it is one value: the page sets it while it is shown, `WrSeoHead` reads it.
7
22
  let dynamicSeo;
8
23
  const listeners = new Set();
@@ -33,10 +48,24 @@ export function WrSeoHead({ site, config, pathname, lang, langs }) {
33
48
  if (title)
34
49
  document.head.querySelectorAll('title:not([data-wr-seo])').forEach((other) => other.remove());
35
50
  }, [title]);
36
- return (_jsxs(_Fragment, { children: [title && _jsx("title", { "data-wr-seo": "", children: title }), tags.map(({ tag, attrs }) => {
51
+ // The page came with these tags in its head (they carry `data-wr-seo`): a snapshot saved them, and the Worker adds a product's. The app draws its own when it starts, and the ones it draws are the ones it
52
+ // keeps up to date, so a tag that came with the page goes as soon as the app draws the one with the same name. Until then (a product's data has not come) it stays: it is the best the page has said.
53
+ const saved = useRef(null);
54
+ saved.current ?? (saved.current = typeof document === 'undefined' ? [] : [...document.head.querySelectorAll(`[${SEO_MARK}]`)]);
55
+ const drawn = JSON.stringify({ title, tags, jsonLd });
56
+ useEffect(() => {
57
+ const mine = [...document.head.querySelectorAll(`[${SEO_MARK}]`)].filter((tag) => !saved.current?.includes(tag));
58
+ for (const tag of saved.current ?? []) {
59
+ const key = tagKey(tag);
60
+ if (mine.some((other) => (key ? tagKey(other) === key : other.isEqualNode(tag))))
61
+ tag.remove();
62
+ }
63
+ saved.current = saved.current?.filter((tag) => tag.isConnected) ?? [];
64
+ }, [drawn]);
65
+ return (_jsxs(_Fragment, { children: [title && _jsx("title", { [SEO_MARK]: '', children: title }), tags.map(({ tag, attrs }) => {
37
66
  const props = Object.fromEntries(Object.entries(attrs).map(([key, value]) => [PROP[key] ?? key, value]));
38
67
  const key = `${tag}:${attrs.name ?? attrs.property ?? `${attrs.rel}:${attrs.hreflang ?? ''}`}`;
39
- return tag === 'meta' ? _jsx("meta", { ...props }, key) : _jsx("link", { ...props }, key);
40
- }), jsonLd.map((body, index) => _jsx("script", { type: "application/ld+json", dangerouslySetInnerHTML: { __html: body } }, `ld:${index}`))] }));
68
+ return tag === 'meta' ? _jsx("meta", { [SEO_MARK]: '', ...props }, key) : _jsx("link", { [SEO_MARK]: '', ...props }, key);
69
+ }), jsonLd.map((body, index) => _jsx("script", { type: "application/ld+json", [SEO_MARK]: '', dangerouslySetInnerHTML: { __html: body } }, `ld:${index}`))] }));
41
70
  }
42
71
  //# sourceMappingURL=seo-react.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webround/core",
3
- "version": "0.3.9",
3
+ "version": "0.3.10",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",