Make WordPress Core

Opened 3 years ago

Last modified 2 years ago

#55902 new defect (bug)

Block theme rendering issue

Reported by: jivansoft's profile jivansoft Owned by:
Milestone: Awaiting Review Priority: normal
Severity: blocker Version:
Component: Themes Keywords: needs-patch
Focuses: template Cc:

Description (last modified by sabernhardt)

Hi,

I am from Pagelayer team.

And we have checked and found that in the wp-includes/template-canvas.php file, you have called the function get_the_block_template_html() above the header that breaks the flow of the calling hooks like (get_header, wp_enqueue_scripts, wp_header).

For example, If we add any the_content hook inside the wp_enqueue_scripts hook, then the the_content hook not working properly.

Please check and try fix the issue ASAP if possible.

Change History (5)

#1 @bhrugesh12
3 years ago

#55901 was marked as a duplicate.

#2 @andykeith
2 years ago

We have also noticed this problem. Because of the order the hooks are executed in block-based themes, enqueing a script during the page output (e.g. the_content) happens before wp_enqueue_scripts has run, so the script isn't registered. In normal template based themes, the order of standard WordPress actions goes:

template_redirect
get_header
wp_head
wp_enqueue_scripts
enqueue_block_assets
wp_print_styles
wp_print_scripts
...etc
loop_start
the_post

With block-based FSE themes, the order changes to:

template_redirect
render_block_core_template_part_file
loop_start
the_post
...etc
wp_head
wp_enqueue_scripts
enqueue_block_assets
wp_print_styles
wp_print_scripts

Therefore any scripts enqueued during the_content or during shortcode callbacks (such as page specific scripts that are only required on that page) fall into the $queued_before_register fallback provided in WP_Dependencies. This mechanism handles any scripts enqueued before they were registered. The only reason the scripts enqueued during the_content are loaded at all is because of this fallback.

I would suggest that core behaviour in block themes shouldn't be relying on this fallback. This could be fixed by changing the order of the main post hooks so that the_head is called before the_content as it is in regular template themes.

There is also an additional knock-on affect for themes & plugins which add inline script data (e.g. wp_add_inline_script) during the_content, as reported in #54958. I believe both issues would be resolved by changing the order of the hooks for block-based themes.

#3 @sabernhardt
2 years ago

#55792 was marked as a duplicate.

#4 @sabernhardt
2 years ago

  • Component changed from General to Themes
  • Description modified (diff)
  • Keywords needs-patch added; changes-requested removed

A patch would need to consider the reasoning behind calling the function early: to avoid loading all block assets in the footer (see GB28319).

#5 @andykeith
2 years ago

I see, thanks for the the background. In that case, my view would be to leave the current behaviour. It's something to be aware of in future as it affects our we might enqueue scripts in our plugins. I'll let the original reporter add anything further.

Note: See TracTickets for help on using tickets.