Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new How-to Guide for enqueueing assets in the Editor. #53828

Merged
merged 3 commits into from
Aug 24, 2023

Conversation

ndiego
Copy link
Member

@ndiego ndiego commented Aug 19, 2023

What?

This PR adds a new How-to Guide in the Block Editor Handbook that details the best practices for enqueueing assets in the Editor.

Why?

Numerous dev notes, GitHub issues, and scattered articles discuss enqueueing assets in the Editor. This PR aims to house all that knowledge in one place so it can be easily referenced.

Note that this PR should be considered an initial iteration. There are many nuances and edge cases regarding enqueueing assets in the Editor. There are also a handful of open issues related to this topic (ex. #53590). Future updates will undoubtedly be needed.

@ndiego ndiego added the [Type] Developer Documentation Documentation for developers label Aug 19, 2023
@ndiego ndiego self-assigned this Aug 19, 2023
@ndiego ndiego marked this pull request as ready for review August 19, 2023 17:27
Copy link
Member

@fabiankaegy fabiankaegy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for compiling all this! :)

docs/how-to-guides/enqueueing-assets-in-the-editor.md Outdated Show resolved Hide resolved
docs/how-to-guides/enqueueing-assets-in-the-editor.md Outdated Show resolved Hide resolved
docs/how-to-guides/enqueueing-assets-in-the-editor.md Outdated Show resolved Hide resolved
@github-actions
Copy link

Flaky tests detected in 77eef3f.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/5913306149
📝 Reported issues:

Copy link
Contributor

@justintadlock justintadlock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job simplifying and clarifying this, @ndiego!

@juanmaguitar
Copy link
Contributor

juanmaguitar commented Aug 21, 2023

Great job @ndiego! I think this page is a much-needed reference for this topic.

As a suggestion, I would group the sections describing each scenario, under a parent group called "Scenarios for enqueuing " (or similar), resulting in a structure as follows:

  • The Editor versus Editor content
  • Scenarios for enqueing
    • Editor scripts and styles
    • Editor content scripts and styles
    • Block scripts and styles
    • Theme scripts and styles
  • Backward compatibility and known issues

Some practical examples could also be added to https://github.com/WordPress/gutenberg-examples and referenced from this page but that would be out of the scope of this PR

@ndiego
Copy link
Member Author

ndiego commented Aug 23, 2023

As a suggestion, I would group the sections describing each scenario, under a parent group called "Scenarios for enqueuing " (or similar), resulting in a structure as follows:

Thanks @juanmaguitar, I have updated the heading structure accordingly.

Some practical examples could also be added to https://github.com/WordPress/gutenberg-examples and referenced from this page but that would be out of the scope of this PR

This is a great idea. Definitely, something to add in the next iteration.

@ndiego ndiego enabled auto-merge (squash) August 23, 2023 11:32
@ndiego ndiego merged commit 255ac93 into trunk Aug 24, 2023
48 of 49 checks passed
@ndiego ndiego deleted the add/equeueing-assets-in-editor-doc branch August 24, 2023 12:33
@github-actions github-actions bot added this to the Gutenberg 16.6 milestone Aug 24, 2023

This guide is designed to be the definitive reference for enqueueing assets (scripts and styles) in the Editor. The approaches outlined here represent the recommended practices but keep in mind that this resource will evolve as WordPress does. Updates are encouraged.

As of WordPress 6.3, the Post Editor is iframed if all registered blocks have a [`Block API version 3`](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-metadata/) or higher and no traditional metaboxes are registered. The Site Editor has always been iframed. This guide assumes you are looking to enqueue assets for the iframed Editor, but refer to the backward compatibility section below for additional considerations.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth noting that enqueuing assets to work for iframing should also work if the editor is not iframed, just not vice versa (things working without iframe will not necessarily work with the iframe).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @ellatrix. I will add this note to the doc later this week. I have a few more minor tweaks to make.

@ellatrix
Copy link
Member

I added a link to your page in https://make.wordpress.org/core/2023/07/18/miscellaneous-editor-changes-in-wordpress-6-3/#post-editor-iframed.

@chimok
Copy link

chimok commented Sep 4, 2023

I did always load SVG sprites with AJAX, this doesn't work anymore with iframed editor and i'm wondering if there's a better way to use SVG fragments inside Gutenberg? Sure, I could use some conditions and use the tricks mention in the new howto.

Another problem is any script loaded with enqueue_block_assets get loaded twice, inside admin page and inside iframe. For now I will use wp_enqueue_scripts for that purpose, so it will only get loaded on frontend. But then no icons inside Gutenberg.
I believe it takes years until all plugins are compatible with this behavior.

This was the code:

function loadIcons() {
    fetch( '/app/themes/custom/static/icons.svg' )
        .then( ( response ) => response.text() )
        .then( ( result ) =>
            document.body.insertAdjacentHTML( 'afterbegin', result )
        );
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Developer Documentation Documentation for developers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create guide in the Block Editor Handbook on how to enqueue assets in the Editor
6 participants