Skip to content

Devserver has problems serving static assets with assetsPublicPath != '/' #1176

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jbayona89 opened this issue Dec 15, 2017 · 8 comments
Closed

Comments

@jbayona89
Copy link

jbayona89 commented Dec 15, 2017

Hello I have made a Vuetify app using vue-cli and vue init vuetifyjs/webpack-advanced the resulting code is this https://github.com/vuetifyjs/webpack-advanced/tree/master/template

The problem I have is that my production app isn't in the root of the server and it is a subpath in www.example.com/foo. When I run npm run build all my static assets like images and fonts are referenced like /static/images/example.png, so the server doesn't find them as they are being looked in www.example.com/static/images/example.png instead of www.example.com/foo/static/images/example.png

If I change config/index.js and set assetsPublicPath: '/foo' and change all my references from /static/... to /foo/static/... then my production server finds all my static assets but then my dev server using npm run dev can't find them as they are being referenced as localhost:8080/foo/static/images/example.png but they seem to be at localhost:8080/static/images/example.png for some reason.

My ideal solution would be just to keep the references as /static and tell webpack or vueloader somehow that on npm run build it should prepend /foo to all of my assets urls. If that is impossible at least I would like to know a way to make my dev and production environments to work with the same code even though it implies to prepend manually /foo to my static assets urls

Thank you

@LinusBorg
Copy link
Contributor

LinusBorg commented Dec 15, 2017

First of all, why are you treating them as static assets instead of letting Webpack handle them by putting them into ./assets? that would prevent the whole problem.

If that's not possible, we will have to work something out, becaue it seems that webpack-dev-server doesn't apply the publicPath setting to static files (but he does to built files) - that's a pity.

I will investigate a bit more if that's really the case and what to do about that, but for now we will have to work around that.

We can do that by generating the static paths dynamically depending on environment:

// /config/dev.env.js
STATIC_PATH_PREFIX: '"/"' 

// /config/prod.env.js
STATIC_PATH_PREFIX: '"/foo/"' 

Then add a helper to your app:

Vue.mixin({
  methods: {
    $toStaticPath(path = '') {
      return process.env.STATIC_PATH_PREFIX + path
    }
  }
})

and use it like this:

<img v-bind:src="$toStaticPath('static/images/example.png')">

@jbayona89
Copy link
Author

Thank you, I will try to use assets whenever it's possible and Will use this for the rest of my urls

@LinusBorg LinusBorg changed the title Prepend subpath in production build DEvelopment build has problems serving static assets with assetsPublicPath != '/' Dec 15, 2017
@LinusBorg LinusBorg changed the title DEvelopment build has problems serving static assets with assetsPublicPath != '/' Devserver has problems serving static assets with assetsPublicPath != '/' Dec 15, 2017
@nkovacs
Copy link

nkovacs commented Dec 16, 2017

Like I said in #1141, adding CopyWebpackPlugin to the dev config (with the exact same configuration except with config.dev.assetsSubDirectory) fixed my issue. You could try that.

@LinusBorg
Copy link
Contributor

I see how that could provide a workaround, but it would have to copy to a different location, e.g. /public, so that it doesn't mess with /dist - that would have to be docoumented and explained to users.

So thanks, I will fall back to this if I don't find a way to make webpack-dev-server serve the files appropriately.

@LinusBorg
Copy link
Contributor

Oh, I just saw this: https://github.com/webpack-contrib/copy-webpack-plugin#this-doesnt-copy-my-files-with-webpack-dev-server

Starting in version 3.0.0, we stopped using fs to copy files to the filesystem and started depending on webpack's in-memory filesystem:

That could work after all. Thanks!

@alxppp
Copy link

alxppp commented Dec 28, 2017

Thanks @LinusBorg! That has been bugging me for a while now. The latest development branch is working fine for me!

Two small hints for others updating from the old dev server:

  • Instead of assetsPublicPath: '/abc/' it now has to be assetsPublicPath: '/abc'
  • To get browser reloads working again, I had to change history api fallback to:
historyApiFallback: {
  rewrites: [
    { from: /.*/, to: `${config.dev.assetsPublicPath}/index.html` },
  ],
},

@LinusBorg
Copy link
Contributor

Hey, thanks for the feedback, that's very valuable.

I'll add those fixes when I can.

LinusBorg added a commit that referenced this issue Dec 31, 2017
After the switch to CopyWebpackPlugin (#1176), historyAPIFallback had to be adjusted to still serve index.html from all catched paths correctly.
See: #1176 (comment)
@SirM2z
Copy link
Contributor

SirM2z commented May 17, 2018

Hello @LinusBorg
My browser generated extra duplicate requests when I added this configuration

historyApiFallback: {
  rewrites: [
    { from: /.*/, to: `${config.dev.assetsPublicPath}/index.html` },
  ],
},

before modifying:
image

after modification:
image

I found that extra requests are related to favicon.ico.

But it can work normally when I change back historyApiFallback: true

pksunkara pushed a commit to sunkarapk/spoiler that referenced this issue Jun 24, 2018
After the switch to CopyWebpackPlugin (#1176), historyAPIFallback had to be adjusted to still serve index.html from all catched paths correctly.
See: vuejs-templates/webpack#1176 (comment)
oozliuoo referenced this issue in gzkiwiinc/webpack Jul 31, 2018
* develop:
  bump template version
  fix bug with vuejs-templates#1202
  Make CSS sourcemaps on by default
  Ensure page reloads in history mode serve index.html
  use old sourcemap option
  Add postcss-url to match postcss-import (vuejs-templates#1115)
  Change engine>node{4 => 6} for template (vuejs-templates#1206)
  Fix static file serving for publicPath != `/` (fix#1176) (vuejs-templates#1180)
  Load, render template version using helper (vuejs-templates#1202)
  fix typo in a comment (vuejs-templates#1183)
  fix vuejs-templates#1140
wdev733 added a commit to wdev733/vue-dashboard that referenced this issue Aug 6, 2019
After the switch to CopyWebpackPlugin (#1176), historyAPIFallback had to be adjusted to still serve index.html from all catched paths correctly.
See: vuejs-templates/webpack#1176 (comment)
fullstack412 added a commit to fullstack412/Vue-dashboard that referenced this issue Aug 16, 2019
After the switch to CopyWebpackPlugin (#1176), historyAPIFallback had to be adjusted to still serve index.html from all catched paths correctly.
See: vuejs-templates/webpack#1176 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants