question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Vue config async support

See original GitHub issue

What problem does this feature solve?

Allow for asynchronous actions within vue.config.js. The specific use-case I’m looking at right now is the need to prerender dynamic routes based on data returned by an api. I’m found a few things online but nothing that appears to work with vue cli 3.x

What does the proposed API look like?

Await the result of the configureWebpack or chainWebpack methods:

const path = require('path')
const axios = require('axios')
const PrerenderSPAPlugin = require('prerender-spa-plugin')

module.exports = {
  configureWebpack: async () => {
    const { data } = await axios.get('http://some-api.com/companies')
    const { companies } = data
    return {
      plugins: [
        new PrerenderSPAPlugin({
          // Required - The path to the webpack-outputted app to prerender.
          staticDir: path.join(__dirname, 'dist'),
          // Required - Routes to render.
          routes: [ '/' ].concat(companies.map(company => `/companies/${company}`))
        })
      ]
    }
  }
}

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:27
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

44reactions
LinusBorgcommented, Jan 22, 2019

That could make sense, but would also be a lot of work and possibly a breaking change. We will have to have a thorough look at this.

Meanwhile, you can use a local plugin file to write your own wrapper around build and do the async fetching before actually running build:

package.json:

"vuePlugins": {
  "service": ["build.prerender.js"]
  }

build.prerender.js

module.exports = (api, options) => {
  api.registerCommand('build:prerender', async (args) => {
    const PrerenderSPAPlugin = require('prerender-spa-plugin')
    const { data } = await axios.get('http://some-api.com/companies')
    const { companies } = data
    api.chainWebpack(config => {
      config.plugin('prerender').use(PrerenderSPAPlugin, [{
          // Required - The path to the webpack-outputted app to prerender.
          staticDir: path.join(__dirname, 'dist'),
          // Required - Routes to render.
          routes: [ '/' ].concat(companies.map(company => `/companies/${company}`))
        }])
    })
    
    await api.service.run('build', args)
  })
}

module.exports.defaultModes = {
  'build:prerender': 'production'
}

Usage in package.json:

"scripts": {
  "build": "vue-cli-service build:prerender"
}
5reactions
patarapolwcommented, Aug 21, 2019

If need both build:async and serve:async, currently, I have to… (in vue.async.config.js)

function configureWebpack(webpackConfig) {
  ...
}

function defineWebpack(webpackConfig, def) {
  webpackConfig.plugin("define").tap((args) => {
    args[0] = {
      ...args[0],
      ...def
    }
    return args;
  });
}

async function generateDefinitions() {
  await ...
}

module.exports = (api, options) => {
  api.registerCommand('build:async', async (args) => {
    const def = await generateDefinitions();

    api.configureWebpack(configureWebpack);
    api.chainWebpack((webpackConfig) => {
      defineWebpack(webpackConfig, def);
    });
    
    await api.service.run('build', args)
  }),
  api.registerCommand('serve:async', async (args) => {
    const def = await generateDefinitions();

    api.configureWebpack(configureWebpack);
    api.chainWebpack((webpackConfig) => {
      defineWebpack(webpackConfig, def);
    });

    await api.service.run('serve', args)
  })
}

module.exports.defaultModes = {
  'build:async': 'production',
  'serve:async': 'development'
}

There should be a single Vue Config object, therefore, a more elegant way to do this…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Asynchronous Loading of vue.config.js for Pre-Rendering ...
The configureWebpack option doesn't support Promises yet, meanwhile you can fetch the data before running the build.
Read more >
Async Components - Vue.js
The resulting AsyncComp is a wrapper component that only calls the loader function when it is actually rendered on the page. In addition,...
Read more >
How to Use Async and Await with Vue.js Apps - Medium
async and await are great ways to chain promises. The syntax is much more convenient than chaining then functions. The only thing you...
Read more >
Coding Better Composables: Async Without Await (5/5)
The setup function will return when it runs into an await statement. ... Let's see how some VueUse composables implement this pattern.
Read more >
Handling Asynchrony in Vue 3 / Composition API
If Suspense is about to be used, we can start right away with using async / await directly in the setup function: But...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found