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

Version missing from EnqueuedAsset types #2297

Closed
benknight opened this issue Mar 14, 2022 · 7 comments
Closed

Version missing from EnqueuedAsset types #2297

benknight opened this issue Mar 14, 2022 · 7 comments
Labels
component: interfaces Relating to GraphQL Interface Types effort: low Around a day or less impact: low Fixes a minor issue for some people, slight DX improvement status: in progress Currently being worked on type: bug Issue that causes incorrect or unexpected behavior

Comments

@benknight
Copy link

benknight commented Mar 14, 2022

Script and stylesheet versions don't resolve correctly because they reference an undefined filed named $version on the dependency objects. Desired behavior for the resolver would be to return $ver if it's set, and fall back to $default_version so that consumers can use query string ?v=... to manage caching the same way native themes do.

https://github.com/wp-graphql/wp-graphql/blob/develop/src/Type/InterfaceType/EnqueuedAsset.php#L53

See:

https://developer.wordpress.org/reference/classes/wp_dependencies/
https://developer.wordpress.org/reference/classes/_wp_dependency/
https://developer.wordpress.org/reference/classes/wp_scripts/

@benknight benknight changed the title Version mission from EnqueuedAsset types Version missing from EnqueuedAsset types Mar 14, 2022
@justlevine justlevine added type: bug Issue that causes incorrect or unexpected behavior effort: low Around a day or less impact: low Fixes a minor issue for some people, slight DX improvement status: actionable Ready for work to begin component: interfaces Relating to GraphQL Interface Types and removed status: actionable Ready for work to begin labels Mar 14, 2022
@justlevine
Copy link
Collaborator

Hey @benknight thanks for reporting the issue, and the possible fix!

Few notes after investigation:

  • Many scripts and stylesheets dont have a $ver specified in global $wp_{scripts|styles}, which as far as I can tell is on only programmatic way to get the list of assets. Please correct me if I'm wrong.
  • The $default_version doesnt exist on the individual _WP_Dependency, but instead is a property of WP_{Scripts|Styles}. We can get it by getting both globals and then using the $type to get the property dynamically.

consumers can use query string ?v=... to manage caching the same way native themes do.

Not 100% I understand what this would look like in headless. My experience with managing scripts/styles in WP is mainly configuring caching plugins (in native), or stripping them out entirely (aka why I ♥ decoupled ). So while this seems like an easy PR, it's probably best someone who understands the nuances writes it.

Code sample

(Assuming there arent any special cases to consider)

// EnqueuedAsset.php::L53
'version' => [
  'type'        => 'String',
  'description' => __( 'The version of the enqueued asset', 'wp-graphql' ),
  'resolve' => static function( $asset ) {
     global $wp_styles, $wp_scripts;
   
     $version = ! empty( $asset->ver ) ? $asset->ver : ${'wp_' . $asset->type . 's'}->default_version;
     return $version ?: null;
  },
]

@benknight
Copy link
Author

benknight commented Mar 14, 2022

Not 100% I understand what this would look like in headless. My experience with managing scripts/styles in WP is mainly configuring caching plugins (in native), or stripping them out entirely (aka why I ♥ decoupled ). So while this seems like an easy PR, it's probably best someone who understands the nuances writes it.

@justlevine In my case, we encountered a bug because in order to use blocks, it's necessary to include the blocks stylesheet at /wp-includes/css/dist/block-library/style.min.css. However in a recent version of WordPress many of the block's styles changed which broke our post styles because many users still had the old stylesheet cached. This is solved in native by appending the version number to the stylesheet URL e.g. ?v=5.1.2. We also manually include some plugin stylesheets/scripts for things like extended block libraries like Stackable for instance. These are vulnerable to the same caching issues if the authors were to push an update. So in other words, we need to be able to replicate native's cache-busting behavior using version numbers which should follow the same logic of using the dependency's version if specified, while falling back to the default version (which is just the WordPress semver string).

@benknight
Copy link
Author

benknight commented Mar 14, 2022

Update -- Digging deeper into this I found out that in WordPress 5.9 a lot of styles were moved into a global inline styles block. Many of the Gutenberg blocks styles are now in there, which means headless sites need to include it somehow, which means it needs to be available via the GraphQL API. The block looks something like this:

