Repository navigation
iAPI Router: Reimplement styles assets logic for client-side navigation - #2
Conversation
…WordPress#67826)" This reverts commit 0a5be06.
…eets (WordPress#68923)" This reverts commit a53bb20.
|
Size Change: -89 B (0%) Total Size: 1.84 MB
ℹ️ View Unchanged
|
luisherranz
left a comment
There was a problem hiding this comment.
This looks very promising, David. Using element.sheet.disabled is brilliant 🙂👏
Are the E2E tests the same as those in the previous implementation?
79db678 to
116aad9
Compare
9ca2e02 to
ed4eafb
Compare
|
@SantosGuillamot, I think we shouldn't bother using labels here. I have already deactivated the "Enforce labels on Pull Request" action so the next time there is a commit, it will (hopefully) disappear. |
It makes sense. If it doesn't work, we could check the repo in the workflow as it is done here. I can submit a PR if needed. |
SantosGuillamot
left a comment
There was a problem hiding this comment.
There is a bug in this branch when the experimental full client-side navigation is enabled. When navigating back to the first loaded page, it removes the whole body. I'll share a video of the bug and how it is working fine in the interactivity-api branch.
Bug
CSN.-.bug.mp4
Expected
CSN.-.working.mp4
|
I haven't checked, but maybe it could be due to this… |
|
@SantosGuillamot, @luisherranz, I think the problem you're discussing should be related to WordPress/gutenberg#66772 (perhaps a previous PR). The issue is that That's not a problem when However, for full-site, client-side navigation, the |
|
For context: |
|
Oh, @luisherranz, I've discovered something-we can actually use Seems that we were modifying the wrong Check out this change: gutenberg/packages/interactivity-router/src/assets/styles.ts Lines 77 to 83 in f14de01 That effectively enables the style sheet without requesting it again, and allows us to enable/disable any style sheet on demand. ☝️ |
|
Awesome! ❤️ |
|
Ready for review, @luisherranz! |
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
47c4920 to
c273234
Compare
luisherranz
left a comment
There was a problem hiding this comment.
What a fantastic work, David ❤️
I have some comments and questions, but nothing serious.
This is so great! It feels like we've finally covered everything (I hope 😄) 👏👏👏
| const [ normalizedA, normalizedB ] = [ a, b ].map( ( element ) => { | ||
| if ( element.getAttribute( 'media' ) === 'preload' ) { | ||
| element = element.cloneNode( true ) as StyleElement; | ||
| const { originalMedia } = element.dataset; | ||
| if ( originalMedia ) { | ||
| element.setAttribute( 'media', originalMedia ); | ||
| element.removeAttribute( 'data-original-media' ); | ||
| } else { | ||
| element.removeAttribute( 'media' ); | ||
| } | ||
| } | ||
| return element; | ||
| } ); |
There was a problem hiding this comment.
Wouldn't it be better to normalize all the elements before comparing them? I mean, can't we be creating a lot of elements unnecessarily, over and over again, by normalizing this way?
There was a problem hiding this comment.
Great, thanks. Two things:
- We could also delete the
areNodesEqualfunction as it doesn't add much value after this optimization. - We could cache the normalized elements so we don't have to recreate them for the group of elements that are in the DOM on each prefetch/navigation.
What do you think?
There was a problem hiding this comment.
We could remove areNodesEqual, yup. And I guess the normalized cache would work, although I think cloneNode is not that expensive, at least for these simple nodes without nested elements. 🤔
There was a problem hiding this comment.
I think
cloneNodeis not that expensive
Yeah, that's probably right. My mind tells me that we shouldn't use DOM APIs unnecessarily 🤷
What?
This PR reimplements the style assets manager that was introduced previously in Gutenberg. It replaces the
document.adoptedStyleSheetsapproach with another based on new<style>and<link rel=stylesheet>elements in the DOM.It's required so that blocks like WooCommerce's Product Collection can correctly load blocks that were not present in the page load after a client-side navigation.
Why?
The previous approach was based on the
document.adoptedStyleSheetsAPI, which requires constructedCSSStyleSheetinstances to be passed. However, for third-party style sheets without the appropriate CORS configuration, these instances were impossible to create, as the CSS code is unreadable.How?
The new approach works as follows: when a new page is prefetched, the
<style>and<link rel=stylesheet>elements are cloned and added in the DOM. Once added, they are immediately disabled until the corresponding page is visited. The style sheets are then kept in the DOM between navigations to prevent additional network requests and are enabled or disabled depending on the active page.