Controlling static assets with Vite import queries
Choose when Vite imports an asset as text, returns a URL, inlines it, or emits a worker, then inspect the build and development responses.
An image, a shader, and a worker can all look like ordinary imports. Vite’s query suffixes let me choose whether the import supplies text, a URL, an inline resource, or a worker constructor.
The examples use Vite, TypeScript, and pnpm. The documented asset query behavior is shared by the versions referenced in the examples.
Identify the value the caller needs
In Webpack, asset modules or dedicated loaders often express whether a file should be emitted, inlined, or imported as source. In Vite, a supported asset import resolves to a URL by default. In development, that can be the asset path under the dev server. Production builds can emit a named asset or inline it under the configured size limit.
The vite:asset plugin inspects the resource type and query. A shader imported with ?raw becomes text. An explicit ?inline forces an inline resource. ?url selects URL handling, and ?no-inline requests a separate asset instead of a data URL. Vite static asset handling
flowchart TD
A["import asset from './file.ext'"] --> B{"Asset query"}
B -- "?raw" --> C["Read file contents as UTF-8<br/>export default '...'"]
B -- "?url" --> D["Resolve the asset URL<br/>export default '/assets/file-[hash].ext'"]
B -- "?inline" --> E["Force an inline data URL<br/>export default 'data:mime;base64,...'"]
B -- "Default asset import" --> F{"File under assetsInlineLimit?"}
F -- "Yes" --> E
F -- "No" --> D
The default inline limit is 4,096 bytes in the build documented here. The exact result can also depend on the asset type, library mode, or the inline-limit callback. Inspect the generated file before relying on its delivery format.
Import shader source or an independently cacheable URL
For WebGL or Three.js, the shader API typically wants GLSL text. ?raw exposes the file content as a default string export. The build can put that text in the JavaScript that uses it, removing a separate application-level fetch for the shader.
1
2
3
4
5
6
7
8
9
// src/graphics/renderPipeline.ts
import waterVertexShader from './shaders/water.vert?raw';
import waterFragmentShader from './shaders/water.frag?raw';
export function createWaterProgram(gl: WebGL2RenderingContext): WebGLProgram {
const vs = compileShader(gl, gl.VERTEX_SHADER, waterVertexShader);
const fs = compileShader(gl, gl.FRAGMENT_SHADER, waterFragmentShader);
return linkProgram(gl, vs, fs);
}
If the caller needs a URL, ?url tells Vite to import the resource that way even if the extension isn’t recognized by default. Small assets can still use data URLs. Add ?no-inline when an independently cacheable URL is required regardless of size.
1
2
3
4
5
6
7
// src/services/pdfDownloader.ts
import templateThumbnailUrl from '../assets/tiny-preview.png?no-inline';
export function getThumbnailLink(): string {
// Return a separate HTTP URL even for this small image
return templateThumbnailUrl;
}
Base64 increases the uncompressed size of a binary resource by about one third. Inlining saves a request, while emitting a file lets the resource keep its own cache entry. I compare compressed output and browser request behavior before switching an asset that appears in several routes.
Inline a critical asset or give a worker its own chunk
The ?inline query opts a resource into a data URL even if the ordinary size threshold would emit it. A small above-the-fold icon may be a good candidate. A large logo reused across multiple chunks is worth measuring before inlining. SVGs can use URL encoding instead of Base64 depending on the optimizer.
1
2
3
4
5
6
// src/components/CriticalBrandLogo.tsx
import brandLogoDataUri from '../assets/brand-logo.svg?inline';
export function BrandLogo() {
return <img src={brandLogoDataUri} alt="Brand Logo" width={160} height={40} />;
}
The worker query imports a constructor for a worker that Vite processes as a separate entry.
1
2
3
4
5
6
// src/services/computeBridge.ts
// Constructor for the worker emitted in a separate chunk
import ComputeWorker from '../workers/heavyComputation.worker?worker';
const worker = new ComputeWorker();
worker.postMessage({ data: [1, 2, 3] });
Use ?worker&inline to bundle a worker for inline creation, typically through a Blob URL. Before choosing that delivery mode, check the application’s content security policy and whether it permits the resulting worker source. Keep a separate chunk when it should be downloaded and cached independently.
Set the build limit and TypeScript declarations
The build config can adjust the inline size limit and output filenames or extend the set of recognized asset types. This example raises the limit to 8 KB and groups emitted assets under a stable naming scheme.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// vite.config.ts
import { defineConfig } from 'vite';
export default defineConfig({
build: {
// Raise the inline limit from 4 KB to 8 KB
assetsInlineLimit: 8192,
rollupOptions: {
output: {
// Keep emitted asset names consistent
assetFileNames: 'assets/[name]-[hash][extname]',
chunkFileNames: 'assets/[name]-[hash].js',
entryFileNames: 'assets/[name]-[hash].js'
}
}
},
// Recognize additional asset extensions
assetsInclude: ['**/*.gltf', '**/*.glb', '**/*.hdr']
});
TypeScript needs to know that an asset query produces a string or worker constructor. Start with vite/client declarations. Extend ambient module declarations for custom asset patterns that the defaults do not cover.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// src/vite-env.d.ts
/// <reference types="vite/client" />
// The raw query returns a string
declare module '*?raw' {
const content: string;
export default content;
}
// The URL query returns a URL string
declare module '*?url' {
const src: string;
export default src;
}
// The inline query returns a data URL string
declare module '*?inline' {
const dataUri: string;
export default dataUri;
}
Type declarations describe the interface and do not cause a file to be emitted. Keep the declared type aligned with the query behavior in the actual bundler.
Inspect the production output and dev server response
Run pnpm vite build to see which assets are emitted. In this sample, the shader text and the inline badge appear inside JavaScript, while the emitted banner has a hashed URL under dist/assets/.
Figure 1. The production build lists the emitted files separately from the JavaScript that contains inline resources.
When the app uses the dev server at http://localhost:5173, use curl to check the response for each query or inspect the browser’s module requests. A raw shader export should contain GLSL source text. A URL import can export a path such as "/src/assets/logo.svg". The inline badge response can export a string beginning with data:. An import request may include Vite’s import query marker so the server returns an ESM wrapper instead of the raw resource.
Figure 2. Development requests make the query-dependent module responses visible.
I confirm the returned value, whether the production build produced a file, and the request behavior on the route that uses it. That checks the effect of the query in both builds and avoids optimizing the wrong bottleneck.