Skip to content

Commit

Permalink
perf: resolve tags after dom:beforeRender
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Sep 10, 2023
1 parent 07f1e4b commit 7bb1064
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
15 changes: 7 additions & 8 deletions packages/dom/src/renderDOMHead.ts
Expand Up @@ -36,19 +36,18 @@ export async function renderDOMHead<T extends Unhead<any>>(head: T, options: Ren
if (!dom)
return

const tags: DomRenderTagContext[] = (await head.resolveTags())
.map(tag => <DomRenderTagContext> {
tag,
id: HasElementTags.includes(tag.tag) ? hashTag(tag) : tag.tag,
shouldRender: true,
})

const beforeRenderCtx: DomBeforeRenderCtx = { shouldRender: true, tags }
const beforeRenderCtx: DomBeforeRenderCtx = { shouldRender: head.dirty, tags: [] }
await head.hooks.callHook('dom:beforeRender', beforeRenderCtx)
// allow integrations to block to the render
if (!beforeRenderCtx.shouldRender)
return

const tags = (await head.resolveTags())
.map(tag => <DomRenderTagContext> {
tag,
id: HasElementTags.includes(tag.tag) ? hashTag(tag) : tag.tag,
shouldRender: true,
})
let state = head._dom as DomState
// let's hydrate - fill the elMap for fast lookups
if (!state) {
Expand Down
5 changes: 4 additions & 1 deletion packages/schema/src/hooks.ts
Expand Up @@ -22,6 +22,9 @@ export interface DomRenderTagContext {
}

export interface DomBeforeRenderCtx extends ShouldRenderContext {
/**
* @deprecated will always be empty, prefer other hooks
*/
tags: DomRenderTagContext[]
}
export interface ShouldRenderContext { shouldRender: boolean }
Expand All @@ -36,7 +39,7 @@ export interface HeadHooks {
'tags:resolve': (ctx: { tags: HeadTag[] }) => HookResult

// @unhead/dom
'dom:beforeRender': (ctx: ShouldRenderContext & { tags: DomRenderTagContext[] }) => HookResult
'dom:beforeRender': (ctx: DomBeforeRenderCtx) => HookResult
'dom:renderTag': (ctx: DomRenderTagContext, document: Document, track: any) => HookResult
'dom:rendered': (ctx: { renders: DomRenderTagContext[] }) => HookResult

Expand Down

0 comments on commit 7bb1064

Please sign in to comment.