<style id='global-styles-inline-css'>
body{--wp--preset--color--black: #000000;--wp--preset--color--cyan-bluish-gray: #abb8c3;--wp--preset--color--white: #ffffff;--wp--preset--color--pale-pink: #f78da7;--wp--preset--color--vivid-red: #cf2e2e;--wp--preset--color--luminous-vivid-orange: #ff6900;--wp--preset--color--luminous-vivid-amber: #fcb900;--wp--preset--color--light-green-cyan: #7bdcb5;--wp--preset--color--vivid-green-cyan: #00d084;--wp--preset--color--pale-cyan-blue: #8ed1fc;--wp--preset--color--vivid-cyan-blue: #0693e3;--wp--preset--color--vivid-purple: #9b51e0;--wp--preset--gradient--vivid-cyan-blue-to-vivid-purple: linear-gradient(135deg,rgba(6,147,227,1) 0%,rgb(155,81,224) 100%);--wp--preset--gradient--light-green-cyan-to-vivid-green-cyan: linear-gradient(135deg,rgb(122,220,180) 0%,rgb(0,208,130) 100%);--wp--preset--gradient--luminous-vivid-amber-to-luminous-vivid-orange: linear-gradient(135deg,rgba(252,185,0,1) 0%,rgba(255,105,0,1) 100%);--wp--preset--gradient--luminous-vivid-orange-to-vivid-red: linear-gradient(135deg,rgba(255,105,0,1) 0%,rgb(207,46,46) 100%);--wp--preset--gradient--very-light-gray-to-cyan-bluish-gray: linear-gradient(135deg,rgb(238,238,238) 0%,rgb(169,184,195) 100%);--wp--preset--gradient--cool-to-warm-spectrum: linear-gradient(135deg,rgb(74,234,220) 0%,rgb(151,120,209) 20%,rgb(207,42,186) 40%,rgb(238,44,130) 60%,rgb(251,105,98) 80%,rgb(254,248,76) 100%);--wp--preset--gradient--blush-light-purple: linear-gradient(135deg,rgb(255,206,236) 0%,rgb(152,150,240) 100%);--wp--preset--gradient--blush-bordeaux: linear-gradient(135deg,rgb(254,205,165) 0%,rgb(254,45,45) 50%,rgb(107,0,62) 100%);--wp--preset--gradient--luminous-dusk: linear-gradient(135deg,rgb(255,203,112) 0%,rgb(199,81,192) 50%,rgb(65,88,208) 100%);--wp--preset--gradient--pale-ocean: linear-gradient(135deg,rgb(255,245,203) 0%,rgb(182,227,212) 50%,rgb(51,167,181) 100%);--wp--preset--gradient--electric-grass: linear-gradient(135deg,rgb(202,248,128) 0%,rgb(113,206,126) 100%);--wp--preset--gradient--midnight: linear-gradient(135deg,rgb(2,3,129) 0%,rgb(40,116,252) 100%);--wp--preset--duotone--dark-grayscale: url('#wp-duotone-dark-grayscale');--wp--preset--duotone--grayscale: url('#wp-duotone-grayscale');--wp--preset--duotone--purple-yellow: url('#wp-duotone-purple-yellow');--wp--preset--duotone--blue-red: url('#wp-duotone-blue-red');--wp--preset--duotone--midnight: url('#wp-duotone-midnight');--wp--preset--duotone--magenta-yellow: url('#wp-duotone-magenta-yellow');--wp--preset--duotone--purple-green: url('#wp-duotone-purple-green');--wp--preset--duotone--blue-orange: url('#wp-duotone-blue-orange');--wp--preset--font-size--small: 13px;--wp--preset--font-size--medium: 20px;--wp--preset--font-size--large: 36px;--wp--preset--font-size--x-large: 42px;}.has-black-color{color: var(--wp--preset--color--black) !important;}.has-cyan-bluish-gray-color{color: var(--wp--preset--color--cyan-bluish-gray) !important;}.has-white-color{color: var(--wp--preset--color--white) !important;}.has-pale-pink-color{color: var(--wp--preset--color--pale-pink) !important;}.has-vivid-red-color{color: var(--wp--preset--color--vivid-red) !important;}.has-luminous-vivid-orange-color{color: var(--wp--preset--color--luminous-vivid-orange) !important;}.has-luminous-vivid-amber-color{color: var(--wp--preset--color--luminous-vivid-amber) !important;}.has-light-green-cyan-color{color: var(--wp--preset--color--light-green-cyan) !important;}.has-vivid-green-cyan-color{color: var(--wp--preset--color--vivid-green-cyan) !important;}.has-pale-cyan-blue-color{color: var(--wp--preset--color--pale-cyan-blue) !important;}.has-vivid-cyan-blue-color{color: var(--wp--preset--color--vivid-cyan-blue) !important;}.has-vivid-purple-color{color: var(--wp--preset--color--vivid-purple) !important;}.has-black-background-color{background-color: var(--wp--preset--color--black) !important;}.has-cyan-bluish-gray-background-color{background-color: var(--wp--preset--color--cyan-bluish-gray) !important;}.has-white-background-color{background-color: var(--wp--preset--color--white) !important;}.has-pale-pink-background-color{background-color: var(--wp--preset--color--pale-pink) !important;}.has-vivid-red-background-color{background-color: var(--wp--preset--color--vivid-red) !important;}.has-luminous-vivid-orange-background-color{background-color: var(--wp--preset--color--luminous-vivid-orange) !important;}.has-luminous-vivid-amber-background-color{background-color: var(--wp--preset--color--luminous-vivid-amber) !important;}.has-light-green-cyan-background-color{background-color: var(--wp--preset--color--light-green-cyan) !important;}.has-vivid-green-cyan-background-color{background-color: var(--wp--preset--color--vivid-green-cyan) !important;}.has-pale-cyan-blue-background-color{background-color: var(--wp--preset--color--pale-cyan-blue) !important;}.has-vivid-cyan-blue-background-color{background-color: var(--wp--preset--color--vivid-cyan-blue) !important;}.has-vivid-purple-background-color{background-color: var(--wp--preset--color--vivid-purple) !important;}.has-black-border-color{border-color: var(--wp--preset--color--black) !important;}.has-cyan-bluish-gray-border-color{border-color: var(--wp--preset--color--cyan-bluish-gray) !important;}.has-white-border-color{border-color: var(--wp--preset--color--white) !important;}.has-pale-pink-border-color{border-color: var(--wp--preset--color--pale-pink) !important;}.has-vivid-red-border-color{border-color: var(--wp--preset--color--vivid-red) !important;}.has-luminous-vivid-orange-border-color{border-color: var(--wp--preset--color--luminous-vivid-orange) !important;}.has-luminous-vivid-amber-border-color{border-color: var(--wp--preset--color--luminous-vivid-amber) !important;}.has-light-green-cyan-border-color{border-color: var(--wp--preset--color--light-green-cyan) !important;}.has-vivid-green-cyan-border-color{border-color: var(--wp--preset--color--vivid-green-cyan) !important;}.has-pale-cyan-blue-border-color{border-color: var(--wp--preset--color--pale-cyan-blue) !important;}.has-vivid-cyan-blue-border-color{border-color: var(--wp--preset--color--vivid-cyan-blue) !important;}.has-vivid-purple-border-color{border-color: var(--wp--preset--color--vivid-purple) !important;}.has-vivid-cyan-blue-to-vivid-purple-gradient-background{background: var(--wp--preset--gradient--vivid-cyan-blue-to-vivid-purple) !important;}.has-light-green-cyan-to-vivid-green-cyan-gradient-background{background: var(--wp--preset--gradient--light-green-cyan-to-vivid-green-cyan) !important;}.has-luminous-vivid-amber-to-luminous-vivid-orange-gradient-background{background: var(--wp--preset--gradient--luminous-vivid-amber-to-luminous-vivid-orange) !important;}.has-luminous-vivid-orange-to-vivid-red-gradient-background{background: var(--wp--preset--gradient--luminous-vivid-orange-to-vivid-red) !important;}.has-very-light-gray-to-cyan-bluish-gray-gradient-background{background: var(--wp--preset--gradient--very-light-gray-to-cyan-bluish-gray) !important;}.has-cool-to-warm-spectrum-gradient-background{background: var(--wp--preset--gradient--cool-to-warm-spectrum) !important;}.has-blush-light-purple-gradient-background{background: var(--wp--preset--gradient--blush-light-purple) !important;}.has-blush-bordeaux-gradient-background{background: var(--wp--preset--gradient--blush-bordeaux) !important;}.has-luminous-dusk-gradient-background{background: var(--wp--preset--gradient--luminous-dusk) !important;}.has-pale-ocean-gradient-background{background: var(--wp--preset--gradient--pale-ocean) !important;}.has-electric-grass-gradient-background{background: var(--wp--preset--gradient--electric-grass) !important;}.has-midnight-gradient-background{background: var(--wp--preset--gradient--midnight) !important;}.has-small-font-size{font-size: var(--wp--preset--font-size--small) !important;}.has-medium-font-size{font-size: var(--wp--preset--font-size--medium) !important;}.has-large-font-size{font-size: var(--wp--preset--font-size--large) !important;}.has-x-large-font-size{font-size: var(--wp--preset--font-size--x-large) !important;}
</style>

These are enqueued by the core under the handle global-styles. Here it is being returned in my GraphiQL IDE:

image

WPGraphQL will need to be modified somehow to provide the inline style code block on some field. Perhaps this should be opened as a separate issue since it's technically tangental to problem of referencing an undefined "version" field on dependecies.

@justlevine
Copy link
Collaborator

#2259 is tracking that, and you can use the provided snippet locally in the interim - only thing that isnt are layout styles, since theyre not saved in global-styles (see WordPress/gutenberg/issues/35376).

Regarding this issue, i'm going to give it a few days in case someone shares an edge case for the version that needs to be included, and then if nobody else has yet will get to work on the PR.

@stale
Copy link

stale bot commented Aug 2, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Aug 2, 2022
@stale
Copy link

stale bot commented Sep 2, 2022

This issue has been automatically closed because it has not had recent activity. If you believe this issue is still valid, please open a new issue and mark this as a related issue.

@stale stale bot closed this as completed Sep 2, 2022
Repository owner moved this from 🏗 In progress to ✅ Done in WPGraphQL Issue Triage Sep 2, 2022
@justlevine
Copy link
Collaborator

merged in v1.8.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: interfaces Relating to GraphQL Interface Types effort: low Around a day or less impact: low Fixes a minor issue for some people, slight DX improvement status: in progress Currently being worked on type: bug Issue that causes incorrect or unexpected behavior
Projects
Status: Done
Development

No branches or pull requests

2 participants