8000 [AssetMapper] Add support for loading JSON using import statements by nicolas-grekas · Pull Request #61133 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Conversation

@nicolas-grekas
Copy link
Member
@nicolas-grekas nicolas-grekas commented Jul 16, 2025
Q A
Branch? 7.4
Bug fix? no
New feature? no
Deprecations? no
Issues -
License MIT

This is inspired by https://web.dev/blog/json-imports-baseline-newly-available

Modern browsers support loading JSONs via the import data from './foo.json' with {type: 'json'} syntax. While this has been promoted as a new baseline, that's still not widely supported.

This PR proposes to add support for a more portable alternative using import jsonPromise from './foo.json' instead, with some server-side assisted loader.
On the client-side, one could then use the imported data by awaiting it first (the import returns a Promise):
json = await jsonPromise

Note that we already support importing css via import statements.

Native support for import './foo.css' with {type: 'css'} exists, but that's even less available.

@carsonbot carsonbot added this to the 7.4 milestone Jul 16, 2025
@nicolas-grekas nicolas-grekas force-pushed the am-json branch 2 times, most recently from a5a31cb to 12eba70 Compare July 16, 2025 08:14
Kocal

This comment was marked as resolved.

@Kocal
Copy link
Member
Kocal commented Jul 16, 2025

Switched to a project with AssetMapper correctly installed, the following code

import json from './foo.json' with {type: 'json'};

console.log(json)

with the patch suggested in #61133 (review) correctly resolves the JSON

@nicolas-grekas
Copy link
Member Author
nicolas-grekas commented Jul 16, 2025

This PR is about this:

-import json from './foo.json' with {type: 'json'};
+import jsonPromise from './foo.json';

and then (note the await):

console.log(await jsonPromise);

@Kocal
Copy link
Member
Kocal commented Jul 16, 2025

Ah, fine then

@nicolas-grekas
Copy link
Member Author

We should skip using the loader when with {type: 'json'}; is in use (same for css BTW).

@tacman
Copy link
Contributor
tacman commented Jul 20, 2025

This looks great! The workflow for loading javascript-routes within a module right now is klunky:

bin/console fos:js-routing:dump --format=js --target=public/js/fos_js_routes.js --callback="export default"

and then

import Routing from "fos-routing";
import RoutingData from "/js/fos_js_routes.js";
Routing.setData(RoutingData);

I think the ux-translator component works the same way, dumping javascript instead of json

https://symfony.com/bundles/ux-translator/current/index.html#configuring-the-dumped-translations

Both of these may make for good examples when writing the documentation or blog post about it.

@fabpot
Copy link
Member
fabpot commented Jul 26, 2025

Thank you @nicolas-grekas.

@fabpot fabpot merged commit c7493bb into symfony:7.4 Jul 26, 2025
10 of 11 checks passed
@nicolas-grekas nicolas-grekas deleted the am-json branch July 28, 2025 10:01
This was referenced Oct 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

0