tc39/proposal-import-attributes

Trouble with JSON Imports in JavaScript

amirfarzamnia opened this issue · 4 comments

This problem is about bringing in JSON modules using the 'import' keyword in JavaScript. The code seems to work well on both mobile and computer devices:

import('./index.json', { assert: { type: 'json' }}).then(({ default: json }) => document.body.appendChild(Object.assign(document.createElement('div'), { innerHTML: json.message })));

However, according to the documentation, the 'assert' option is outdated and has been replaced with the 'with' keyword. So, the code should be like this:

import('./index.json', { with: { type: 'json' }}).then(({ default: json }) => document.body.appendChild(Object.assign(document.createElement('div'), { innerHTML: json.message })));

But now, this code only seems to work on computers and won't work on mobile devices anymore!

I think this is a problem that needs fixing.

What do you mean by "mobile devices"? iOS only supports with, while on Android:

  • Firefox doesn't support either yet, but they are working on with
  • Chrome <= 123 supports assert
  • Chrome >= 120 supports with

In general, be careful when using proposals because they are never supported everywhere. Hopefully in one year it will be ;)

So, this code works fine on Chrome for Windows, but it's not working on the latest version of Chrome on Android. Is this not an issue?

It's not an issue with the proposal, if anything it's an issue with Chrome that didn't release it yet.

Can you go on your Chrome settings, About Chrome (at the end) and check the version?

I got it, my bad.