Skip to content

[AssetMapper] Add support for loading JSON using import statements #61133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 26, 2025

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
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
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