Your Static Host Can Silently Break Import Attributes

2026-12-03来源 dev.toEnglish

Your local dev server has been quietly lying to you.

You write import config from './settings.json' with { type: 'json' };, run it locally, and it works — the object shows up, no bundler, no fetch, no JSON.parse. You ship it. Same code, same file, same browser. It throws.

Nothing in your diff changed. The only thing that's different is which server answered the request — and what header it decided to send back.

// works on your machine import config from './settings.json' with { type: 'json' }; console.log(config.apiBase); 

This is a real, newly-usable browser feature — import attributes — and it comes with a gotcha specific enough that it will cost someone a deploy before they learn it from a blog post instead of a stack trace.

What actually shipped

For as long as ES modules have existed, importing a JSON file meant one of three things: let your bundler's loader rewrite it into JS at build time, fetch() it and await res.json(), or — in Node — just require() it and hope you never ship that file over the network. There was no way to say "this import is JSON" directly in module syntax, because the module system had no concept of a non-JavaScript module at all.

Import attributes close that gap. You add a with { type: 'json' } clause to the import, and the module loader treats the target as data, not code:

// static import import config from './settings.json' with { type: 'json' }; // dynamic import const mod = await import('./settings.json', { with: { type: 'json' } }); console.log(mod.default.apiBase); 

This is a Baseline newly available feature — it's been shipping in Chrome since version 123 (March 2024), and Firefox and Safari have since caught up, so all three engines understand the syntax today. "Newly available" is doing real work in that sentence, though: Baseline doesn't call this widely available until roughly 30 months of universal support have passed, which lands around late 2027. If any part of your audience is on an older point release, check before you rely on it as the only path.

🎮 Try it yourself

▶️ Open the interactive playground →

Runs right in your browser — poke at it and watch the concept react live.

The obvious fix that isn't one

Say your import throws in production. The instinctive first move is to assume you wrote the import wrong, so you try variations:

// "maybe I need the old syntax?" import config from './settings.json' assert { type: 'json' }; // "maybe I should destructure what I need?" import { apiBase } from './settings.json' with { type: 'json' }; 

Both are wrong, for different reasons, and neither error message tells you the actual problem.

assert { type: 'json' } is the previous version of this proposal. MDN now documents it as non-standard — the syntax moved from assert to with as the feature advanced through standardization, and engines are converging on with as the only one that keeps working. If a tutorial you're reading uses assert, it predates the rename.

The named import is wrong for a completely different reason that has nothing to do with syntax age: JSON modules only ever have a default export. There is no mechanism for turning top-level JSON keys into named bindings — a JSON document doesn't have "exports," it has one value, and that value is what import config from … binds to. import { apiBase } from … isn't almost-right. It's not a thing the spec defines, for any version of the syntax.

Neither of those was the actual bug, though. The actual bug was never in the import statement at all.

The real diagnosis: the browser doesn't trust your file extension

Here's the part that matters more than the syntax: a browser does not decide a response is JSON because the URL ends in .json. It decides based on one thing — the Content-Type header the server actually sent back with the response.

Your local dev server (Vite, the Next.js dev server, http-server, whatever) almost certainly serves .json files with Content-Type: application/json out of the box, because its MIME-type table knows the extension. That's why it works locally without you ever thinking about it.

Plenty of production hosts don't make that same choice as reliably. A plain static file server with an incomplete or custom MIME map, an S3-style bucket where metadata was set by whatever uploaded the file, a reverse proxy with a default_type fallback — any of these can hand back text/plain, application/octet-stream, or nothing at all for a .json file, and still serve the bytes correctly. Your file is fine. Your JSON is valid. The header lies about what it is, and the module loader takes the header's word over the filename's.

Open your browser's network tab, request the same .json file from your production host, and check the response headers. If Content-Type isn't a JSON type — application/json, or anything ending in +json — that import was never going to succeed there, no matter how you spelled the import statement.

# this response makes the import throw, regardless of the file's actual contents HTTP/1.1 200 OK Content-Type: text/plain 

The check is strict on purpose. It's the same philosophy that already governs <script type="module">: browsers will not execute a response as a module unless the server explicitly labels it correctly, because a lenient content-sniffing rule for executable content is a script-injection vector waiting to happen. JSON modules inherited that same strictness even though JSON itself can't execute anything — consistency won over convenience here, and once you know that's the tradeoff, the error stops being mysterious.

The fix

There's no workaround in the import statement, because the import statement was never the problem. The fix lives entirely in server configuration: make sure whatever serves your JSON file sends a JSON Content-Type.

# nginx: make sure .json has an explicit, correct type location ~ \.json$ { default_type application/json; } 
// Node static-file servers: the `mime`/`mime-types` package usually // already maps .json correctly — the bug is almost always a custom // static handler that skips it, or an object-storage upload that // never set metadata at all. res.setHeader('Content-Type', 'application/json'); 

Once the header is right, the same import statement that threw in production works identically to how it worked on your machine — because now, for the first time, the two environments are actually answering the same question the same way.

What to check before you ship this

If you're adopting import attributes for the first time, don't just test the import locally and call it done — that's testing against the one server in the whole chain that was never going to lie to you. Check the response headers your production host sends for the exact files you're importing, on the exact path readers will hit. It's a thirty-second check in the network tab that replaces a very confusing on-call page.

And when you do reach for this, remember the shape of what you're allowed to get back: one default export, nothing named, and only if the server agrees with the file extension. Get the header right and this finally kills the fetch-then-JSON.parse dance for static config and data files — no bundler required.

🧠 Test yourself

Think it clicked? Take the 8-question quiz →

Instant feedback, a hint on every question, and an explanation for each answer — right or wrong.

Have you checked what Content-Type your own static host actually sends for a .json file? Open the network tab before you find out the hard way.

🚀 Want more like this? Every guide, playground, and quiz lives on bestpractic.org — open it and sign up free so the next one finds you.

Thanks for reading! Let's stay connected:

原文:https://www.a8q9.cn/en/article/your-static-host-can-silently-break-import-attributes-498661.html

相关文章