@unhead/ssr 1.0.21 → 1.1.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const SelfClosingTags = ["meta", "link", "base"];
3
+ const shared = require('@unhead/shared');
4
4
 
5
5
  const propsToString = (props) => {
6
6
  const handledAttributes = [];
@@ -15,10 +15,35 @@ const propsToString = (props) => {
15
15
  return handledAttributes.length > 0 ? ` ${handledAttributes.join(" ")}` : "";
16
16
  };
17
17
 
18
+ function encodeInnerHtml(str) {
19
+ return str.replace(/[&<>"'/]/g, (char) => {
20
+ switch (char) {
21
+ case "&":
22
+ return "&amp;";
23
+ case "<":
24
+ return "&lt;";
25
+ case ">":
26
+ return "&gt;";
27
+ case '"':
28
+ return "&quot;";
29
+ case "'":
30
+ return "&#x27;";
31
+ case "/":
32
+ return "&#x2F;";
33
+ default:
34
+ return char;
35
+ }
36
+ });
37
+ }
18
38
  const tagToString = (tag) => {
19
39
  const attrs = propsToString(tag.props);
20
40
  const openTag = `<${tag.tag}${attrs}>`;
21
- return SelfClosingTags.includes(tag.tag) ? openTag : `${openTag}${tag.children || ""}</${tag.tag}>`;
41
+ if (!shared.TagsWithInnerContent.includes(tag.tag))
42
+ return shared.SelfClosingTags.includes(tag.tag) ? openTag : `${openTag}</${tag.tag}>`;
43
+ let content = tag.innerHTML || "";
44
+ if (tag.textContent)
45
+ content = encodeInnerHtml(tag.textContent);
46
+ return shared.SelfClosingTags.includes(tag.tag) ? openTag : `${openTag}${content}</${tag.tag}>`;
22
47
  };
23
48
 
24
49
  function ssrRenderTags(tags) {
@@ -52,6 +77,20 @@ async function renderSSRHead(head) {
52
77
  };
53
78
  }
54
79
  const ctx = { tags: await head.resolveTags() };
80
+ if (head.resolvedOptions.experimentalHashHydration) {
81
+ ctx.tags.push({
82
+ tag: "meta",
83
+ props: {
84
+ name: "unhead:ssr",
85
+ content: shared.computeHashes(
86
+ ctx.tags.filter((t) => {
87
+ const entry = head.headEntries().find((entry2) => entry2._i === t._e);
88
+ return entry?._m !== "server";
89
+ }).map((tag) => tag._h)
90
+ )
91
+ }
92
+ });
93
+ }
55
94
  await head.hooks.callHook("ssr:render", ctx);
56
95
  const html = ssrRenderTags(ctx.tags);
57
96
  const renderCtx = { tags: ctx.tags, html };
@@ -59,4 +98,8 @@ async function renderSSRHead(head) {
59
98
  return renderCtx.html;
60
99
  }
61
100
 
101
+ exports.encodeInnerHtml = encodeInnerHtml;
102
+ exports.propsToString = propsToString;
62
103
  exports.renderSSRHead = renderSSRHead;
104
+ exports.ssrRenderTags = ssrRenderTags;
105
+ exports.tagToString = tagToString;
package/dist/index.d.ts CHANGED
@@ -1,6 +1,19 @@
1
- import { Unhead, SSRHeadPayload } from '@unhead/schema';
1
+ import { Unhead, SSRHeadPayload, HeadTag } from '@unhead/schema';
2
2
  export { SSRHeadPayload } from '@unhead/schema';
3
3
 
4
4
  declare function renderSSRHead<T extends {}>(head: Unhead<T>): Promise<SSRHeadPayload>;
5
5
 
6
- export { renderSSRHead };
6
+ declare const propsToString: (props: Record<string, any>) => string;
7
+
8
+ declare function encodeInnerHtml(str: string): string;
9
+ declare const tagToString: <T extends HeadTag>(tag: T) => string;
10
+
11
+ declare function ssrRenderTags<T extends HeadTag>(tags: T[]): {
12
+ headTags: string;
13
+ bodyTags: string;
14
+ bodyTagsOpen: string;
15
+ htmlAttrs: string;
16
+ bodyAttrs: string;
17
+ };
18
+
19
+ export { encodeInnerHtml, propsToString, renderSSRHead, ssrRenderTags, tagToString };
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- const SelfClosingTags = ["meta", "link", "base"];
1
+ import { TagsWithInnerContent, SelfClosingTags, computeHashes } from '@unhead/shared';
2
2
 
3
3
  const propsToString = (props) => {
4
4
  const handledAttributes = [];
@@ -13,10 +13,35 @@ const propsToString = (props) => {
13
13
  return handledAttributes.length > 0 ? ` ${handledAttributes.join(" ")}` : "";
14
14
  };
15
15
 
16
+ function encodeInnerHtml(str) {
17
+ return str.replace(/[&<>"'/]/g, (char) => {
18
+ switch (char) {
19
+ case "&":
20
+ return "&amp;";
21
+ case "<":
22
+ return "&lt;";
23
+ case ">":
24
+ return "&gt;";
25
+ case '"':
26
+ return "&quot;";
27
+ case "'":
28
+ return "&#x27;";
29
+ case "/":
30
+ return "&#x2F;";
31
+ default:
32
+ return char;
33
+ }
34
+ });
35
+ }
16
36
  const tagToString = (tag) => {
17
37
  const attrs = propsToString(tag.props);
18
38
  const openTag = `<${tag.tag}${attrs}>`;
19
- return SelfClosingTags.includes(tag.tag) ? openTag : `${openTag}${tag.children || ""}</${tag.tag}>`;
39
+ if (!TagsWithInnerContent.includes(tag.tag))
40
+ return SelfClosingTags.includes(tag.tag) ? openTag : `${openTag}</${tag.tag}>`;
41
+ let content = tag.innerHTML || "";
42
+ if (tag.textContent)
43
+ content = encodeInnerHtml(tag.textContent);
44
+ return SelfClosingTags.includes(tag.tag) ? openTag : `${openTag}${content}</${tag.tag}>`;
20
45
  };
21
46
 
22
47
  function ssrRenderTags(tags) {
@@ -50,6 +75,20 @@ async function renderSSRHead(head) {
50
75
  };
51
76
  }
52
77
  const ctx = { tags: await head.resolveTags() };
78
+ if (head.resolvedOptions.experimentalHashHydration) {
79
+ ctx.tags.push({
80
+ tag: "meta",
81
+ props: {
82
+ name: "unhead:ssr",
83
+ content: computeHashes(
84
+ ctx.tags.filter((t) => {
85
+ const entry = head.headEntries().find((entry2) => entry2._i === t._e);
86
+ return entry?._m !== "server";
87
+ }).map((tag) => tag._h)
88
+ )
89
+ }
90
+ });
91
+ }
53
92
  await head.hooks.callHook("ssr:render", ctx);
54
93
  const html = ssrRenderTags(ctx.tags);
55
94
  const renderCtx = { tags: ctx.tags, html };
@@ -57,4 +96,4 @@ async function renderSSRHead(head) {
57
96
  return renderCtx.html;
58
97
  }
59
98
 
60
- export { renderSSRHead };
99
+ export { encodeInnerHtml, propsToString, renderSSRHead, ssrRenderTags, tagToString };
package/package.json CHANGED
@@ -1,19 +1,19 @@
1
1
  {
2
2
  "name": "@unhead/ssr",
3
3
  "type": "module",
4
- "version": "1.0.21",
5
- "packageManager": "pnpm@7.25.1",
4
+ "version": "1.1.0",
5
+ "packageManager": "pnpm@7.27.1",
6
6
  "author": "Harlan Wilton <harlan@harlanzw.com>",
7
7
  "license": "MIT",
8
8
  "funding": "https://github.com/sponsors/harlan-zw",
9
- "homepage": "https://github.com/harlan-zw/unhead#readme",
9
+ "homepage": "https://unhead.harlanzw.com",
10
10
  "repository": {
11
11
  "type": "git",
12
- "url": "git+https://github.com/harlan-zw/unhead.git",
12
+ "url": "git+https://github.com/unjs/unhead.git",
13
13
  "directory": "packages/ssr"
14
14
  },
15
15
  "bugs": {
16
- "url": "https://github.com/harlan-zw/unhead/issues"
16
+ "url": "https://github.com/unjs/unhead/issues"
17
17
  },
18
18
  "sideEffects": false,
19
19
  "exports": {
@@ -30,10 +30,8 @@
30
30
  "dist"
31
31
  ],
32
32
  "dependencies": {
33
- "@unhead/schema": "1.0.21"
34
- },
35
- "devDependencies": {
36
- "zhead": "^1.1.0"
33
+ "@unhead/schema": "1.1.0",
34
+ "@unhead/shared": "1.1.0"
37
35
  },
38
36
  "scripts": {
39
37
  "build": "unbuild .",