vue-cli icon indicating copy to clipboard operation
vue-cli copied to clipboard

Asset CDN with PWA plugin

Open ryancalhoun opened this issue 6 years ago • 7 comments

What problem does this feature solve?

I want to host my app's assets on a CDN with a different origin by setting publicPath to an absolute URL.The reason is that my app's entry-point is Google App Engine, to serve the index. But the digested assets need to be in Cloud Storage so old versions remain available.

I also want to use the PWA plugin to link a manifest.json, but the manifest.json and service worker are required to be on the same origin as the index.

Currently I'm using sed to remove the CDN URL from the index after the app is built.

What does the proposed API look like?

Maybe a config option under pwa, like pwa.publicPath. It could behave exactly like publicPath, but only apply to the manifest.json and service-worker.js. Because of the digesting issue, it would probably make sense to keep the precache-manifests at the app's publicPath so those files could live on the CDN too.

ryancalhoun avatar Mar 04 '19 19:03 ryancalhoun

Hi @ryancalhoun I am struggling with this too

For clarity, this is what I am currently doing. Hopefully this assists if this feature goes to development.

Setting publicPath as where will be, in public

THEN after each build

  • in index.html - change all the URL for all assets FROM publicPath, to CDN path.... except for manifest.json that has to be the public path
  • in service-worker.js - change the path from publicPath to CDN path
  • in precache-manifest - change the path from publicPath to CDN path (for all assets)

i.e. the requirement is some kind of configuration for CDN asset path, thus avoiding all this post build processing.

Thanks!

alexbainbridge avatar Jun 09 '19 11:06 alexbainbridge

I think I'm running into the same issue when trying to deploy to (the CDN of) darklang.com.

When I configure publicPath: DARK_STATIC_ASSETS_BASE_URL the resulting index.html has links referencing /DARK_STATIC_ASSETS_BASE_URL instead of just DARK_STATIC_ASSETS_BASE_URL.

But I'm not building a PWA.

Sch3lp avatar Jan 19 '20 17:01 Sch3lp

Have either of you figured this out? I'm also using the pwa plugin and want to experiment with loading all css/js assets via bunnycdn. I'm a bit surprised there is almost no one else trying to do this

vesper8 avatar Mar 08 '20 13:03 vesper8

Just like Ryan, I ended up using sed to string-replace the resulting index.html in a post-build script:

sed -i .bak 's/\/DARK_STATIC_ASSETS_BASE_URL/DARK_STATIC_ASSETS_BASE_URL/g' dist/index.html

app-root/.env.dark:
PUBLIC_PATH=DARK_STATIC_ASSETS_BASE_URL

app-root/package.json:
"scripts": { "dark": "vue-cli-service build --mode dark", ... }

(probably not necessary, but this is what I currently have in my codebase)
app-root/vue.config.js:
publicPath: process.env.PUBLIC_PATH || '/',

Sch3lp avatar Mar 08 '20 22:03 Sch3lp

hrm.. thanks for sharing your solution. It feels like there should be a better way to achieve this.. we can't be the only ones wanting to use a CDN to serve our dist folder's css/js/images

have you encountered any problems or anomalies serving your vue-cli pwa via a cdn?

vesper8 avatar Mar 09 '20 02:03 vesper8

Just like Ryan, I ended up using sed to string-replace the resulting index.html in a post-build script:

sed -i .bak 's/\/DARK_STATIC_ASSETS_BASE_URL/DARK_STATIC_ASSETS_BASE_URL/g' dist/index.html

app-root/.env.dark: PUBLIC_PATH=DARK_STATIC_ASSETS_BASE_URL

app-root/package.json: "scripts": { "dark": "vue-cli-service build --mode dark", ... }

(probably not necessary, but this is what I currently have in my codebase) app-root/vue.config.js: publicPath: process.env.PUBLIC_PATH || '/',

Use this way will change manifest.json url to cdn which is not good

xicond avatar Jun 14 '22 04:06 xicond

Any improvement?

DevinXian avatar Apr 23 '24 09:04 DevinXian