Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this possible to configure publicPath in vue-cli dynamically in runtime?

Is this possible to change dynamically publicPath for vue application (in vue.config)? This means that I want to set public path be dynamically depends on real url(some domain), e.g. i want to make only one build, but use it for staging and prod env (and use different cdn-s for assets in different countries, etc). I find that webpack_public_path aims at setting at runtime the public path. But how can I use webpack_public_path with vueJS(vue-cli)?

webpack_public_path - didn't work for me. Maybe someone can provide a real example in repo?

Expected result: if I build once app in "production" mode, deploy (artifact) app in several environments, each environment setting his own publicPath at runtime (cdn). E.g. for prod artifact on domain test.com in Europe - used cdn: test-cdn-europe.com, for test.ua - is used another-test-cdn.ua and so on. But I want change this publicPath in vue.config in runtime (maybe base on current domain or something like that). So I can make only one build (because it can take too much time - to make several builds).

Can you suggest any ideas to solve this problem? Thanks!

like image 419
Forget-me-not Avatar asked Oct 26 '25 10:10

Forget-me-not


1 Answers

You can assign a new value to __webpack_public_path__, but you have to do so before the app itself starts.

So best would be to put this into its own file and import it before Vue itself:

import './publicpath'
import Vue from 'vue'

then in publicpath.jsyou would do something like this:

__webpack_public_path__ = window.your_public_path

You could of course also use window.location to get the domain or other things

like image 163
Linus Borg Avatar answered Oct 29 '25 00:10

Linus Borg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!