Skip to content

Css font path using relative build path #1284

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

Open
fatlinesofcode opened this issue Feb 1, 2018 · 13 comments
Open

Css font path using relative build path #1284

fatlinesofcode opened this issue Feb 1, 2018 · 13 comments
Labels

Comments

@fatlinesofcode
Copy link

fatlinesofcode commented Feb 1, 2018

Ive changed assetsPublicPath to a relative path.

    assetsPublicPath: './',

All works fine except links to font files from css are broken. I've added fonts to my project under
/src/assets/fonts/myfont.woff

I've added them to app.vue like so

@font-face {
    font-family: 'HelveticaNeue';
    src: url('assets/fonts/HelveticaNeue.woff') format('woff');
    font-weight: 400;
    font-style: normal;
  }

All works fine in dev. In production the font files are emitted to dist/static/fonts/ however within the css file the path is

@font-face {
    font-family: HelveticaNeue;
    src: url(static/fonts/HelveticaNeue.6f58d34.woff) format("woff");
    font-weight: 400;
    font-style: normal
}

Which causes the following path error in in the browser
Request URL:http://localhost/dist/static/css/static/fonts/HelveticaNeue.6f58d34.woff
Request Method:GET
Status Code:404 Not Found

The path in the css should be url(../fonts/

Is there a way to resolve this?
Something to do with postcss-url perhaps?

@fatlinesofcode
Copy link
Author

I'm using // Template version: 1.3.1

@LinusBorg
Copy link
Contributor

LinusBorg commented Feb 5, 2018

Please try this: #1266 (comment)

I wilkl look into this further at a later time. I'm very short on free time in the coming days, so it may be a while.

@LinusBorg LinusBorg added the bug label Feb 5, 2018
@fatlinesofcode
Copy link
Author

Hi @LinusBorg thanks your help that does fix the path within the Css file, however now the files are emitted to the wrong folder. They are outputted to one level below /dist/ :(

@LinusBorg
Copy link
Contributor

Damnit. This seems to be really tricky. Relative paths are of he devil :D

@fatlinesofcode
Copy link
Author

yep, its a pain. Pretty sure postcss-url can solve this issue. I see you've added it to the project, maybe it just needs some additional config?

@Macavirus
Copy link

Having the same issue here.

The only thing I've changed from the default config is changing the assetsPublicPath in index.js. I have to do this because my files are not on a webserver but used on an embedded Chrome instance.

    assetsPublicPath: './',

I include the fonts in the style tag of App.vue like this:

@font-face {
  font-family: "Noto Sans SemCond";
  src: local("Noto Sans SemiCondensed"), local("NotoSans-SemiCondensed"),
    url("assets/fonts/subset-NotoSans-SemiCondensed.woff2") format("woff2");
  font-weight: normal;
  font-style: normal;
}

The suggested fix by @LinusBorg from here does not work as well. Is there any way to include fonts in the production build? This seems like a pretty common use case.

@johnellinwood
Copy link

I had the exact same issue using the material-design-icons in this template. My dev configuration worked, but I had set prod to use relative paths. A valid workaround is described in issue 200 which has been open since July, 2016. The code for your webpack base font rule should be:

{
  test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
  loader: 'url-loader',
  options: {
    limit: 10000,
    name: utils.assetsPath('fonts/[name].[hash:7].[ext]'),
    // workaround for vuejs-templates webpack issue 1266
    publicPath: process.env.NODE_ENV === 'production' ? '../../' : '/'
  }
}

Incidentally, all I had to do the get material-design-icons working was npm i material-design-icons, add import 'material-design-icons/iconfont/material-icons.css' to the main.js file, and then use the above workaround.

@stephanedemotte
Copy link

Same here, need relative path for electron.js, some news about that ?

@ajithkgshk
Copy link

I had the same issue. My development config worked, but my production build did not. Kept getting the wrong path for fonts. In my case, the dir structure was as below

src
    assets
        fonts
.
.
www
    dist
        static
            fonts

I added the following line to the webpack base config file where it checks for font files.
publicPath: process.env.NODE_ENV === 'production' ? '../../' : '/'

@ThePlastic
Copy link

ThePlastic commented Jun 8, 2018

solved same issue with it, thanks @johnellinwood

@MutableLoss
Copy link

I'm just posting to mention this seems to be an actual issue with the webpack url-loader. While I do not use vuejs-templates (but certainly will in the future), I found this exact issue with one of my older React/Electron projects, then finding myself trying to figure out why my production builds were trying to access fonts using an exact development path.

At the time of this writing I'm using Webpack 4.10.0, and url-loader 0.6.2. While I haven't tried 1.1.1 just yet, it's probably safe to say it doesn't change anything considering the last post was made 27 days ago in the referenced issue.

Anyways, I thought I would mention this before anyone spent too much time trying to find the source of this issue in this particular project.

@hugomejia
Copy link

hugomejia commented Feb 6, 2019

I had the exact same issue, so, Just in case somebody is building a Cordova/Phonegap App with Vuej + Vuetify , I confirm the same work around worked for me. Thanks @johnellinwood !!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

10 participants