Skip to content

Conversation

sirreal
Copy link
Member

@sirreal sirreal commented Aug 27, 2025

Trac ticket: https://core.trac.wordpress.org/ticket/63887

Script and style tags can add a sourceURL comment to name them for developer tools. This is similar to sourceMappingURL that are often used with JavaScript compilers. They are described in this specification.

Messages and styles that come from inline scripts and styles are often difficult to understand and debug because they typically point to part of the current URL, like (index):1530 or ?p=1535:1530. These point to a line number in the page HTML and are confusing and inconsistent across pages.

Inline scripts and styles can provide a sourceURL to provide a consistent name and simplify debugging, for example //# sourceURL=example-js-after. It seems to be supported in Chrome, Firefox, and Edge at this time. Safari does not appear to support it.

Some examples from developer tools in Chrome:

Inline styles before

body { (index):475
    background: red !important;
}

After

body { example-inline-css:2
    background: red !important;
}

Inline scripts before

Uncaught Error: Oh no!
    at (index):1401:7

After

example-js-after:2 Uncaught Error: Oh no!
    at example-js-after:2:7

This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

Copy link

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@sirreal sirreal force-pushed the 63887/add-inline-script-style-sourceurl branch from aa0570d to e887b43 Compare August 27, 2025 08:43
@sirreal sirreal marked this pull request as ready for review August 27, 2025 08:59
Copy link

github-actions bot commented Aug 27, 2025

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 props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props jonsurrell, swissspidy, alshakero, westonruter.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@sirreal
Copy link
Member Author

sirreal commented Aug 27, 2025

Inline scripts and styles include a sourceURL that matches the ID attributes used in their tags, for example:

  • scripthandle-js-before
  • scripthandle-js-after
  • stylehandle-inline-css

wp_localize_script() is not handled in the first version of this PR. If desired, that can likely be added.

@alshakero brought this to my attention. I know @jsnajdr, @sgomes, and @youknowriad also expressed interest.

@sirreal sirreal requested review from youknowriad, westonruter, gziolo and swissspidy and removed request for gziolo August 27, 2025 09:13
Copy link
Member

@swissspidy swissspidy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea!

I saw this feature recently too and thought it would be perfect for core, so thanks for adding this!

@sirreal
Copy link
Member Author

sirreal commented Aug 27, 2025

Shall I see about handling wp_localize_script() as well?

@sirreal sirreal force-pushed the 63887/add-inline-script-style-sourceurl branch from 3c4e068 to 89ac596 Compare August 27, 2025 11:10
@sirreal
Copy link
Member Author

sirreal commented Aug 27, 2025

I pushed a change to add sourceURL to scripts produced by wp_localize_script() as well.

Copy link

@alshakero alshakero left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested it and tried my best to break it (new lines, */ in handle and weird characters in handle), and it worked like a charm!

TIL: New lines will make it through when used in a script handle. But the rawurlencode took care of these.

@westonruter
Copy link
Member

I saw this feature recently too and thought it would be perfect for core, so thanks for adding this!

Me too! I had this on my list to follow up on. Specifically via: https://x.com/csswizardry/status/1954863359695720623

@westonruter
Copy link
Member

What about prefixing the sourceURL with inline: as suggested in that tweet?

@sirreal
Copy link
Member Author

sirreal commented Aug 28, 2025

What about prefixing the sourceURL with inline:

I don't feel strongly, but after reflecting I'm inclined to add it. I don't believe there are technical reasons to use a specific name or format, this should be what's easiest for people to identify. I do think there's value in using the same name as the ID of the script or style element, but that would be fine with or without the inline: prefix.

Right now the source URLs look like this:

  • handle-js-before
  • handle-js-after
  • handle-js-extra
  • handle-inline-css

With the prefix and no other changes they'd be:

  • inline:handle-js-before
  • inline:handle-js-after
  • inline:handle-js-extra
  • inline:handle-inline-css

It seems slightly redundant in the style case inline:handle-inline-css, but in general this adds nice clarity about the provenance.

pento pushed a commit that referenced this pull request Aug 28, 2025
Improve the source locations referenced by developer tooling in supporting browsers. Inline source locations are named like `inline:handle-js-after` and appear in the developer tools "sources" panel.

Developed in #9628.

Props jonsurrell, swissspidy, alshakero, westonruter.
Fixes #63887.


git-svn-id: https://develop.svn.wordpress.org/trunk@60685 602fd350-edb4-49c9-b593-d223f7449a82
Copy link

A commit was made that fixes the Trac ticket referenced in the description of this pull request.

SVN changeset: 60685
GitHub commit: bc10e6b

This PR will be closed, but please confirm the accuracy of this and reopen if there is more work to be done.

@github-actions github-actions bot closed this Aug 28, 2025
@sirreal sirreal deleted the 63887/add-inline-script-style-sourceurl branch August 28, 2025 07:55
markjaquith pushed a commit to markjaquith/WordPress that referenced this pull request Aug 28, 2025
Improve the source locations referenced by developer tooling in supporting browsers. Inline source locations are named like `inline:handle-js-after` and appear in the developer tools "sources" panel.

Developed in WordPress/wordpress-develop#9628.

Props jonsurrell, swissspidy, alshakero, westonruter.
Fixes #63887.

Built from https://develop.svn.wordpress.org/trunk@60685


git-svn-id: http://core.svn.wordpress.org/trunk@60021 1a063a9b-81f0-0310-95a4-ce76da25c4cd
github-actions bot pushed a commit to platformsh/wordpress-performance that referenced this pull request Aug 28, 2025
Improve the source locations referenced by developer tooling in supporting browsers. Inline source locations are named like `inline:handle-js-after` and appear in the developer tools "sources" panel.

Developed in WordPress/wordpress-develop#9628.

Props jonsurrell, swissspidy, alshakero, westonruter.
Fixes #63887.

Built from https://develop.svn.wordpress.org/trunk@60685


git-svn-id: https://core.svn.wordpress.org/trunk@60021 1a063a9b-81f0-0310-95a4-ce76da25c4cd
Copy link

@csswizardry csswizardry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a quick heads up, you may want to be aware of this. Especially at WP’s scale: https://x.com/csswizardry/status/1955262508178043002

@sirreal
Copy link
Member Author

sirreal commented Aug 28, 2025

Thanks, it sounds like we now have a technical reason to avoid the inline: prefix 🙂 I'll do some investigation and prepare a change to remove it.

From the linked tweet:

inline: breaks LoAF attribution so a pattern like //# sourceURL=foo.inline.js is probably better.

Referring to Long Animation Frames API.

@sirreal
Copy link
Member Author

sirreal commented Aug 28, 2025

#9655 removes the inline: prefix.

jonnynews pushed a commit to spacedmonkey/wordpress-develop that referenced this pull request Sep 24, 2025
Improve the source locations referenced by developer tooling in supporting browsers. Inline source locations are named like `inline:handle-js-after` and appear in the developer tools "sources" panel.

Developed in WordPress#9628.

Props jonsurrell, swissspidy, alshakero, westonruter.
Fixes #63887.


git-svn-id: https://develop.svn.wordpress.org/trunk@60685 602fd350-edb4-49c9-b593-d223f7449a82
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants