Skip to content

fix: use home_url() for multisite-aware asset paths in @vite directive - #544

Open
ivowk wants to merge 4 commits into
roots:mainfrom
ivowk:fix/vite-multisite-asset-url
Open

ivowk wants to merge 4 commits into
roots:mainfrom
ivowk:fix/vite-multisite-asset-url

Conversation

@ivowk

@ivowk ivowk commented May 29, 2026

Copy link
Copy Markdown

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
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 Outdated
ivowk and others added 2 commits June 19, 2026 07:13
@ivowk
ivowk requested a review from QWp6t June 24, 2026 07:43
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