Skip to content

Commit

Permalink
perf: dirty flag to avoid redundant head updates
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Sep 10, 2023
1 parent 7bb1064 commit de34c67
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/dom/src/renderDOMHead.ts
Expand Up @@ -149,5 +149,6 @@ export async function renderDOMHead<T extends Unhead<any>>(head: T, options: Ren
// clear all side effects still pending
Object.values(state.pendingSideEffects).forEach(fn => fn())
head._dom = state
head.dirty = false
await head.hooks.callHook('dom:rendered', { renders: tags })
}
4 changes: 4 additions & 0 deletions packages/schema/src/head.ts
Expand Up @@ -123,6 +123,10 @@ export interface Unhead<Input extends {} = Head> {
* @internal
*/
_domUpdatePromise?: Promise<void>
/**
* @internal
*/
dirty: boolean
}

export interface DomState {
Expand Down
6 changes: 5 additions & 1 deletion packages/unhead/src/createHead.ts
Expand Up @@ -61,10 +61,14 @@ export function createHeadCore<T extends {} = Head>(options: CreateHeadOptions =
...(options?.plugins || []),
]

const updated = () => hooks.callHook('entries:updated', head)
const updated = () => {
head.dirty = true
hooks.callHook('entries:updated', head)
}
let entryCount = 0
let entries: HeadEntry<T>[] = []
const head: Unhead<T> = {
dirty: false,
resolvedOptions: options,
hooks,
headEntries() {
Expand Down
2 changes: 2 additions & 0 deletions test/vue/dom/classes.test.ts
Expand Up @@ -40,6 +40,8 @@ describe('vue dom classes', () => {

isNavActive.value = true

// wait 100ms
await new Promise(resolve => setTimeout(resolve, 100))
await renderDOMHead(head, { document: dom.window.document })
expect(dom.serialize()).toMatchInlineSnapshot(`
"<!DOCTYPE html><html><head>
Expand Down

0 comments on commit de34c67

Please sign in to comment.