Skip to content

Commit a68a569

Browse files
committed
feat(asset-mapper): document support for importing JSON assets in AssetMapper
1 parent 537d2d6 commit a68a569

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

frontend/asset_mapper.rst

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,81 @@ the page. If you use a dynamic import to lazily-load a JavaScript file and that
510510
file imports a CSS file (using the non-dynamic ``import`` syntax), that CSS file
511511
will also be downloaded asynchronously.
512512

513+
Importing JSON files
514+
--------------------
515+
516+
.. versionadded:: 7.4
517+
518+
Support for importing JSON assets was added in Symfony 7.4.
519+
520+
AssetMapper allows you to import JSON assets directly from your JavaScript code.
521+
While modern browsers support the native ``import data from './foo.json' with { type: 'json' }`` syntax,
522+
this feature is not yet widely supported. AssetMapper provides a fully compatible alternative
523+
without requiring any bundler.
524+
525+
AssetMapper provides a more compatible alternative by allowing you to import JSON
526+
files using the standard import syntax:
527+
528+
.. code-block:: javascript
529+
530+
// assets/app.js
531+
import dataPromise from './data.json';
532+
533+
// The import returns a Promise that resolves to the JSON content
534+
const data = await dataPromise;
535+
console.log(data.name); // Access your JSON data
536+
537+
Usage Example
538+
~~~~~~~~~~~~~
539+
540+
Consider a JSON file containing user data:
541+
542+
.. code-block:: json
543+
544+
{
545+
"users": [
546+
{"id": 1, "name": "Alice", "email": "[email protected]"},
547+
{"id": 2, "name": "Bob", "email": "[email protected]"}
548+
]
549+
}
550+
551+
You can import and use this data in your JavaScript:
552+
553+
.. code-block:: javascript
554+
555+
// assets/controllers/user-list-controller.js
556+
import usersPromise from '../data/users.json';
557+
558+
async function displayUsers() {
559+
const userData = await usersPromise;
560+
561+
userData.users.forEach(user => {
562+
console.log(`${user.name}: ${user.email}`);
563+
});
564+
}
565+
566+
displayUsers();
567+
568+
How it Works
569+
~~~~~~~~~~~~
570+
571+
When you import a JSON file, AssetMapper:
572+
573+
1. **Detects the JSON import** in your JavaScript files during asset processing
574+
2. **Creates a JavaScript module** that exports a Promise resolving to the JSON content
575+
3. **Adds it to the importmap** so your browser can locate and load it correctly
576+
4. **Versions the file** like any other asset, ensuring proper cache busting
577+
578+
This approach works in all modern browsers without requiring native JSON import
579+
support, making it a reliable alternative to the newer ``with { type: 'json' }``
580+
syntax.
581+
582+
.. note::
583+
584+
Like CSS imports, JSON imports are processed by AssetMapper and added to the
585+
importmap automatically. The imported JSON assets are also versioned, so any
586+
changes to the JSON content will result in a new versioned filename.
587+
513588
Issues and Debugging
514589
--------------------
515590

0 commit comments

Comments
 (0)