From 85c85d3c4259724d97d997edc4d5d61ffb91dec0 Mon Sep 17 00:00:00 2001 From: Ryan Weaver Date: Thu, 9 Nov 2023 12:54:45 -0500 Subject: [PATCH] [AssetMapper] If assets are served from a subdirectory or CDN, also adjust importmap keys --- .../Component/AssetMapper/ImportMap/ImportMapRenderer.php | 5 +++++ .../AssetMapper/Tests/ImportMap/ImportMapRendererTest.php | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/src/Symfony/Component/AssetMapper/ImportMap/ImportMapRenderer.php b/src/Symfony/Component/AssetMapper/ImportMap/ImportMapRenderer.php index ef955fca1447c..58bb9c0e347df 100644 --- a/src/Symfony/Component/AssetMapper/ImportMap/ImportMapRenderer.php +++ b/src/Symfony/Component/AssetMapper/ImportMap/ImportMapRenderer.php @@ -62,6 +62,11 @@ public function render(string|array $entryPoint, array $attributes = []): string continue; } + // for subdirectories or CDNs, the import name needs to be the full URL + if (str_starts_with($importName, '/') && $this->assetPackages) { + $importName = $this->assetPackages->getUrl(ltrim($importName, '/')); + } + $preload = $data['preload'] ?? false; if ('css' !== $data['type']) { $importMap[$importName] = $path; diff --git a/src/Symfony/Component/AssetMapper/Tests/ImportMap/ImportMapRendererTest.php b/src/Symfony/Component/AssetMapper/Tests/ImportMap/ImportMapRendererTest.php index a0d90e0cc5c15..6d53e93c8599d 100644 --- a/src/Symfony/Component/AssetMapper/Tests/ImportMap/ImportMapRendererTest.php +++ b/src/Symfony/Component/AssetMapper/Tests/ImportMap/ImportMapRendererTest.php @@ -54,6 +54,10 @@ public function testBasicRender() 'path' => 'https://ga.jspm.io/npm:es-module-shims', 'type' => 'js', ], + '/assets/implicitly-added' => [ + 'path' => '/assets/implicitly-added-d1g35t.js', + 'type' => 'js', + ], ]); $assetPackages = $this->createMock(Packages::class); @@ -92,6 +96,8 @@ public function testBasicRender() $this->assertStringNotContainsString('', $html); // remote js $this->assertStringContainsString('"remote_js": "https://cdn.example.com/assets/remote-d1g35t.js"', $html); + // both the key and value are prefixed with the subdirectory + $this->assertStringContainsString('"/subdirectory/assets/implicitly-added": "/subdirectory/assets/implicitly-added-d1g35t.js"', $html); } public function testNoPolyfill() pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy