new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Vite · Guide · all subjects

assets/url-construction

5 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

new URL(url, import.meta.url) for static assets

Combining `import.meta.url` with the native URL constructor can obtain the full, resolved URL of a static asset using relative path from a JavaScript module. Example: `const imgUrl = new URL('./img.png', import.meta.url).href`. This works natively in modern browsers - Vite doesn't need to process this code during development.

Dynamic URLs with new URL() and template literals

The `new URL(url, import.meta.url)` pattern supports dynamic URLs via template literals. Example: `function getImageUrl(name) { return new URL(`./dir/${name}.png`, import.meta.url).href }`. Note that this does not include files in subdirectories.

Vite transforms new URL() in production builds

During the production build, Vite will perform necessary transforms so that URLs from `new URL(url, import.meta.url)` still point to the correct location even after bundling and asset hashing. The URL string must be static so it can be analyzed.

Non-static new URL() patterns not transformed

Vite will not transform `new URL()` if the URL string is not static, for example: `const imgUrl = new URL(imagePath, import.meta.url).href`. If `build.target` does not support `import.meta.url`, this can cause runtime errors.

new URL() does not work with SSR

The `new URL(url, import.meta.url)` pattern does not work if using Vite for Server-Side Rendering, because `import.meta.url` has different semantics in browsers vs. Node.js. The server bundle also cannot determine the client host URL ahead of time.

Give your agent this brain