create-react-app icon indicating copy to clipboard operation
create-react-app copied to clipboard

CRA 4 fails to compile when css background-image ulr('url') not resolved

Open a-tonchev opened this issue 5 years ago • 19 comments

I have a css file that is imported in my react app, it is something like:

styles.css

.flag { ... background-image: url("/img/flags.png") !important; }

It is imported in my react file -> import './styles.css';

In the previous version of CRA it works well, and when I build the app it was also okay, since I have the /img/flags.png in my 'public' directory.

After CRA 4 it fails to compile

Best regards

a-tonchev avatar Nov 05 '20 13:11 a-tonchev

This guide still works: https://create-react-app.dev/docs/adding-images-fonts-and-files

You should not link to the public folder in your src or css. Create a static directory in your src folder and do like:

background-image: url("./static/img/flags.png");

pascalstr avatar Nov 05 '20 14:11 pascalstr

@pascalstr, Your solution works, but I have to move all the assets that do not need to be in the src folder, such as images, videos, fonts, and so on. Also, all of these will beside together in the media folder that CRA created by default and is not customized for production.

saeed-rohani avatar Nov 05 '20 15:11 saeed-rohani

@pascalstr Thanks that works for me!

a-tonchev avatar Nov 05 '20 15:11 a-tonchev

@Saeed-Rohani #9937 seems to be a similar issue, maybe close this in favour of #9973

pascalstr avatar Nov 05 '20 15:11 pascalstr

This guide still works: https://create-react-app.dev/docs/adding-images-fonts-and-files

You should not link to the public folder in your src or css. Create a static directory in your src folder and do like:

background-image: url("./static/img/flags.png");

@Saeed-Rohani The guide makes sense but how would you reference an image in an inline style to do something like this:

style={{backgroundImage:"url(/img/example.png)"}}

in this case static will not work and the use case being...

style={{backgroundImage:"url(/img/example-"+var+".png)"}}

Cheers

makis-spy avatar Nov 14 '20 08:11 makis-spy

I'm surprised this isn't a show stopper for more people. Any light on a work around or approach I'm not getting would be helpful.

Cheers!

makis-spy avatar Nov 16 '20 15:11 makis-spy

This issue has been automatically marked as stale because it has not had any recent activity. It will be closed in 5 days if no further activity occurs.

stale[bot] avatar Dec 19 '20 08:12 stale[bot]

any progress on this? i just spent too many hours figuring why my app won't suddenly compile after upgrading react scripts

bazo avatar Dec 23 '20 16:12 bazo

I'm surprised that more people haven't realised that this is a major concern for SEO.

Lets say that you have a image file within the src folder, and you're using it within your CSS file like this -

.Profile { text-align: center; background-image: url("../../images/user/profile.png"); }

If this profile.png file is inside the src > images > user directory, then while creating a build with react-scripts, the URL generated for this file will be something like this -

https://{my-domain.com}/static/media/profile.{hash}.jpg

This {hash} value changes with every build. Therefore, if you use this in a static website, and if your website is crawled by google bot, you may see 404's reported in the google search console the next time you deploy, because Google can take several weeks before indexing your site again.

This is where an image in the public folder would have helped, because that URL will never change, and can safely remain indexed in search engines.

sagar7993 avatar Jan 13 '21 03:01 sagar7993

Same error over here. In CRA 3.4 it just was worked fine. :(

walterdl avatar Jan 13 '21 13:01 walterdl

Hi, I am new in issue discussion what the result to fix? my case is using react as one of cms page content, all the assets relative to domain with year/month folder organiser, can not put it on static folder any clue?

klepon avatar Feb 08 '21 17:02 klepon

Hey for quick fix you can use craco (https://github.com/gsoft-inc/craco/tree/master/packages/craco)

install per instructions. In my case im using .cracorc file.

Put this inside .cracorc: module.exports = { reactScriptsVersion: "react-scripts", style: { css: { loaderOptions: () => { return { url: false } } } } }

Save, when launched with changed package.json (see craco install instructions) with CRA 4.0 .scss properly resolves from public just like before

maciejgaciarz avatar Jun 17 '21 12:06 maciejgaciarz

Is there no better fix than using yet another package with yet another configuration file?

Moving my assets into src doesn't work (and I don't like the idea anyway), whether I use url("/aze"), url("./aze") or url("aze"). I'm going to try downgrading now but this sucks.

Cochonours avatar Jul 14 '21 09:07 Cochonours

What changes are planned in this regard?

artemlatark avatar Aug 14 '21 16:08 artemlatark

I ran into this issue too. I was previously using something like: background: #000 url("/images/bg/bg.jpg") repeat;

Inside of /src/App/app.css. I ended up moving all the static/global CSS files I use to /public/css/* instead and use them in /public/index.html like so:

<link rel="stylesheet" type="text/css" href="%PUBLIC_URL%/css/slate.css">  
<link rel="stylesheet" type="text/css" href="%PUBLIC_URL%/css/SlateOverrides.css">

Now all my static CSS files sit inside the public folder and in my actual App I use JS imports / styled components to style everything else. This seems to be the intended best practice.

mobeigi avatar Sep 25 '21 03:09 mobeigi

This issue has been automatically marked as stale because it has not had any recent activity. It will be closed in 5 days if no further activity occurs.

stale[bot] avatar Jan 09 '22 13:01 stale[bot]

This is still an issue. with react-scripts v3, the same import have worked for me, but now I'm getting this error: Uncaught Error: Cannot find module '../../../../assets/images/game/enchants/types/major.png'

(The file is in public/assets/images/game...)

image

Morta1 avatar Oct 13 '22 08:10 Morta1

Closing in on almost 36 months since this issue was opened. I would be interested in creating a pull request to resolve this but am not sure where to start, any recommendations on how I could begin to get this fixed?

djwadhwa avatar Oct 14 '23 08:10 djwadhwa