Automattic / Automattic/nginx-http-concat

Bug with wp_add_inline_script

Open
#55 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
PHP
Stars
120
Forks
37
PR merge metrics
No merged PRs in 30d

Description

This is a fun edge case bug:

When a script ("child") is concatenated with another script that it requires ("parent"), but also uses `wp_add_inline_script()` with the third argument being `before`, any code that has dependencies on the parent script will fail.

Just as an example:

1. The Shortcake plugin [registers select2 with a dependency on jQuery](https://github.com/wp-shortcake/shortcake/blob/eac99f7bbfde3e182441d5c6b75aa4cd4334a44f/inc/class-shortcode-ui.php#L261)
2. It then [adds an inline script that depends on jQuery](https://github.com/wp-shortcake/shortcake/blob/eac99f7bbfde3e182441d5c6b75aa4cd4334a44f/inc/class-shortcode-ui.php#L265), but wants it output _before_ the select2 JS file.
3. The nginx-http-concat plugin [will output the _before_ script](https://github.com/Automattic/nginx-http-concat/blob/595b5c7aa14afc46f4f6af22868792dacf74421a/jsconcat.php#L171)
4. The nginx-http-concat plugin will output [the concatenated blob](https://github.com/Automattic/nginx-http-concat/blob/595b5c7aa14afc46f4f6af22868792dacf74421a/jsconcat.php#L175)
5. If the parent and child script are in the same blob, the _before_ JS will fail.

I believe what needs to be done is to check if a script has a `wp_add_inline_script()` with a _before_ , it would be good to break the blob apart at that exact script, output the inline script, and then the concatenated blob.

We could get more detailed and check if any parent/child dependencies exist within the same blob first.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with jsconcat.php around the linked handling near lines 171 and 175, then trace how inline `before` scripts and dependent parent/child scripts are grouped. Reproduce the Shortcake/select2 dependency case described in the issue. Done means the inline code executes before the dependent script without breaking scripts that share the concatenated resource.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, php, wordpress
Domain
frontend, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.