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

Enqueued scripts should use plugins_url() instead of plugin_dir_url() #1647

Open
westonruter opened this issue Nov 11, 2024 · 2 comments
Open
Labels
Good First Issue Issue particularly suitable to be worked on by new contributors [Plugin] Embed Optimizer Issues for the Embed Optimizer plugin (formerly Auto Sizes) [Plugin] Optimization Detective Issues for the Optimization Detective plugin [Plugin] Performance Lab Issue relates to work in the Performance Lab Plugin only [Plugin] Web Worker Offloading Issues for the Web Worker Offloading plugin. [Type] Enhancement A suggestion for improvement of an existing feature

Comments

@westonruter
Copy link
Member

Feature Description

When the URLs for JS files are generated in the plugins, the plugin_dir_url() function is used. Its output is concatenated with the relative path:

wp_json_encode( add_query_arg( 'ver', OPTIMIZATION_DETECTIVE_VERSION, plugin_dir_url( __FILE__ ) . 'detect.js' ) ),

However, this is not ideal because the resulting URL is not directly filterable. It would be better to use plugins_url():

- add_query_arg( 'ver', OPTIMIZATION_DETECTIVE_VERSION, plugin_dir_url( __FILE__ ) . 'detect.js'
+ plugins_url( add_query_arg( 'ver', OPTIMIZATION_DETECTIVE_VERSION, 'detect.js' ), __FILE__ )

In this way, the plugins_url filter can be used to potentially rewrite the URL to point to a CDN, for example.

See instances: https://github.com/search?q=repo%3AWordPress%2Fperformance%20%2Fplugin_dir_url%5C(%2F&type=code

@westonruter westonruter added [Plugin] Embed Optimizer Issues for the Embed Optimizer plugin (formerly Auto Sizes) [Plugin] Optimization Detective Issues for the Optimization Detective plugin [Plugin] Performance Lab Issue relates to work in the Performance Lab Plugin only [Plugin] Web Worker Offloading Issues for the Web Worker Offloading plugin. [Type] Enhancement A suggestion for improvement of an existing feature Good First Issue Issue particularly suitable to be worked on by new contributors labels Nov 11, 2024
@github-project-automation github-project-automation bot moved this to Not Started/Backlog 📆 in WP Performance 2024 Nov 11, 2024
@westonruter westonruter moved this from Not Started/Backlog 📆 to To Do 🔧 in WP Performance 2024 Nov 12, 2024
@felixarntz
Copy link
Member

@westonruter I'm not sure I agree with your assessment:

  • plugin_dir_url() wraps plugins_url(), so the URL up to the plugin's own directory is still filterable.
  • This means e.g. changing to point to a CDN works just as well with plugin_dir_url().
  • I don't see any benefit of being able to filter the remainder of the URL (the path relative to the plugin's directory). You can't (or at the very least shouldn't) change the contents of a plugin from the outside, so being able to filter that part makes no sense to me. It's safer not to allow filtering it.

@westonruter
Copy link
Member Author

A couple additional use cases for being able to filter the entire URL:

  • Replace a minified JS or CSS file with a non-minified one, or vice-versa. This requires adding/removing .min from the URL path. (Example filter from Jetpack.)
  • Add a cache busting query parameter.

Jetpack favors plugins_url() over plugin_dir_url() with 62 files vs 8 files, respectively.

Maybe more importantly, Gutenberg also favors plugins_url() over plugin_dir_url(), with 32 files vs 5 files, respectively.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Good First Issue Issue particularly suitable to be worked on by new contributors [Plugin] Embed Optimizer Issues for the Embed Optimizer plugin (formerly Auto Sizes) [Plugin] Optimization Detective Issues for the Optimization Detective plugin [Plugin] Performance Lab Issue relates to work in the Performance Lab Plugin only [Plugin] Web Worker Offloading Issues for the Web Worker Offloading plugin. [Type] Enhancement A suggestion for improvement of an existing feature
Projects
Status: To Do 🔧
Development

No branches or pull requests

2 participants