11import { TagsWithInnerContent , createElement } from 'zhead'
2- import type { DomRenderTagContext , HeadClient } from '@unhead/schema'
2+ import type { BeforeRenderContext , DomRenderTagContext , Unhead } from '@unhead/schema'
33import { setAttributesWithSideEffects } from './setAttributesWithSideEffects'
44
55export interface RenderDomHeadOptions {
@@ -12,16 +12,22 @@ export interface RenderDomHeadOptions {
1212/**
1313 * Render the head tags to the DOM.
1414 */
15- export async function renderDOMHead < T extends HeadClient < any > > ( head : T , options : RenderDomHeadOptions = { } ) {
15+ export async function renderDOMHead < T extends Unhead < any > > ( head : T , options : RenderDomHeadOptions = { } ) {
1616 const dom : Document = options . document || window . document
1717
1818 const tags = await head . resolveTags ( )
1919
20- await head . hooks . callHook ( 'dom:beforeRender' , { head, tags, document : dom } )
20+ const context : BeforeRenderContext = { shouldRender : true , tags }
21+ await head . hooks . callHook ( 'dom:beforeRender' , context )
22+ // allow integrations to block to the render
23+ if ( ! context . shouldRender )
24+ return
2125
22- for ( const tag of tags ) {
23- const renderCtx : DomRenderTagContext = { tag , document : dom , head }
26+ for ( const tag of context . tags ) {
27+ const renderCtx : DomRenderTagContext = { shouldRender : true , tag }
2428 await head . hooks . callHook ( 'dom:renderTag' , renderCtx )
29+ if ( ! renderCtx . shouldRender )
30+ return
2531
2632 const entry = head . headEntries ( ) . find ( e => e . _i === Number ( tag . _e ) ) !
2733
@@ -94,12 +100,13 @@ export let domUpdatePromise: Promise<void> | null = null
94100/**
95101 * Queue a debounced update of the DOM head.
96102 */
97- export async function debouncedRenderDOMHead < T extends HeadClient < any > > ( delayedFn : ( fn : ( ) => void ) => void , head : T , options : RenderDomHeadOptions = { } ) {
103+ export async function debouncedRenderDOMHead < T extends Unhead < any > > ( head : T , options : RenderDomHeadOptions & { delayFn ?: ( fn : ( ) => void ) => void } = { } ) {
98104 // within the debounced dom update we need to compute all the tags so that watchEffects still works
99105 function doDomUpdate ( ) {
100106 domUpdatePromise = null
101107 return renderDOMHead ( head , options )
102108 }
103-
104- return domUpdatePromise = domUpdatePromise || new Promise ( resolve => delayedFn ( ( ) => resolve ( doDomUpdate ( ) ) ) )
109+ // we want to delay for the hydration chunking
110+ const delayFn = options . delayFn || ( fn => setTimeout ( fn , 25 ) )
111+ return domUpdatePromise = domUpdatePromise || new Promise ( resolve => delayFn ( ( ) => resolve ( doDomUpdate ( ) ) ) )
105112}
0 commit comments