in Education by
I am using BrowserRouter as Router from react-router-dom. I want to host my application at a path that is not /. I used the basename attribute on the router tag, and it works great. I am referring to images in my react components like this: image The HTML reference works great. But in my CSS files, this breaks: background-image: url(one.svg); I tried this but it breaks for prod builds (dev env works) : background-image: url(/one.svg); This works, but only for production builds (react-scripts build). It breaks during dev (react-scripts start command). background-image: url(/my-cool-path/one.svg); I want to be able to have CSS URL paths that work when I do BOTH a production build and during dev (hot reloading with react-scripts start command). Any ideas for a solution? Is this possible? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Short answer: not the way you're hoping. Problem is, React is Javascript, and your CSS is not handled by Javascript. You could be saved, if you're compiling CSS - from SCSS for example - you could create a file ie. env-vars.scss, and have it define a variable like $basePath, setting it to /my-cool-path/. ( like $basePath: "/my-cool-path/";) Import that file into your main scss file, and have git (or whatever repo you use) ignore the file. Then you can manually recreate that file on the server, setting the $basePath to / ( $basePath: "/"; ). You can then use the $basePath in your urls in scss, say for example background-image: url($basePath + "img/some-background.png"); When ever the scss is compiled, it'll pull the var from that file, and if your repo is ignoring it, you can create different values depending on the environment you're running the compile on. However building with react-scripts start and react-scripts build might also provide you a way to set an ENV variable and import it into your scss, but I'm not entirely familiar with this so I'd have to direct you to google for that one I'm afraid. If you're not compiling your CSS from a preprocessor, it'll be a tougher task.

Related questions

0 votes
    i'm messing with React and i was trying to make some transition between the router pages. Before using ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    I am trying to use Storybook v5.0 with my project and I am using React + Webpack4 + CSS Modules. ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    Most computers have BIOS which can be configured so that it can ask for a password once the system starts. ... -for-Cyber Security:,Cyber Security-Jobs:,Cyber Security Applications...
asked Nov 1, 2021 in Education by JackTerrance
0 votes
    Redis can be configured to meet different requirements by editing the configuration settings in __....
asked Apr 12, 2021 in Technology by JackTerrance
0 votes
    Non-VNet VMs cannot be configured with _________. A. Multi NIC B. Dynamic Ips C. Static IPs D. MAC...
asked Nov 20, 2022 in Education by JackTerrance
0 votes
    Does anyone know how to add another folder to the asset pipeline in Rails 3.1? I'd like to serve app/assets/ ... an asset path, in your application.rb file: config.assets.paths...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
    Under which section of IT Act, stealing any digital asset or information is written a cyber-crime. (a) 65 (b) ... Security questions and answers pdf, mcq on Cyber Security pdf,...
asked Nov 4, 2021 in Education by JackTerrance
0 votes
    Which of the following is an intangible asset? (1)Hardware components (2)Business Premises (3)Inventory and Machinery (4)Trade Secrets...
asked Mar 22, 2021 in Technology by JackTerrance
0 votes
    We have redesigned the structure to a website which has several business units. Now I want to redirect (301 ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 20, 2022 in Education by JackTerrance
0 votes
    network that contains multiple hubs is most likely configured in a …………… topology. A) Bus B) Mesh C) Star D) Tree Select the correct answer from above options...
asked Dec 25, 2021 in Education by JackTerrance
0 votes
    Devices are configured ______ . when device is used at the startup of windows when out of order Select the correct answer from above options...
asked Dec 17, 2021 in Education by JackTerrance
0 votes
    Devices are configured ______ . when device is used at the startup of windows when out of order Select the correct answer from above options...
asked Dec 17, 2021 in Education by JackTerrance
0 votes
    ____________ is an expansion point which, after being attached and configured by a DFM in the user interface, will light ... 2)Funnel (3)None of the options (4)Controller Service...
asked Apr 20, 2021 in Technology by JackTerrance
0 votes
    ____________ is an expansion point which, after being attached and configured by a DFM in the user interface, will light ... 2)Funnel (3)None of the options (4)Controller Service...
asked Apr 19, 2021 in Technology by JackTerrance
...