0

I'm trying to load a React app onto an asp.net view (project was initially written in asp.Net, am creating new pages using React for learning purposes and for fun).

When the React app is running in dev move, all images load without any issue with all pathing working.

After building the app using npm run build, the js file and corresponding image files are generated and I place them on a folder on my asp.net application (e.g. Scripts folder).

When I try to view the page, the React app loads and the screen is rendered, but the images are all broken as the website can't find them.

After looking to see why the images aren't loading, they're getting a 404 not found error due to the images trying to be loaded from the current URL, rather than where the js file and images are stored.

For example, the view which loads the React app is on https://localhost/Home/ReactPage

And ReactPage.cshtml has the following in it:

<div id="thisIsTheReactDiv"></div>

<script src="/Scripts/ThisFolder/app.js" type="text/javascript"></script>

The React app js is in the Script folder in the project, so to access it, the src has been put as shown above. To access the image you'd have to do something like https://localhost/Scripts/ThisFolder/image.png

At this stage what it's doing is the image is being linked to https://localhost/Home/ReactPage/image.png which causes a 404 error.

Are there any webpack configs that can be done to make it point to a specific path before it gets built?

Is there any way to make the React app when building for PROD update the image relative paths so it looks at a specific folder rather than trying to get it from the current path?

1 Answers1

0

Alright, after a few hours of searching (which lead me to post this question), I've figured out how Webpack works with relative pathing now.

Looks like there's a section called "publicPath" in the output setting where you can type in to get the path relative to where your React JS file is deployed at.

So it looks like typing in

 publicPath: "/Scripts/ThisFolder/",

will make the relative path to start from there so all images will load from that URL.

As seen from this Stack Overflow link: What does "publicPath" in Webpack do?