Open ivictbor opened 3 years ago
+1
Also, it could be useful for webpack5 output: { publicPath: 'auto' }
feature
+1
This is a must for any kind of embedding where a widget is not served from the same domain for instance. It messes with the routing otherwise.
背景: vueCli 3.30后,废弃baseURL ,统一使用publicPath,同时禁用了output.publicPath 问题:项目打包的静态资源JS+CSS,想配置CDN域名,和项目域名不同域名,怎么搞嘛。 之前是只配置publicPath即可。
html 我们是通过Nginx代理到的CDN域名 JS+CSS 直接配置CDN域名,没有代理的原因是想节省链路,直接享受CDN加速
"@vue/cli-plugin-babel": "~4.5.0", "@vue/cli-plugin-eslint": "~4.5.0", "@vue/cli-plugin-router": "~4.5.0", "@vue/cli-plugin-typescript": "~4.5.0", "@vue/cli-plugin-unit-mocha": "~4.5.0", "@vue/cli-plugin-vuex": "~4.5.0", "@vue/cli-service": "~4.5.0",
@sodatea 豪群哥,方便帮忙指点一下吗~
I think I found a fix for this.
First, use vue cli's publicPath
instead of webpack:
vue.config.js
module.exports = defineConfig({
publicPath: WEB_CDN_DOMAIN || '/',
...
Then, change your router history from createWebHistory(process.env.BASE_URL)
to createWebHistory('/')
Index.js
import { createRouter, createWebHistory } from 'vue-router';
const router = createRouter({
history: createWebHistory('/'),
routes: [...],
});
export default router;
I think I found a fix for this.
First, use vue cli's
publicPath
instead of webpack:vue.config.js
module.exports = defineConfig({ publicPath: WEB_CDN_DOMAIN || '/', ...
Then, change your router history from
createWebHistory(process.env.BASE_URL)
tocreateWebHistory('/')
Index.js
import { createRouter, createWebHistory } from 'vue-router'; const router = createRouter({ history: createWebHistory('/'), routes: [...], }); export default router;
This fix works for me, but somehow in my project the router var should be like this (changed from base: process.env.BASE_URL,
)
const router = new VueRouter({
mode: 'history',
base: '/',
routes
})
if you just want set webpack.output.publicPath
in vue-cli, you can use this plugin vuecli-publicpath-webpack-plugin
Version
@vue/cli-service 3.5.0
Environment info
Steps to reproduce
Define output.publicPath in vue.config.js
What is expected?
I need a way to pass output.publicPath without this exception message. My usecase:
I serve one same Vue SPA on hundreds and thouthands of subdomains:
https://sub1.mydomain.com/ https://sub2.mydomain.com/ https://sub3.mydomain.com/ https://sub4.mydomain.com/
etc. Like workpsaces in Slack.
Every time when new user joins service and creates new workpace on new subdomain, it does not hit a cache on CDN because URL is always different https://sub123.mydomain.com/js/app.js, https://sub124.mydomain.com/js/app.js.
So CDN bypasses the request to slower upstream and takes redundant place in caches store by storing same files on different urls.
To solve this, only one simple thing which I need to do is:
(+add CORS settings on serving end) so now the cache on CDN will not be separated and all new users will hit one cache when they load JS/CSS/fonts.
If I use native top-level publicPath option, some Vue runtime code creates a redirect and breaks a route by adding domain there, but I don't want redirect, I really want index.html to be served from https://sub2.mydomain.com/, I only need to change URLs of assets, not route of the application.
I had to destroy validateWebpackConfig.js in my CI build pipeline to solve this issue quickly, and after this, applications works perfectly:
But it would be better if we can disable this "smart mode" and allow some flexibility, at least if we would have an option like allowOutputPublicPath :
Is there any better/faster ways to point all statics to be loaded from a different domain? This definitely not super popular case, but from time to time developers might need it.
What is actually happening?
Configuration Error: Avoid modifying webpack output.publicPath directly. Use the "publicPath" option instead