Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in a nuxtjs spa, how to link the assets with relative urls?

When a nuxtjs app in spa mode is built, the assets are referenced at /_nuxt/vendor.a0f2fda15695e202a186.js in index.html.

Is it possible to reference to those files with a relative url (without the first slash)?

(something like <script type="text/javascript" src="_nuxt/vendor.a0f2fda15695e202a186.js"></script>)

like image 815
François Romain Avatar asked Jan 11 '18 12:01

François Romain


People also ask

How do I link to a relative URL?

To link pages using relative URL in HTML, use the <a> tag with href attribute. Relative URL is used to add a link to a page on the website. For example, /contact, /about_team, etc.

What is a relative path URL?

A relative URL is a URL that only includes the path. The path is everything that comes after the domain, including the directory and slug. Because relative URLs don't include the entire URL structure, it is assumed that when linking a relative URL, it uses the same protocol, subdomain and domain as the page it's on.

When would you use a relative URL?

Relative URLs are more convenient because they are shorter and often more portable. However, you can use them only to reference links on the same server as the page that contains them.

What is absolute URL and relative URL?

An absolute URL contains all the information necessary to locate a resource. A relative URL locates a resource using an absolute URL as a starting point. In effect, the "complete URL" of the target is specified by concatenating the absolute and relative URLs.


1 Answers

1. Is it possible to reference to those files with a relative url?

Probably not.

See https://github.com/nuxt/nuxt.js/issues/1380

2. The real problem

The reason you want a "relative path" to your assets is because your SPA (or your assets) are not at the root directory of your domain. For example:

Your SPA is NOT at https://my-domain.com/

But at https://my-domain.com/my-app/

3. The solution

Go to nuxt.config.js and edit the following properties.

build: {
    // Set the absolute path where the assets are.
    // https://nuxtjs.org/api/configuration-build#publicpath
    publicPath: 'https://my-domain.com/my-app/',
},

router: {
    // Set the "Base" of the router.
    // https://router.vuejs.org/en/api/options.html#base
    base: '/my-app/'
},

link: [
    // Do this if you have files that are located at /static/
    // https://nuxtjs.org/guide/assets#static
    {
        rel: 'stylesheet',
        type: 'text/css',
        href: '/my-app/myStyleSheet.css'
    }
]

4. Reference

  • https://nuxtjs.org/api/configuration-build#publicpath
  • https://router.vuejs.org/en/api/options.html#base
  • https://nuxtjs.org/guide/assets#static
like image 161
user2875289 Avatar answered Oct 02 '22 01:10

user2875289