Vue.js routes serving from subdirectory

17,214

Solution 1

The assetsPublicPath config is just for webpack assets. For vue-router, you need to set the base option in the constructor.

See docs: https://router.vuejs.org/en/api/options.html#base

Solution 2

I have been able to solve this, using the base property of vue-route. In the case of the example it would look like this:

export default new Router({
    mode: 'history',
    base: '/subdir/app/',
    routes: [
    {
        path: '/',
        component: ContentView,
        children: [

My question now is how can I make this subdirectory dynamically. Imagine a single server, and 2 different urls pointing to this server.

www.dominio / app1
www.dominio / app2
Share:
17,214

Related videos on Youtube

Gurnzbot
Author by

Gurnzbot

Updated on September 15, 2022

Comments

  • Gurnzbot
    Gurnzbot over 1 year

    I would like to serve my Vue.js app from a subdirectory on a staging server. For example: http://url.com/subdir/app/

    Right now if I do this and set up the build config assetsPublicPath to serve from that folder, all the assets are served fine but my app does not get routed correctly. The "home" page gets routed to the 'catch-all', and any further routes simply show the normal white-page 404.

    Here is my router:

    export default new Router({
        mode: 'history',
        routes: [
        {
            path: '/',
            component: ContentView,
            children: [
                {
                    path: '/',
                    name: 'DataTable',
                    component: DataTable,
                    meta: { requiresAuth: true }
                },
    
                // ===================================
                //  Login
                // ===================================
                {
                    path: '/login',
                    name: 'AppLogin',
                    component: AppLogin,
                    meta: { checkAlreadyLoggedIn: true }
                },
                {
                    path: '/logout',
                    name: 'AppLogout',
                    component: AppLogout,
                    meta: { requiresAuth: true }
                }
            ]
        },
        {
            path: '*',
            component: ContentView,
            children: [
                {
                    path: '/',
                    name: 'NotFound',
                    component: NotFound
                }
            ]
        }
    ]})
    

    And here is the necessary config/index.js changes: assetsPublicPath: '/subdir/app/'

    In local development the routes work fine. But on the staging server all static assets, the built JS and CSS etc all serve fine. However the home route shows the catch-all. I am assuming it's because my routes are not set up correctly, or because I need to do something to serve from a subdirectory in production.

  • Gurnzbot
    Gurnzbot almost 7 years
    Thank you! I checked the environment and set the base depending on that: base: process.env.environment === 'development' ? '/' : '/subdir/app/'
  • Toskan
    Toskan over 5 years
    so, why not a working example of what he should do? some cryptic documentation only? :S still +1
  • lexa-b
    lexa-b over 4 years
    also, it's necessary to configure "publicPath" webpack param
  • user5855178
    user5855178 about 4 years
    I have yet to see anyone offer a working sample of this issue. I ran into this issue just this week. That base option only gets you part way there.. there is no working sample for this online that i have seen thus far.
  • Tassoman
    Tassoman over 3 years
    In my opinion is a better choice using the vue.config.js file as described in offy vue-cli docs
  • Abraham Putra Prakasa
    Abraham Putra Prakasa over 3 years
    Are you already get any hint for this matter? I also need dynamic multiple subfolder, but using only same one vue.js dist build. Thank you
  • vidgarga
    vidgarga over 3 years
    I receive the base url by parameter: base: (req.getResponseHeader('root-base') == null ? '/' : req.getResponseHeader('root-base')) + 'p'
  • jtalarico
    jtalarico almost 3 years
    Sorry - new to more complex routing issues, so sorry if this is covered. Using base works just fine, but for hitting a route directly or even refreshing the browser on a route path that's not the root (or base) results in a 404. I'm guessing this is because the app won't load initially unless the root is hit first. Any workarounds or does there need to be a rewrite/redirect rule applied on the web server?
  • Markus
    Markus about 2 years
    For router v4 (Vue3), you have to pass the base path to createWebHistory. This worked for me: ... history: createWebHistory(location.pathname), ...
  • Markus
    Markus about 2 years
    try base: location.pathname,
  • Danny Coulombe
    Danny Coulombe about 2 years
    @Markus reply should be the accepted answer.