You will notice in the project structure we have two directories for assets:
/src/assets/. What is the difference between them? Some are static assets while the others are processed and embedded by the build system.
So let’s try to answer the question above. We’ll first talk about using regular assets then we’ll see what static assets are.
*.vue components, all your templates and CSS are parsed by
css-loader to look for asset URLs. For example, in
<img src="./logo.png"> and
"./logo.png" is a relative asset path and will be resolved by Webpack as a module dependency.
file-loader to process it. Quasar CLI has already configured these webpack loaders for you, so you basically get features such as filename fingerprinting and conditional base64 inlining for free, while being able to use relative/module paths without worrying about deployment.
Since these assets may be inlined/copied/renamed during build, they are essentially part of your source code. This is why it is recommended to place Webpack-processed assets inside
/src/assets, along side other source files. In fact, you don’t even have to put them all in
/src/assets: you can organize them based on the module/component using them. For example, you can put each component in its own directory, with its static assets right next to it.
Relative URLs, e.g.
./assets/logo.png will be interpreted as a module dependency. They will be replaced with a auto-generated URL based on your Webpack output configuration.
URLs prefixed with
~ are treated as a module request, similar to
require('some-module/image.png'). You need to use this prefix if you want to leverage Webpack’s module resolving configurations. Quasar provides
assets Webpack alias out of the box, so it is recommended that you use it like this:
<img src="~assets/logo.png">. Notice
~ in front of ‘assets’.
Root-relative URLs, e.g.
statics/logo.png are not processed at all. This should be placed in
src/statics/. These won’t be processed by Webpack at all. The statics folder is simply copied over to the distributable folder as-is.
Quasar has some smart algorithms behind the curtains which ensure that no matter what you build (SPA, PWA, Cordova, Electron), your statics are correctly referenced if and only if all your statics start with
statics/ string. For this reason, do not use
/statics as URL.
<!-- Good! -->
Please note that whenever you bind “src” to a variable in your Vue scope, it must be one from the statics folder. The reason is simple: the URL is dynamic, so Webpack (which packs up assets at compile time) doesn’t know which file you’ll be referencing at runtime, so it won’t process the URL.
You can force serving static assets by using
srcas a Vue property. Instead of
:src="'statics/path/to/image'". Please note the usage of single and double quotes.
In order for Webpack to return the correct asset paths, you need to use
require('./relative/path/to/file.jpg'), which will get processed by
file-loader and returns the resolved URL. For example:
Note the above example will include every image under
./bgs/ in the final build. This is because Webpack cannot guess which of them will be used at runtime, so it includes them all.