@@ -203,6 +203,7 @@ const getInnermostProperty = (propsList, property) => {
203203const reducePropsToState = propsList => ( {
204204 baseTag : getBaseTagFromPropsList ( [ TAG_PROPERTIES . HREF ] , propsList ) ,
205205 bodyAttributes : getAttributesFromPropsList ( ATTRIBUTE_NAMES . BODY , propsList ) ,
206+ defer : getInnermostProperty ( propsList , HELMET_PROPS . DEFER ) ,
206207 encode : getInnermostProperty (
207208 propsList ,
208209 HELMET_PROPS . ENCODE_SPECIAL_CHARACTERS
@@ -286,6 +287,23 @@ const warn = msg => {
286287let _helmetIdleCallback = null ;
287288
288289const handleClientStateChange = newState => {
290+ if ( _helmetIdleCallback ) {
291+ cancelIdleCallback ( _helmetIdleCallback ) ;
292+ }
293+
294+ if ( newState . defer ) {
295+ _helmetIdleCallback = requestIdleCallback ( ( ) => {
296+ commitTagChanges ( newState , ( ) => {
297+ _helmetIdleCallback = null ;
298+ } ) ;
299+ } ) ;
300+ } else {
301+ commitTagChanges ( newState ) ;
302+ _helmetIdleCallback = null ;
303+ }
304+ } ;
305+
306+ const commitTagChanges = ( newState , cb ) => {
289307 const {
290308 baseTag,
291309 bodyAttributes,
@@ -299,43 +317,37 @@ const handleClientStateChange = newState => {
299317 title,
300318 titleAttributes
301319 } = newState ;
320+ updateAttributes ( TAG_NAMES . BODY , bodyAttributes ) ;
321+ updateAttributes ( TAG_NAMES . HTML , htmlAttributes ) ;
322+
323+ updateTitle ( title , titleAttributes ) ;
324+
325+ const tagUpdates = {
326+ baseTag : updateTags ( TAG_NAMES . BASE , baseTag ) ,
327+ linkTags : updateTags ( TAG_NAMES . LINK , linkTags ) ,
328+ metaTags : updateTags ( TAG_NAMES . META , metaTags ) ,
329+ noscriptTags : updateTags ( TAG_NAMES . NOSCRIPT , noscriptTags ) ,
330+ scriptTags : updateTags ( TAG_NAMES . SCRIPT , scriptTags ) ,
331+ styleTags : updateTags ( TAG_NAMES . STYLE , styleTags )
332+ } ;
302333
303- if ( _helmetIdleCallback ) {
304- cancelIdleCallback ( _helmetIdleCallback ) ;
305- }
306-
307- _helmetIdleCallback = requestIdleCallback ( ( ) => {
308- updateAttributes ( TAG_NAMES . BODY , bodyAttributes ) ;
309- updateAttributes ( TAG_NAMES . HTML , htmlAttributes ) ;
310-
311- updateTitle ( title , titleAttributes ) ;
334+ const addedTags = { } ;
335+ const removedTags = { } ;
312336
313- const tagUpdates = {
314- baseTag : updateTags ( TAG_NAMES . BASE , baseTag ) ,
315- linkTags : updateTags ( TAG_NAMES . LINK , linkTags ) ,
316- metaTags : updateTags ( TAG_NAMES . META , metaTags ) ,
317- noscriptTags : updateTags ( TAG_NAMES . NOSCRIPT , noscriptTags ) ,
318- scriptTags : updateTags ( TAG_NAMES . SCRIPT , scriptTags ) ,
319- styleTags : updateTags ( TAG_NAMES . STYLE , styleTags )
320- } ;
337+ Object . keys ( tagUpdates ) . forEach ( tagType => {
338+ const { newTags, oldTags} = tagUpdates [ tagType ] ;
321339
322- const addedTags = { } ;
323- const removedTags = { } ;
324-
325- Object . keys ( tagUpdates ) . forEach ( tagType => {
326- const { newTags, oldTags} = tagUpdates [ tagType ] ;
340+ if ( newTags . length ) {
341+ addedTags [ tagType ] = newTags ;
342+ }
343+ if ( oldTags . length ) {
344+ removedTags [ tagType ] = tagUpdates [ tagType ] . oldTags ;
345+ }
346+ } ) ;
327347
328- if ( newTags . length ) {
329- addedTags [ tagType ] = newTags ;
330- }
331- if ( oldTags . length ) {
332- removedTags [ tagType ] = tagUpdates [ tagType ] . oldTags ;
333- }
334- } ) ;
348+ cb && cb ( ) ;
335349
336- _helmetIdleCallback = null ;
337- onChangeClientState ( newState , addedTags , removedTags ) ;
338- } ) ;
350+ onChangeClientState ( newState , addedTags , removedTags ) ;
339351} ;
340352
341353const flattenArray = possibleArray => {
0 commit comments