|
1 | 1 | import type { Unhead as UnheadSchema } from 'unhead/types'
|
2 | 2 | import { DOCUMENT } from '@angular/common'
|
3 | 3 | import { Inject, Injectable } from '@angular/core'
|
4 |
| -import { renderSSRHead } from 'unhead/server' |
5 |
| -import { UnheadInjectionToken } from './context' |
| 4 | +import { UnheadInjectionToken } from '@unhead/angular' |
| 5 | +import { extractUnheadInputFromHtml, renderSSRHead } from 'unhead/server' |
6 | 6 |
|
7 | 7 | function attrToElement(element: HTMLElement, acc: string) {
|
8 | 8 | const [key, value] = acc.match(/([a-z0-9-]+)(?:="([^"]*)")?/i)?.slice(1, 3) || []
|
@@ -36,19 +36,21 @@ const attrRegex = /([a-z0-9-]+(?:="[^"]*")?)/gi
|
36 | 36 | @Injectable({
|
37 | 37 | providedIn: 'root',
|
38 | 38 | })
|
39 |
| -export class Unhead { |
| 39 | +export class UnheadSSRService { |
40 | 40 | constructor(
|
41 | 41 | @Inject(DOCUMENT) private document: Document,
|
42 | 42 | @Inject(UnheadInjectionToken) private unhead: UnheadSchema,
|
43 | 43 | ) {}
|
44 | 44 |
|
45 |
| - async _ssrModifyResponse() { |
| 45 | + async render() { |
| 46 | + const { input } = extractUnheadInputFromHtml(this.document.documentElement.outerHTML) |
| 47 | + this.unhead.entries.set(0, { _i: 0, input, options: {} }) |
46 | 48 | const { headTags, htmlAttrs, bodyAttrs, bodyTags, bodyTagsOpen } = await renderSSRHead(this.unhead, {
|
47 | 49 | omitLineBreaks: false,
|
48 | 50 | })
|
49 | 51 | htmlAttrs.match(attrRegex)?.forEach(attr => attrToElement(this.document.documentElement, attr))
|
50 | 52 | bodyAttrs.match(attrRegex)?.forEach(attr => attrToElement(this.document.body, attr))
|
51 | 53 | this.document.body.innerHTML = bodyTagsOpen + this.document.body.innerHTML + bodyTags
|
52 |
| - this.document.head.innerHTML = headTags + this.document.head.innerHTML |
| 54 | + this.document.head.innerHTML = headTags |
53 | 55 | }
|
54 | 56 | }
|
0 commit comments