In a Vite app, what do a plain import of `icons.svg` and its `?url`, `?raw` and `?inline` forms each return?
answer
- all four give a string
- a URL or the file's text
- a size threshold in bytes
- 4096 by default
- suffixes that force the choice
basics
~10 sAll four return strings. A plain import or ?url gives a URL, a hashed file or a data URL below build.assetsInlineLimit (4096 bytes); ?raw gives the file's text; ?inline always gives a data URL.
solid answer
~40 sA plain `import icons from './icons.svg'` returns a URL: `/src/icons.svg` from the dev server, and in the build a hashed file under `assets/`, or a data URL if the file is smaller than `build.assetsInlineLimit`, 4096 bytes by default. `?url` does the same for files Vite does not treat as assets, such as a worklet script. `?raw` returns the file's contents as a string in the JavaScript bundle, which suits an SVG sprite you insert into the DOM. `?inline` always returns a data URL and `?no-inline` never does. Files that must keep their exact name, like `robots.txt`, go in `public/` instead and are referenced by root-absolute path.
code
typescript · 13 linesimport spriteUrl from './icons/reserves-sprite.svg' // URL (or data URL < 4096 B)
import spriteText from './icons/reserves-sprite.svg?raw' // file contents
import otter from './icons/otter.svg?inline' // always a data URL
import pdfWorkerUrl from './pdf/render.ts?worker&url' // URL of the built worker
const holder = document.createElement('div')
holder.hidden = true
holder.innerHTML = spriteText // <symbol> elements now in the DOM
document.body.prepend(holder)
const logo = document.querySelector<HTMLImageElement>('#logo')!
logo.src = otter
console.log(spriteUrl, pdfWorkerUrl)go deeper
Recall that a plain import gives a URL, ?raw gives text and ?inline gives a data URL. Know that public/ files are referenced by root path, not imported.
Explain build.assetsInlineLimit, its 4096-byte default, the rules that force or forbid inlining, and why the dev server inlines small SVGs to match the build.
Pick the form by what the bytes cost: ?raw grows the JavaScript chunk, inlining trades a request for an uncacheable copy, and emitted files cache on their own.
Set a team default for icons and sprites, including the inline limit, so every app makes the same request-versus-bytes trade instead of rediscovering it.
## The four forms An **asset import** is an `import` whose target is not JavaScript. Vite recognises common image, media and font extensions, plus `.webmanifest`, `.pdf` and `.txt`, and a plain default import of one returns a string. Suffixes change what that string is: | Import | Returns | Dev server | Build | |---|---|---|---| | `import icons from './icons.svg'` | URL | `/src/icons.svg` | hashed file under `assets/`, or a data URL below the limit | | `'./icons.svg?url'` | URL | same as plain | same as plain; works for any file type | | `'./icons.svg?raw'` | file contents | the text | the text, placed in the JavaScript chunk | | `'./icons.svg?inline'` | data URL | always inlined | always inlined | | `'./icons.svg?no-inline'` | URL | served file | always emitted as a file | `url()` references in CSS follow the same rules as a plain import, and every emitted file gets a hashed name so it can be cached for a long time. ## When a plain import is inlined In the build, Vite decides per file: 1. `?no-inline` means never inline; `?inline` means always. 2. With `build.lib` set, assets are always inlined, regardless of size. 3. An `.html` file, or an SVG referenced with a `#fragment`, is never inlined; a fragment points into a sprite that is meant to be shared. 4. If `build.assetsInlineLimit` is a function, its `true` or `false` answer decides; returning nothing falls back to the default rule. 5. Otherwise the file is inlined when it is **smaller than** the limit, 4096 bytes by default, and is not a Git LFS placeholder. Setting the limit to `0` turns inlining off. The dev server normally serves the file itself, but it inlines an SVG under the limit too, so that development matches the build. SVGs that are safe to embed become URL-encoded data URLs rather than base64. ## Why the dev URL differs from the build URL The dev server transforms and serves files on request, so an import resolves to the file's own path, such as `/src/icons.svg`. The build copies the file into the output with a content hash in its name and rewrites the import to that URL. Code that hard-codes either path breaks in the other mode; code that uses the imported string works in both. ## Choosing a form for an SVG sprite - **`?raw`** when the page must own the markup: insert the sprite's `<symbol>` elements into the document once, and every `<use href="#icon-otter">` can be styled with CSS. - **Plain import or `?url`** when the sprite should be a separately cached file that markup references by URL. - **`?inline`** for a few tiny icons where saving a request is worth adding the bytes to the importing file. `?raw` text lives inside the JavaScript chunk, so a large sprite imported this way grows the bundle that has to be parsed before the page runs. ## Related suffixes - `?worker` and `?sharedworker` return a constructor for a worker built from the script; `?worker&inline` embeds it as base64 and `?worker&url` returns its URL. - `?url` is also how you get a URL for a file outside the asset list without changing config. ## Where `public/` fits Some files should not be imported at all. Put them in `public/` (the `publicDir` option) when they are never referenced from source, such as `robots.txt`, or must keep their exact file name: - They are served at `/` in development and copied to the root of the output directory as they are, unhashed and untransformed. - Reference them by root-absolute path: `public/donate-banner.png` is `/donate-banner.png`. - When the app is deployed under a sub-path through the `base` option, URLs that Vite sees in imports, CSS and HTML are adjusted for you; a path you build at runtime should start from `import.meta.env.BASE_URL`. - Importing a non-asset file such as JavaScript or CSS from `public/` is rejected with an error that suggests `?url`. For TypeScript, the `vite/client` types declare all of these forms, so `import raw from './icons.svg?raw'` is typed as a string.
- Why is a 3 KB icon inlined in the build when a 5 KB one is emitted as a file?Because `build.assetsInlineLimit` defaults to 4096 bytes: a file smaller than that becomes a base64 or URL-encoded data URL inside the importing file, and a larger one is emitted as a hashed file. Use `?no-inline` or `?inline` to override one import, or set the limit to `0` to stop inlining.
- When should a file go in `public/` rather than be imported?When nothing in source imports it, as with `robots.txt`, or when it must keep its exact name. Files in `public/` are copied unhashed and untransformed and are referenced by root-absolute path such as `/robots.txt`; everything else is better imported so it gets a hashed name and is checked at build time.
- A plain SVG import returns the SVG markup as a string.
- ?raw returns a URL to the file, like ?url does.
- Every imported asset is always inlined as base64.
- Files in public/ are hashed and should be imported like other assets.
- ?inline only inlines a file below the size limit.