Skip to content
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

Cannot load local images in CSS #2365

Closed
Noah-Silvera opened this issue Mar 6, 2020 · 3 comments
Closed

Cannot load local images in CSS #2365

Noah-Silvera opened this issue Mar 6, 2020 · 3 comments
Labels
bug An error in the Docusaurus core causing instability or issues with its execution status: needs triage This issue has not been triaged by maintainers

Comments

@Noah-Silvera
Copy link
Contributor

Noah-Silvera commented Mar 6, 2020

🐛 Bug Report

If you use the React/Webpack import css style where css files are imported into js files like import styles from "./index.module.css"; then background images loaded by URL's fail for local images such as png's, svgs, and jpgs.

Have you read the Contributing Guidelines on issues?

yes

To Reproduce

  1. Load a css file via an import into a JS file (the styles.module.css file for the index.js landing page works nicely)
  2. Use a background-image: url('<some-local-file>.png|jpg|svg') type tag on an imported class
  3. Try to load docusaurus

Expected behavior

Docusaurus loads the png image and other common types (jpg, svg, etc) via the css

Actual Behavior

Docusaurus fails to load the background image with an error like so.

2020-03-06 11_24_57-DevTools - localhost_3001_

Your Environment

I've made no modifications to the webpack config that ships with docusaurus-init

  • Docusaurus version used: 2.0.0-alpha.43
  • Chrome 80.0.3987.132, Node.js 10.16.3):
  • Desktop

Reproducible Demo

I've modified src/pages/index.js and src/pages/styles.module.css to load a background-image URL tag with a local png (static/img/docusaurus.png). Run yarn start to see the error.

https://github.com/Noah-Silvera/docusaurus-png-load-bug

@Noah-Silvera Noah-Silvera added bug An error in the Docusaurus core causing instability or issues with its execution status: needs triage This issue has not been triaged by maintainers labels Mar 6, 2020
@Noah-Silvera Noah-Silvera changed the title Cannot load PNG images in CSS Cannot load local PNG images in CSS Mar 6, 2020
@Noah-Silvera Noah-Silvera changed the title Cannot load local PNG images in CSS Cannot load local images in CSS Mar 6, 2020
@Noah-Silvera
Copy link
Contributor Author

I think this just needs an update to the webpack loader to support some basic image types.

@lex111
Copy link
Contributor

lex111 commented Mar 6, 2020

background-image: url('../../static/img/docusaurus.png')

Use absolute path like this background-image: url('/img/docusaurus.png') (same as static/img/docusaurus.png)

@lex111 lex111 closed this as completed Mar 6, 2020
@apurvaojas
Copy link
Contributor

apurvaojas commented Dec 29, 2020

background-image: url('../../static/img/docusaurus.png')

Use absolute path like this background-image: url('/img/docusaurus.png') (same as static/img/docusaurus.png)

@lex111
The absolute path will not work if we have a different baseURL in the production environment.
In that case, we need to use a relative path, which is breaking for SVG images.

image

  • Docusaurus version used: 2.0.0-alpha.70

Reported an SVG specific issue #3964

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug An error in the Docusaurus core causing instability or issues with its execution status: needs triage This issue has not been triaged by maintainers
Projects
None yet
Development

No branches or pull requests

3 participants