Skip to content

[AssetMapper] If assets are served from a subdirectory or CDN, also adjust importmap keys #52519

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

Merged
merged 1 commit into from
Nov 10, 2023

Conversation

weaverryan
Copy link
Member

@weaverryan weaverryan commented Nov 9, 2023

Q A
Branch? 6.4
Bug fix? yes
New feature? no
Deprecations? none
Issues Fixes #52520 (extra points for opening the PR before the issue was submitted? Anyone?)
License MIT

Hi!

Subdirectories & CDN paths are tricky. Suppose you have:

// assets/app.js
import './foo.js';

When we see that relative import to foo.js, we automatically add an entry in the importmap - e.g.:

"/assets/foo.js": "/assets/foo-abcd1234digest.js"

So, when the browser downloads /assets/app-abcd1234diegest.js, it follows the ./foo.js relative path to resolve to /assets/foo.js. It then sees that key in the importmap, and downloads the final file.

However, suppose you're under a subdirectory so that the browser downloads /subdir/assets/app-abcd1234diegest.js. In that case, it will resolve the relative import to /subdir/assets/foo.js. And so, the key in the importmap also needs to have the /subdir inside of it.

Additionally, for a CDN, if the browser downloads https://cdn.com/assets/app.js, then it will resolve the import to https://cdn.com/assets/bar.js. If the key in the importmap is /assets/bar.js, that will NOT be used, as this is interpreted as the URL of the site - e.g. https://myfrontendsite.com/assets/bar.js. So even in this case, the key needs to be the full https://cdn.com/assets/bar.js so that it's used.

I tested this locally on a subdirectory project as well as a CDN, where you set framework.asset.base_urls to some https://cdn.com/ type of URL.

Cheers!

@hashbanged
Copy link
Contributor

Thank you, @weaverryan! Extra points:

extra-points

@fabpot fabpot merged commit fa4726f into symfony:6.4 Nov 10, 2023
This was referenced Nov 10, 2023
@weaverryan weaverryan deleted the asset-mapper-importmap-base-url-key branch November 11, 2023 01:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

AssetMapper's importmap key is missing project prefix when deploying to subdirectory
4 participants