Skip to content

Commit 85c85d3

Browse files
committed
[AssetMapper] If assets are served from a subdirectory or CDN, also adjust importmap keys
1 parent 5aaa534 commit 85c85d3

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

src/Symfony/Component/AssetMapper/ImportMap/ImportMapRenderer.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ public function render(string|array $entryPoint, array $attributes = []): string
6262
continue;
6363
}
6464

65+
// for subdirectories or CDNs, the import name needs to be the full URL
66+
if (str_starts_with($importName, '/') && $this->assetPackages) {
67+
$importName = $this->assetPackages->getUrl(ltrim($importName, '/'));
68+
}
69+
6570
$preload = $data['preload'] ?? false;
6671
if ('css' !== $data['type']) {
6772
$importMap[$importName] = $path;

src/Symfony/Component/AssetMapper/Tests/ImportMap/ImportMapRendererTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ public function testBasicRender()
5454
'path' => 'https://ga.jspm.io/npm:es-module-shims',
5555
'type' => 'js',
5656
],
57+
'/assets/implicitly-added' => [
58+
'path' => '/assets/implicitly-added-d1g35t.js',
59+
'type' => 'js',
60+
],
5761
]);
5862

5963
$assetPackages = $this->createMock(Packages::class);
@@ -92,6 +96,8 @@ public function testBasicRender()
9296
$this->assertStringNotContainsString('<link rel="stylesheet" href="/subdirectory/assets/styles/app-nopreload-d1g35t.css">', $html);
9397
// remote js
9498
$this->assertStringContainsString('"remote_js": "https://cdn.example.com/assets/remote-d1g35t.js"', $html);
99+
// both the key and value are prefixed with the subdirectory
100+
$this->assertStringContainsString('"/subdirectory/assets/implicitly-added": "/subdirectory/assets/implicitly-added-d1g35t.js"', $html);
95101
}
96102

97103
public function testNoPolyfill()

0 commit comments

Comments
 (0)