In a Vite app, should a 2 MB GeoJSON file be imported as JSON or fetched by URL, and what must change for `.geojson`?
answer
- data inside the chunk or beside it
- named exports from root fields
- a parse-speed trick above 10 kB
- an extension Vite does not know
- extend the asset list or add a suffix
basics
~20 sA JSON import puts the data inside a JavaScript chunk; a ?url import keeps it in its own cached file, so fetch 2 MB. Vite treats only .json as JSON: rename the file, use ?url, or add .geojson to assetsInclude.
solid answer
~40 sA `.json` import becomes a JavaScript module: the default export is the whole object, root fields are named exports while `json.namedExports` is on (the default), and with `json.stringify: 'auto'` data over 10 kB is emitted as `JSON.parse("…")`, which parses faster than an object literal. Either way the 2 MB lives inside a chunk, is parsed before the module runs and is downloaded again whenever that chunk changes. Fetching keeps it apart: `import reservesUrl from './reserves.geojson?url'` emits a hashed file you `fetch()` when the map opens. Vite does not recognise `.geojson` as JSON or as an asset, so either rename the file to `.json`, use `?url` or `?raw`, or add `'**/*.geojson'` to `assetsInclude` so a plain import returns its URL.
go deeper
Recall that a .json import gives you the parsed data and that ?url gives you a file to fetch. Know that Vite recognises files by extension.
Explain json.namedExports, json.stringify's 10 kB auto rule and why a .geojson file needs a rename, a suffix or an assetsInclude pattern.
Decide by cost: bundled data is parsed on every load and re-downloaded with its chunk, while a fetched, hashed file caches on its own and loads only when needed.
Set a size and update-frequency rule for data files across apps, so large datasets ship as fetched assets and small constants stay bundled.
## Two ways a data file can ship A data file can travel **inside** JavaScript or **beside** it. - **Imported as JSON**, it becomes a module whose exports are the data. The bytes sit inside a chunk, arrive with the code that imports it, and are parsed before that code runs. - **Imported as a URL**, it is emitted as its own file with a hashed name, and the code fetches it when it is needed. The browser caches it separately, so a code change does not download the data again, and a data change does not invalidate the code. For a 2 MB set of reserve boundaries that only the map view needs, the second is usually right. For a small lookup table that every page needs immediately, the first is simpler. ## What a JSON import produces Vite treats `.json` files as JSON modules: - The **default export** is the whole parsed value: `import reserves from './reserves.json'`. - With **`json.namedExports`** on (the default, `true`), each root field is also a named export, so `import { features } from './reserves.json'` works, which helps tree-shaking. - **`json.stringify`** defaults to `'auto'`: when the data is bigger than 10 kB, Vite emits `export default JSON.parse("…")`, because engines parse a JSON string faster than an equivalent object literal. `true` always does this and `false` never does. None of this changes where the bytes go: a JSON import's data is part of a chunk, whichever form the chunk uses. ## Why `.geojson` needs a decision Vite decides how to treat a file by its extension: | Extension | How Vite treats a plain import | |---|---| | `.json` | a JSON module with the exports described above | | a built-in asset type such as `.png`, `.svg` or `.pdf` | a URL string | | a pattern listed in `assetsInclude` | a URL string | | anything else, such as `.geojson` | neither JSON nor asset handling applies | So `reserves.geojson` needs one of these: 1. **Rename it to `.json`** if you want it bundled as data. 2. **Add a suffix** at the import: `?url` for a URL to fetch, or `?raw` for the text to `JSON.parse` yourself, which still bundles the bytes. 3. **Extend the asset list** with `assetsInclude: ['**/*.geojson']`, so every plain import of that type returns a URL and the file also skips the transform pipeline when HTML or a `fetch` requests it. ## Loading it on demand With a URL, the map view loads the data when it opens: ```ts import reservesUrl from './data/reserves.geojson?url' export async function loadReserves() { const res = await fetch(reservesUrl) return res.json() } ``` The emitted name is hashed, so it can be cached for a long time and a new version gets a new URL. At 2 MB the file is far above `build.assetsInlineLimit` (4096 bytes), so Vite emits it rather than turning it into a data URL. ## Where `public/` fits Putting the file in `public/` and fetching `/data/reserves.geojson` also works, but the name is not hashed, so you must manage its caching yourself. Choose `public/` only when another system needs that exact URL. ## A decision checklist 1. **Size**: a few kilobytes of constants bundle well; megabytes belong in their own file. 2. **When it is needed**: data every page needs at start-up can ride in the entry chunk; data one view needs should load with that view. 3. **How often it changes**: data that changes on a different schedule from the code should not force visitors to download the code again. 4. **Who else reads it**: if another system needs a fixed URL, `public/` is the honest choice. `?raw` sits between the two: the bytes are still bundled, but you parse them yourself, which suits formats Vite does not parse. ## Pitfalls - Importing a large dataset with JSON imports and then wondering why the first render is slow: the whole file is in the critical chunk. - Expecting `json.stringify` to shrink the download; it targets parse speed, not size. - Adding a broad pattern such as `'**/*.json'` to `assetsInclude`, which would turn every JSON import into a URL.
- Does `json.stringify` make a large JSON import smaller to download?No. It changes the emitted form to `JSON.parse("…")`, which engines parse faster than an object literal, but the bytes are roughly the same. With the default `'auto'`, Vite applies it only when the data is bigger than 10 kB.
- What does adding `'**/*.geojson'` to `assetsInclude` change besides the import's return value?Matching files are treated as static assets everywhere: a plain import from JavaScript returns their URL, and when HTML references them or code requests them with `fetch`, Vite serves them without running the plugin transform pipeline. A plugin with `enforce: 'pre'` can still handle the type differently.
- Vite parses any file containing JSON as JSON, whatever its extension.
- A JSON import is fetched lazily at runtime, like a URL import.
- json.stringify compresses the JSON so the download gets smaller.
- Named imports from a JSON file are not supported in Vite.
- A 2 MB file imported with ?url is inlined as a data URL.