@unhead/ssr 0.1.0 → 0.1.2

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 ADDED
@@ -0,0 +1,52 @@
1
+ 'use strict';
2
+
3
+ const propsToString = (props) => {
4
+ const handledAttributes = [];
5
+ for (const [key, value] of Object.entries(props)) {
6
+ if (value === false || value == null)
7
+ continue;
8
+ let attribute = key;
9
+ if (value !== true)
10
+ attribute += `="${String(value).replace(/"/g, """)}"`;
11
+ handledAttributes.push(attribute);
12
+ }
13
+ return handledAttributes.length > 0 ? ` ${handledAttributes.join(" ")}` : "";
14
+ };
15
+
16
+ const SelfClosingTags = ["meta", "link", "base"];
17
+
18
+ const tagToString = (tag) => {
19
+ const attrs = propsToString(tag.props);
20
+ const openTag = `<${tag.tag}${attrs}>`;
21
+ return SelfClosingTags.includes(tag.tag) ? openTag : `${openTag}${tag.children || ""}</${tag.tag}>`;
22
+ };
23
+
24
+ function ssrRenderTags(tags) {
25
+ const schema = { htmlAttrs: {}, bodyAttrs: {}, tags: { head: [], bodyClose: [], bodyOpen: [] } };
26
+ for (const tag of tags) {
27
+ if (tag.tag === "htmlAttrs" || tag.tag === "bodyAttrs") {
28
+ schema[tag.tag] = { ...schema[tag.tag], ...tag.props };
29
+ continue;
30
+ }
31
+ schema.tags[tag.tagPosition || "head"].push(tagToString(tag));
32
+ }
33
+ return {
34
+ headTags: schema.tags.head.join("\n"),
35
+ bodyTags: schema.tags.bodyClose.join("\n"),
36
+ bodyTagsOpen: schema.tags.bodyOpen.join("\n"),
37
+ htmlAttrs: propsToString(schema.htmlAttrs),
38
+ bodyAttrs: propsToString(schema.bodyAttrs)
39
+ };
40
+ }
41
+
42
+ async function renderSSRHead(ctx) {
43
+ const tags = await ctx.resolveTags();
44
+ const beforeRenderCtx = { tags };
45
+ await ctx.hooks.callHook("ssr:beforeRender", beforeRenderCtx);
46
+ const html = ssrRenderTags(beforeRenderCtx.tags);
47
+ const renderCXx = { tags, html };
48
+ await ctx.hooks.callHook("ssr:render", renderCXx);
49
+ return renderCXx.html;
50
+ }
51
+
52
+ exports.renderSSRHead = renderSSRHead;
package/dist/index.d.ts CHANGED
@@ -1,24 +1,5 @@
1
- import { HeadTag, HookResult, HeadClient } from '@unhead/schema';
2
-
3
- interface SSRHeadPayload {
4
- headTags: string;
5
- bodyTags: string;
6
- bodyTagsOpen: string;
7
- htmlAttrs: string;
8
- bodyAttrs: string;
9
- }
10
- declare module '@unhead/schema' {
11
- interface HeadHooks {
12
- 'ssr:beforeRender': (ctx: {
13
- tags: HeadTag[];
14
- }) => HookResult;
15
- 'ssr:render': (ctx: {
16
- tags: HeadTag[];
17
- html: SSRHeadPayload;
18
- }) => HookResult;
19
- }
20
- }
1
+ import { HeadClient, SSRHeadPayload } from '@unhead/schema';
21
2
 
22
3
  declare function renderSSRHead<T extends HeadClient>(ctx: T): Promise<SSRHeadPayload>;
23
4
 
24
- export { SSRHeadPayload, renderSSRHead };
5
+ export { renderSSRHead };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@unhead/ssr",
3
3
  "type": "module",
4
- "version": "0.1.0",
4
+ "version": "0.1.2",
5
5
  "packageManager": "pnpm@7.14.0",
6
6
  "author": "Harlan Wilton <harlan@harlanzw.com>",
7
7
  "license": "MIT",
@@ -28,7 +28,7 @@
28
28
  "dist"
29
29
  ],
30
30
  "dependencies": {
31
- "@unhead/schema": "0.1.0"
31
+ "@unhead/schema": "0.1.2"
32
32
  },
33
33
  "devDependencies": {
34
34
  "zhead": "1.0.0-beta.4"