Skip to content

Commit 2427ec3

Browse files
committed
Update Image Assets page
1 parent d989132 commit 2427ec3

File tree

1 file changed

+53
-9
lines changed

1 file changed

+53
-9
lines changed

docs/guides/working-with-image-assets.md

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Another way you might encounter the same problem is trying to pass the path to a
2929

3030
## Assets and Build Tools
3131

32-
You'll encounter the same problem with both Vite and Vue CLI (webpack).
32+
You'll encounter the same problem with both Vite and the old Vue CLI (webpack).
3333

3434
Both tools have a convention of putting assets such as images in the folder `/src/assets`, but there's no special handling for that folder in those tools. Assets need to be imported into your JS/TS code, just like other dependencies. If they aren't imported then they'll be discarded as part of the tree-shaking process and won't be included in the final build output.
3535

@@ -50,6 +50,8 @@ import imgSrc from '../assets/image.png'
5050
</template>
5151
```
5252

53+
If you've configured a [`base`](https://vitejs.dev/guide/build.html#public-base-path) path (Vite) or a [`publicPath`](https://cli.vuejs.org/config/#publicpath) (Vue CLI) then that will also be automatically added to the imported path string.
54+
5355
## Why Does a Static `src` Work?
5456

5557
So if images need to be imported, why does `<img src="../assets/image.png">` work just fine?
@@ -231,21 +233,63 @@ Both Vite and Vue CLI have support for a `/public` folder. This is a special fol
231233

232234
Using `/public` can be a good option if there are a lot of images that never change, making the hashing unnecessary. However, it does come with its own problems.
233235

234-
Let's assume we put our images in `/public/images`. We won't be able to reference them with static strings in the template, because those will be automatically converted to imports:
236+
In the examples that follow, let's assume we've put our images in `/public/images`.
237+
238+
### `/public` with Vite
239+
240+
If we're using Vite, the special handling for tags like `<img src>` carries across to files in `/public`. We can use static paths, we just need to start the path with a `/`:
235241

236242
```vue-html
237-
<!-- This will fail, files in /public can't be imported -->
243+
<!-- With Vite this will work fine -->
238244
<img src="/images/image.png">
239245
```
240246

241-
To get around that, we can trick the tools by using `:src` instead of `src`:
247+
Note that we **don't** include `/public` in the path.
248+
249+
The Vite plugin has special handling for files beginning with `/`. It'll first check whether the file exists in the `/public` folder, then fall back to an import if the file wasn't found. If you've configured a [`base`](https://vitejs.dev/guide/build.html#public-base-path) path, Vite will rewrite the attribute path accordingly.
250+
251+
If we're working with dynamic paths, or static paths on tags the plugin doesn't understand, then we have to apply the `base` path ourselves.
252+
253+
For example, if we have `base: '/my-app/'` in our Vite config, we can access that path using `import.meta.env.BASE_URL`:
254+
255+
```vue
256+
<script setup>
257+
// This gives us access to the `base` config option
258+
const base = import.meta.env.BASE_URL
259+
260+
defineProps(['name'])
261+
</script>
262+
263+
<template>
264+
<img :src="`${base}images/${name}.png`" />
265+
</template>
266+
```
267+
268+
You can read more about the `/public` folder in [the Vite docs](https://vitejs.dev/guide/assets.html#the-public-directory).
269+
270+
### `/public` with Vue CLI
271+
272+
To use the `/public` folder with Vue CLI, we need to apply the [publicPath](https://cli.vuejs.org/config/#publicpath) ourselves, even for static paths.
273+
274+
For example, the following code will only work if `publicPath` is set to `/`:
242275

243276
```vue-html
244-
<!-- Using :src to bypass the automatic import -->
245-
<img :src="'/images/image.png'">
277+
<img src="/images/image.png">
246278
```
247279

248-
We also need to be careful if we're using a [base path (Vite)](https://vitejs.dev/guide/build.html#public-base-path) or [publicPath (Vue CLI / webpack)](https://cli.vuejs.org/config/#publicpath). Those won't be automatically prepended to the URL, so we need to do it ourselves.
280+
Vue CLI won't rewrite this path at all, it'll just be left as-is.
281+
282+
We can access the `publicPath` using `process.env.BASE_URL`:
283+
284+
```vue
285+
<script setup>
286+
// This gives us access to the `publicPath` config option
287+
const base = process.env.BASE_URL
288+
</script>
289+
290+
<template>
291+
<img :src="`${base}images/image.png`" />
292+
</template>
293+
```
249294

250-
* Read more about the `/public` folder in [the Vite docs](https://vitejs.dev/guide/assets.html#the-public-directory).
251-
* Read more about the `/public` folder in [the Vue CLI docs](https://cli.vuejs.org/guide/html-and-static-assets.html#the-public-folder).
295+
You can read more about the `/public` folder in [the Vue CLI docs](https://cli.vuejs.org/guide/html-and-static-assets.html#the-public-folder).

0 commit comments

Comments
 (0)