A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://cli.vuejs.org/guide/html-and-static-assets.html below:

HTML and Static Assets | Vue CLI

HTML and Static Assets # HTML # The Index File #

The file public/index.html is a template that will be processed with html-webpack-plugin. During build, asset links will be injected automatically. In addition, Vue CLI also automatically injects resource hints (preload/prefetch), manifest/icon links (when PWA plugin is used), and the asset links for the JavaScript and CSS files produced during the build.

Interpolation #

Since the index file is used as a template, you can use the lodash template syntax to interpolate values in it:

In addition to the default values exposed by html-webpack-plugin, all client-side env variables are also available directly. For example, to use the BASE_URL value:

<link rel="icon" href="<%= BASE_URL %>favicon.ico">

See also:

Preload #

<link rel="preload"> is a type of resource hint that is used to specify resources that your pages will need very soon after loading, which you therefore want to start preloading early in the lifecycle of a page load, before the browser's main rendering machinery kicks in.

By default, a Vue CLI app will automatically generate preload hints for all files that are needed for the initial rendering of your app.

The hints are injected using @vue/preload-webpack-plugin and can be modified / deleted via chainWebpack as config.plugin('preload').

Prefetch #

<link rel="prefetch"> is a type of resource hint that tells the browser to prefetch content that the user may visit in the near future in the browser's idle time, after the page finishes loading.

By default, a Vue CLI app will automatically generate prefetch hints for all JavaScript files generated for async chunks (as a result of on-demand code splitting via dynamic import()).

The hints are injected using @vue/preload-webpack-plugin and can be modified / deleted via chainWebpack as config.plugin('prefetch').

Note for multi page setups

When using a multipage setup, the plugin name above should be changed to match the structure 'prefetch-{pagename}', for example 'prefetch-app'.

Example:


module.exports = {
  chainWebpack: config => {
    
    config.plugins.delete('prefetch')

    
    
    config.plugin('prefetch').tap(options => {
      options[0].fileBlacklist = options[0].fileBlacklist || []
      options[0].fileBlacklist.push(/myasyncRoute(.)+?\.js$/)
      return options
    })
  }
}

When the prefetch plugin is disabled, you can manually select specific chunks to prefetch using webpack's inline comments:

import( './someAsyncComponent.vue')

webpack's runtime will inject prefetch links when the parent chunk is loaded.

TIP

Prefetch links will consume bandwidth. If you have a large app with many async chunks and your users are primarily mobile and thus bandwidth-aware, you may want to disable prefetch links and manually select chunks to prefetch.

Disable Index Generation #

When using Vue CLI with an existing backend, you may need to disable the generation of index.html so that the generated assets can be used in a server-rendered page. To do so, the following can be added to vue.config.js:


module.exports = {
  
  filenameHashing: false,
  
  chainWebpack: config => {
    config.plugins.delete('html')
    config.plugins.delete('preload')
    config.plugins.delete('prefetch')
  }
}

However, this is not really recommended because:

Instead, you should consider using the indexPath option to use the generated HTML as a view template in your server-side framework.

Building a Multi-Page App #

Not every app has to be an SPA. Vue CLI supports building a multi-paged app using the pages option in vue.config.js. The built app will efficiently share common chunks between multiple entries for optimal loading performance.

Static Assets Handling #

Static assets can be handled in two different ways:

Relative Path Imports #

When you reference a static asset using relative path (must start with .) inside JavaScript, CSS or *.vue files, the asset will be included into webpack's dependency graph. During this compilation process, all asset URLs such as <img src="...">, background: url(...) and CSS @import are resolved as module dependencies.

For example, url(./image.png) will be translated into require('./image.png'), and

will be compiled into:

h('img', { attrs: { src: require('./image.png') }})

Internally, we configured webpack Assets Modules to determine the final file location with version hashes and correct public base paths, and conditionally inline assets that are smaller than 8KiB, reducing the amount of HTTP requests.

You can adjust the inline file size limit via chainWebpack. For example, to set the limit of inline images to 4KiB instead:


module.exports = {
  chainWebpack: config => {
    config.module
      .rule('images')
        .set('parser', {
          dataUrlCondition: {
            maxSize: 4 * 1024 
          }
        })
  }
}
URL Transform Rules # The public Folder #

Any static assets placed in the public folder will simply be copied and not go through webpack. You need to reference them using absolute paths.

Note we recommend importing assets as part of your module dependency graph so that they will go through webpack with the following benefits:

The public directory is provided as an escape hatch, and when you reference it via absolute path, you need to take into account where your app will be deployed. If your app is not deployed at the root of a domain, you will need to prefix your URLs with the publicPath:

When to use the public folder #

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4