Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit db3c26b

Browse filesBrowse files
bug #64475 [AssetMapper] Render an empty import map as a JSON object (ousamabenyounes)
This PR was merged into the 6.4 branch. Discussion ---------- [AssetMapper] Render an empty import map as a JSON object | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix #64422 | License | MIT When no entrypoint is passed (`{{ importmap([]) }}`) or the import map is empty, `ImportMapRenderer` rendered the `imports` value as a JSON array: ```json { "imports": [] } ``` The import map specification requires `imports` to be a JSON **object**, so browsers reject the empty array: > Uncaught TypeError: Failed to parse import map: "imports" top-level key must be a JSON object. An empty map is now serialized as a JSON object (`{}`); non-empty maps are unchanged (an associative array already encodes as an object). Commits ------- 33855da [AssetMapper] Render an empty import map as a JSON object
2 parents a61ca2d + 33855da commit db3c26b
Copy full SHA for db3c26b

2 files changed

+16-1Lines changed: 16 additions & 1 deletion

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/AssetMapper/ImportMap/ImportMapRenderer.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public function render(string|array $entryPoint, array $attributes = []): string
9696
}
9797

9898
$scriptAttributes = $this->createAttributesString($attributes);
99-
$importMapJson = json_encode(['imports' => $importMap], \JSON_THROW_ON_ERROR | \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES | \JSON_HEX_TAG);
99+
$importMapJson = json_encode(['imports' => $importMap ?: new \stdClass()], \JSON_THROW_ON_ERROR | \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES | \JSON_HEX_TAG);
100100
$output .= <<<HTML
101101
102102
<script type="importmap"$scriptAttributes>
Collapse file

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/AssetMapper/Tests/ImportMap/ImportMapRendererTest.php
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,4 +204,19 @@ public function testItAddsPreloadLinks()
204204
$this->assertSame(['as' => 'style'], $linkProvider->getLinks()[0]->getAttributes());
205205
$this->assertSame('/assets/styles/app-preload-d1g35t.css', $linkProvider->getLinks()[0]->getHref());
206206
}
207+
208+
public function testEmptyImportMapRendersAsJsonObject()
209+
{
210+
$importMapGenerator = $this->createMock(ImportMapGenerator::class);
211+
$importMapGenerator->expects($this->once())
212+
->method('getImportMapData')
213+
->with([])
214+
->willReturn([]);
215+
216+
$renderer = new ImportMapRenderer($importMapGenerator);
217+
$html = $renderer->render([]);
218+
219+
$this->assertStringContainsString('"imports": {}', $html);
220+
$this->assertStringNotContainsString('"imports": []', $html);
221+
}
207222
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.