question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CSS full path for images

See original GitHub issue

Describe the bug

When vite is required (included from another endpoint) the reference to images in the CSS are lost cause the CSS is injected in the page.

div {
    background: url(/image/demo.png)
}

When transpiled I think it would be nice to inject the full path to avoid any problem

div {
    background: url(http://localhost:3000/image/demo.png)
}

Or maybe offer a configuration to set the “publicPath”

Reproduction

Start any vite template including CSS. Then load this from another endpoint. For instance I’m on http://localhost:8000 and I include

<script src="http://localhost:3000/app.js" type="module" defer></script>

System Info

  • vite version: 2.0.0-beta.27
  • Operating System: all
  • Node version: all
  • Package manager (npm/yarn/pnpm) and version: all

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
at86commented, Jun 5, 2021

in vite.config.js use config of

  css: {
    preprocessorOptions: {
      less: {
        rootpath: "http://localhost:3000/src",
      },
    },
  },
1reaction
nobleachcommented, Apr 29, 2021

I’ve noticed the opposite problem. I have imported a component/style framework that uses an icon font via a rule like this:

@font-face {
  font-family: 'rsuite-icon-font';
  src: url('https://cdn.jsdelivr.net/npm/rsuite/dist/styles/fonts/rsuite-icon-font.ttf') format('truetype'), url('https://cdn.jsdelivr.net/npm/rsuite/dist/styles/fonts/rsuite-icon-font.woff') format('woff');
  font-weight: normal;
  font-style: normal;
}

When compiled those absolute urls become:

http://localhost:8080/node_modules/rsuite/lib/styles/https://cdn.jsdelivr.net/npm/rsuite/dist/styles/fonts/rsuite-icon-font.woff

It seems the code is blindly just prepending whatever is in those url() declarations. I see no test for this here: https://github.com/vitejs/vite/blob/main/packages/playground/assets/css/css-url.css So it appears to be an overlooked scenario.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to give the background-image path in CSS?
How to give the background-image path in CSS? · change it in Firebug/Chrome and see which one shows image. – Murali Murugesan ·...
Read more >
HTML File Paths - W3Schools
File paths are used when linking to external files, like: Web pages; Images; Style sheets; JavaScripts. Absolute File Paths. An absolute file path...
Read more >
CSS images: url() · WebPlatform Docs
Usage is simple — you insert the path to the image you want to include in your page inside the brackets of url()...
Read more >
url() - CSS: Cascading Style Sheets - MDN Web Docs
The url() CSS function is used to include a file. The parameter is an absolute URL, a relative URL, a blob URL, or...
Read more >
Absolute Path for the CSS Background-Image property?!
User-1559987826 posted. How can I set up an absolute path for this code snippet: background-image: url('Images/MenuBackground.png');.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found