Opened 12 months ago
Last modified 4 months ago
#61027 new enhancement
Bundled themes: edit front-end JS to work with Interactivity API
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | Future Release | Priority: | normal |
Severity: | normal | Version: | |
Component: | Bundled Theme | Keywords: | |
Focuses: | performance | Cc: |
Description (last modified by )
Note:
- Leveraging the Interactivity API for core themes will help us test and consume our own APIs.
- The Interactivity API will already be enqueued if any block leveraging the API is used (Query, expanding Search and Navigation for example)
- Existing code would stay in place for cases where the theme is used on WordPress < 6.5.
Change History (13)
This ticket was mentioned in PR #5795 on WordPress/wordpress-develop by @flixos90.
12 months ago
#1
- Keywords has-patch added
#2
@
12 months ago
- Keywords has-patch removed
I see @flixos90 already started working on this in https://github.com/WordPress/wordpress-develop/pull/5795
#3
@
12 months ago
- Summary changed from Twenty Twentyone: Replace frontend jQuery usage with Interactivity API to Twenty Twenty-One: Replace front-end JS with Interactivity API
Summary edit: TT1 only uses jQuery with Customizer scripts ('twentytwentyone-control-color', 'twentytwentyone-customize-controls', and 'twentytwentyone-customize-preview').
#4
@
12 months ago
- Description modified (diff)
- Summary changed from Twenty Twenty-One: Replace front-end JS with Interactivity API to Bundled themes: edit front-end JS to work with Interactivity API
- Type changed from defect (bug) to feature request
Let's discuss any possible integrations in (classic) bundled themes on this ticket.
This ticket was mentioned in Slack in #core-performance by mukeshpanchal27. View the logs.
12 months ago
#10
@
6 months ago
- Keywords has-patch removed
- Milestone changed from Awaiting Review to Future Release
- Type changed from feature request to enhancement
Per the latest iteration on the ticket summary, we should probably use this as an overarching ticket for work that should span across multiple sub-tickets (one ticket per theme).
We already have a few related Trac tickets with the original purpose of replacing jQuery in classic default themes with vanilla JS. Given the direction of the Interactivity API, it would IMO make sense to repurpose these tickets as a part of this effort. For reference, here is an older Make Themes post about the original effort: https://make.wordpress.org/themes/2021/10/04/the-performance-impact-of-using-jquery-in-wordpress-themes/
By replacing jQuery with the Interactivity API in those older themes, we would solve two problems at once: Migrate away from jQuery, and facilitate adoption of the more performant Interactivity API.
We will need tickets for the following themes:
- Twenty Twelve (jQuery): #54171
- Twenty Thirteen (jQuery): …
- Twenty Fourteen (jQuery): …
- Twenty Fifteen (jQuery): #60985
- Twenty Sixteen (jQuery): #55126
- Twenty Seventeen (vanilla JS): …
- Twenty Nineteen (vanilla JS): …
- Twenty Twenty (vanilla JS): …
- Twenty Twenty-One (vanilla JS): …
Let's not open other tickets just yet, to avoid additional ticket overhead while this effort may still be stale. I think opening tickets would be best to do on demand as contributors want to own the effort for one of the themes.
Worth highlighting: When I say "replace jQuery or vanilla JS", I really mean "conditionally replace". We need to continue to support older WordPress versions where the Interactivity API was not available yet. Since the interactive features of the default themes have been quite simple and have rarely required updates throughout the years, I think it's reasonable to have two alternative versions of the implementation in each theme that is conditionally loaded based on whether or not the Interactivity API is available on the site.
This PR uses the Interactivity API to replace the custom frontend JS code for the Twenty Twenty-One theme. More concretely:
interactivity.js
module).primary-navigation.js
,responsive-embeds.js
,dark-mode-toggler.js
) as it replaces those functionalities.One important consideration: Since the server-side portion of the Interactivity API does not process this content automatically (unlike blocks, which are processed server-side), it is important to hydrate any dynamic attributes to have the correct initial state. For example, if you add
data-wp-bind--aria-expanded="state.something"
to a tag, you should also set the actualaria-expanded="true|false"
attribute depending on what the initial value ofstate.something
is expected to be.This is an experimental PR, primarily as a proof of concept and to get a better idea of how the API works, and what _should_ or _should not_ be done with it.
To test it, you need to have the Gutenberg plugin activated. You should then see a
script[type="module"]
pointing to the new Twenty Twenty-Oneinteractivity.js
file in the<head>
.A few observations:
Unrelated: While working on this, I discovered and reported https://core.trac.wordpress.org/ticket/60111 which I think is a bug.
Trac ticket: https://core.trac.wordpress.org/ticket/61027