Skip to content

🐛 Fix @vite asset URLs on multisite subsites - #544

Merged
retlehs merged 8 commits into
roots:mainfrom
ivowk:fix/vite-multisite-asset-url
Nov 24, 2026
Merged

retlehs merged 8 commits into
roots:mainfrom
ivowk:fix/vite-multisite-asset-url

Conversation

@ivowk

@ivowk ivowk commented May 29, 2026

Copy link
Copy Markdown
Contributor

In a WordPress multisite network, the @Vite directive generates asset tags pointing to the main site's domain instead of the current subsite's domain, causing CORS errors on subsites. This issue has also been discussed on Roots Discourse.

Access to script at 'https://main.example.com/app/themes/my-theme/public/build/assets/app.js'
from origin 'https://sub.example.com' has been blocked by CORS policy.

Vite::assetPath() resolves the URI from assets.manifests.*.url, which is set once at boot from get_theme_file_uri() and never reflects the active subsite context.

home_url() returns the correct origin for the currently active site. The fix is scoped to is_multisite() so single-site installs are completely unaffected.

@retlehs

retlehs commented Jun 1, 2026

Copy link
Copy Markdown
Member

Thanks for the fix. Could you update this to avoid home_url($path)? It breaks subdirectory multisite by inserting the subsite slug into the asset path

Ideally rewrite only the stale host, preserve the path/query, and add tests for subdomain + subdirectory multisite

…multisite and add subdomain/subdirectory tests
@ivowk

ivowk commented Jun 18, 2026

Copy link
Copy Markdown
Contributor Author

@retlehs Updated, thank you for the feedback!

assetPath() now uses a private rewriteHost() helper that takes only the scheme/host/port from home_url() and applies them to the asset URI, while preserving the original path, query, and fragment. This fixes the subdirectory multisite issue where home_url($path) would prepend the subsite slug into the asset path.

See the changes here:

I also added tests for single-site passthrough, subdomain multisite, and subdirectory multisite.

Comment thread src/Roots/Acorn/Assets/Vite.php Resolved
ivowk and others added 2 commits June 19, 2026 07:13
@ivowk
ivowk requested a review from QWp6t June 24, 2026 07:43
@retlehs retlehs changed the title fix: use home_url() for multisite-aware asset paths in @vite directive 🐛 Fix @vite asset URLs on multisite subsites Nov 23, 2026
@retlehs

retlehs commented Nov 23, 2026

Copy link
Copy Markdown
Member

Thanks for the updates! This rewrites every host on multisite, so assets served from a CDN or another domain would get rewritten to the subsite host. Could you only rewrite when the asset host matches the main site's host (e.g. parse_url(network_home_url(), PHP_URL_HOST)), and add a test for a CDN URL passing through unchanged?

The repeated app/config setup in the tests could also be pulled into a helper or dataset

Also, are you using Bedrock? Want to confirm this is coming from WP_CONTENT_URL being defined from WP_HOME

@ivowk

ivowk commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Good catch on the CDN case, I hadn't thought of that. It now only swaps the host when the asset lives on the main site's host (parse_url(network_home_url(), PHP_URL_HOST)), and anything else is left as is. I added a test for a CDN URL too, and cleaned up the tests with a small helper and a dataset so they're less repetitive.

And yes, we're using Bedrock.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@retlehs retlehs left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you!

@retlehs
retlehs merged commit 79cbce3 into roots:main Nov 24, 2026
5 checks passed
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.

3 participants