Commit fa4726f
committed
bug symfony#52519 [AssetMapper] If assets are served from a subdirectory or CDN, also adjust importmap keys (weaverryan)
This PR was merged into the 6.4 branch.
Discussion
----------
[AssetMapper] If assets are served from a subdirectory or CDN, also adjust importmap keys
| Q | A
| ------------- | ---
| Branch? | 6.4
| Bug fix? | yes
| New feature? | no
| Deprecations? | none
| Issues | Fixes symfony#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!
Commits
-------
85c85d3 [AssetMapper] If assets are served from a subdirectory or CDN, also adjust importmap keysFile tree
2 files changed
+11
-0
lines changed- src/Symfony/Component/AssetMapper
- ImportMap
- Tests/ImportMap
2 files changed
+11
-0
lines changedLines changed: 5 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
62 | 62 | | |
63 | 63 | | |
64 | 64 | | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
65 | 70 | | |
66 | 71 | | |
67 | 72 | | |
| |||
Lines changed: 6 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
54 | 54 | | |
55 | 55 | | |
56 | 56 | | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
57 | 61 | | |
58 | 62 | | |
59 | 63 | | |
| |||
92 | 96 | | |
93 | 97 | | |
94 | 98 | | |
| 99 | + | |
| 100 | + | |
95 | 101 | | |
96 | 102 | | |
97 | 103 | | |
| |||
0 commit comments