Skip to content

Commit 1b43b4b

Browse files
committedFeb 25, 2025
fix(angular): SSR support existing tag replacements
1 parent ce0ac2e commit 1b43b4b

File tree

8 files changed

+32
-34
lines changed

8 files changed

+32
-34
lines changed
 

‎examples/angular/src/app/app.component.ts

-13
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,9 @@ export class AppComponent implements OnInit {
1414
constructor(
1515
) {
1616
useHead({
17-
htmlAttrs: {
18-
lang: 'en',
19-
},
20-
meta: [
21-
{
22-
charset: 'utf-8'
23-
},
24-
{
25-
name: 'viewport',
26-
content: 'width=device-width, initial-scale=1'
27-
}
28-
],
2917
title: 'Hello from Angular 123',
3018
bodyAttrs: {
3119
class: 'foo',
32-
style: 'background-color: salmon'
3320
},
3421
style: [
3522
{

‎examples/angular/src/app/app.config.server.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,19 @@ const serverConfig: ApplicationConfig = {
99
providers: [
1010
provideServerRendering(),
1111
provideServerRoutesConfig(serverRoutes),
12-
provideServerHead(),
12+
provideServerHead({
13+
init: [
14+
{
15+
htmlAttrs: {
16+
lang: 'en-AU',
17+
['data-foo']: true,
18+
style: {
19+
'font-size': '16px'
20+
}
21+
},
22+
}
23+
]
24+
}),
1325
]
1426
};
1527

‎examples/angular/src/app/counter/counter.component.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// counter.component.ts
2-
import {Component, effect, signal} from '@angular/core';
3-
import { Unhead, useHead } from '@unhead/angular'
2+
import {Component, signal} from '@angular/core';
3+
import { useHead } from '@unhead/angular'
44

55
@Component({
66
selector: 'app-counter',

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@
5555
},
5656
"resolutions": {
5757
"@unhead/addons": "workspace:*",
58+
"@unhead/angular": "workspace:*",
5859
"@unhead/dom": "workspace:*",
5960
"@unhead/schema": "workspace:*",
60-
"@unhead/angular": "workspace:*",
6161
"@unhead/schema-org": "workspace:*",
6262
"@unhead/shared": "workspace:*",
6363
"@unhead/ssr": "workspace:*",

‎packages/angular/server/src/server.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
import type { CreateServerHeadOptions } from 'unhead/types'
22
import { makeEnvironmentProviders } from '@angular/core'
33
import { BEFORE_APP_SERIALIZED } from '@angular/platform-server'
4-
import { Unhead, UnheadInjectionToken } from '@unhead/angular'
4+
import { UnheadInjectionToken } from '@unhead/angular'
55
import { createHead as _createServerHead } from 'unhead/server'
6+
import { UnheadSSRService } from './ssr.service'
67

78
export function provideServerHead(options: CreateServerHeadOptions = {}) {
89
const head = _createServerHead(options)
910
return makeEnvironmentProviders([
1011
{ provide: UnheadInjectionToken, useValue: head },
11-
Unhead,
12+
UnheadSSRService,
1213
{
1314
provide: BEFORE_APP_SERIALIZED,
14-
useFactory: (service: Unhead) => () => {
15-
return service._ssrModifyResponse()
15+
useFactory: (service: UnheadSSRService) => () => {
16+
return service.render()
1617
},
17-
deps: [Unhead],
18+
deps: [UnheadSSRService],
1819
multi: true,
1920
},
2021
])

‎packages/angular/src/unhead.service.ts ‎packages/angular/server/src/ssr.service.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { Unhead as UnheadSchema } from 'unhead/types'
22
import { DOCUMENT } from '@angular/common'
33
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'
66

77
function attrToElement(element: HTMLElement, acc: string) {
88
const [key, value] = acc.match(/([a-z0-9-]+)(?:="([^"]*)")?/i)?.slice(1, 3) || []
@@ -36,19 +36,21 @@ const attrRegex = /([a-z0-9-]+(?:="[^"]*")?)/gi
3636
@Injectable({
3737
providedIn: 'root',
3838
})
39-
export class Unhead {
39+
export class UnheadSSRService {
4040
constructor(
4141
@Inject(DOCUMENT) private document: Document,
4242
@Inject(UnheadInjectionToken) private unhead: UnheadSchema,
4343
) {}
4444

45-
async _ssrModifyResponse() {
45+
async render() {
46+
const { input } = extractUnheadInputFromHtml(this.document.documentElement.outerHTML)
47+
this.unhead.entries.set(0, { _i: 0, input, options: {} })
4648
const { headTags, htmlAttrs, bodyAttrs, bodyTags, bodyTagsOpen } = await renderSSRHead(this.unhead, {
4749
omitLineBreaks: false,
4850
})
4951
htmlAttrs.match(attrRegex)?.forEach(attr => attrToElement(this.document.documentElement, attr))
5052
bodyAttrs.match(attrRegex)?.forEach(attr => attrToElement(this.document.body, attr))
5153
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
5355
}
5456
}

‎packages/angular/src/index.ts

-3
This file was deleted.

‎packages/angular/src/public-api.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
* Public API Surface of unhead
33
*/
44

5-
export * from './composables'
6-
export * from './context'
7-
export * from './head.component'
8-
export * from './unhead.service'
5+
export { useHead, useHeadSafe, useScript, useSeoMeta, useUnhead } from './composables'
6+
export { headSymbol, UnheadInjectionToken } from './context'
7+
export { Head } from './head.component'

0 commit comments

Comments
 (0)
Please sign in to comment.