Unable to instantiate Vue web app on http-server - Issue: css and js files are returning 404 Not Found error

My goal is to dockerize my vue app, but I am encountering issues when running it in a docker container as nothing loads in the browser.

To troubleshoot this issue, I decided to test it out locally by running npm run serve, which worked fine and produced the expected result:

https://i.sstatic.net/OWJwD.png

Next, I ran npm run build, resulting in:

https://i.sstatic.net/EwbOG.png

I suspect that the issue might be related to having a large number of jpeg images in the assets directory, specifically a posters folder with over 50,000 images that are dynamically displayed in the app using Vue:

<div v-for="movie in this.recommendations" :key="movie" class="movie-card col-md-2">

 <img v-if="movie['poster']=='True'" :src="getImgUrl(movie['movieId'])" v-bind:alt="pic">

And here's the getImgUrl function:

getImgUrl(imgName) {
            var images = require.context('../assets/posters', false, /\.jpg$/)
            return images('./' + imgName + ".jpg")
    }

While using vue/cli, I received webpack performance recommendations suggesting ways to optimize bundle size. However, I'm unsure how to proceed with implementing lazy loading or if hosting the images on a public Google Drive would resolve the issue.

As someone new to vue, any guidance or assistance would be greatly appreciated!

Answer №1

Utilizing the assets folder and employing require enables you to bundle all images into your code by converting them to base64. This results in massive chunks being created when compiled, as the images are incorporated directly into the source code.

A more efficient approach would be to relocate your images from the assets folder to the public directory. Subsequently, load the images via HTTP. This separation ensures that your code and images remain distinct. Upon page loading, the browser individually requests and loads the images instead of embedding them within the code.

For instance:

<img v-if="movie['poster']=='True'" :src="getImgUrl(movie['movieId'])" v-bind:alt="pic">
getImgUrl(imgName) {
     return `/posters/${imgName}.jpg`
}

As a result, your directory structure would appear as follows:

-public
-|--posters
-|--|--Poster1.jpg
-|--|--Poster2.jpg
-|--|--Poster3.jpg
-|--|--Poster4.jpg
etc

The public directory essentially functions as a web server, allowing direct access to its contents. If, for instance, you were to move your images to the public directory with the provided structure and access localhost:8080/posters/Poster1.jpg, the image would load independently without necessitating a Vue router.

To gain a comprehensive understanding of the public folder and its purpose, refer to the documentation.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble getting VueJS app to load JS files on a Kubernetes cluster

My VueJS app is running from a Docker image in Kubernetes, but as soon as there are multiple replicas/pods, the client encounters issues loading the app - some requests for files result in a 404 error. I believe this is because the requests are being dire ...

Issue encountered when executing the "npm run watch" command in Laravel version 8

I executed the command "npm run watch" in a new Laravel 8 project with only the Auth package installed, and encountered the following output: Here's The component:- <template> <div class="col-md-8 mt-4"> <di ...

Utilizing GeoLocation in JavaScript: Implementing a Wait for $.ajax Response

Whenever I make an AJAX POST request to my backend server, I aim to include the latitude and longitude obtained from the navigator. However, it seems like the request is being sent in the background without waiting for the navigator to complete its task. ...

"Spin an image using Javascript when it is shown on the

I've got a script that shows an image when we are attacked by a monster. The image is initially set to display="none", but switches to display="" when a monster appears. What I'm looking to do is make the image rotate 360° when it changes from ...

Discover similarities between two arrays

My goal is to compare two arrays and generate a JSON array marking true if there's a match and false if there isn't. The second array will always have values that match some from the first, and it will be smaller as it's derived from the fir ...

What is the method for displaying posts using JavaScript?

I'm experiencing an issue and could use some assistance. For instance, if I have HTML code like this: <div class="posts"> posts 1 </div> <div class="posts"> posts 2 </div> <div class="posts"> posts 3 </div&g ...

TabView with Confirmation Popup from PrimeNg

I'm having trouble making the PrimeNg TabView component work with a confirmDialog. Here's my code: <p-tabView (onChange)="onTabChange($event)" [(activeIndex)]="index">...</p-tabView> onTabChange(event){ this.confirmationServ ...

What are some solutions for managing SubPixel Handling issues on an iPhone?

Issue: I am currently in the process of developing a PhoneGap/Cordova app for both iOS and Android using jQuery Mobile. To incorporate a calendar feature into my app, I decided to create my own rather than use existing plugins due to not finding any that m ...

Is there a method to hide an HTML form completely?

Is there a way to quickly hide an HTML form from a webpage once the submit button is clicked and replace it with the result of a .php file in the most efficient manner possible, with minimal code? ...

encountered empty values when retrieving static paths

I need to retrieve the values of slug and tags in my params. It's important to note that tags is not an array, but just a string. export async function getStaticPaths() { const chapters = await client.fetch( `*[_type == "chapters" & ...

Activate ng-show when there are updates to the JSON data

I'm currently working on a feature where I need to hide the upload button and display a new edit button once the user uploads a file. My project involves using both angular-file-upload and md-data-table libraries. To achieve this, I have implemented a ...

Conceal the lower border of a targeted Tab using HTML

I'm working on a horizontal HTML tab and I want to hide the bottom border of the selected tab. Here is the code I currently have - https://jsfiddle.net/vgx2k7p5/ This issue has been raised in discussions on Stack Overflow here and here However, the ...

Adding a proptype for a setState method in React components

I am currently developing a reusable component for a project. This particular component includes user and setUser as props, much like the example provided below. const UserComponent = ({ user, setUser }) => { const calcHandler = useCallback(() =& ...

When making an Ajax request to a webpage, you may receive both a success message and an

I attempted to initiate an ajax call to successtext.html located on my computer. However, upon checking my browser's console, I noticed that both success and error messages were displayed. Here is the script I used: <script type="text/javascript"& ...

AngularJS Setting Default Values in HTML Pages

My goal is to set the default value for a dropdown in an Angular HTML Page. The page uses tabs, and I want the default value to load when a specific tab is clicked. <div class="col-md-9" ng-cloak ng-controller="ServiceTypeController"> <md-conten ...

What is the best way to calculate the total of all elements in an array?

I am currently learning JavaScript and facing a dilemma about whether to use return, document.write, or both in order to calculate the sum of all values in an array named 'amount'. I have been tasked with creating a function called amountTotal(). ...

Setting the useState hook to a User type in React - the ultimate guide!

As someone new to hooks, I'm unsure about what the initial value for useState should be set to. Currently, an empty object is set as the default value for useState with const [user, setUser] = useState({}); This is causing ${crafter.id} to throw an e ...

Tips for showcasing elements within a container while allowing users to scroll through them

I've been attempting to code a Figma design where the element starts at the middle of the screen height. As the user scrolls, the elements move upwards and stay visible until they are out of view, like in the example below. https://i.sstatic.net/imEb ...

Unable to transmit data to CodeIgniter controller through ajax communication

I'm struggling with sending a value from an input element to a Codeigniter controller using ajax. The problem arises because I am using a WYSIWYG editor (summernote), which only allows me to receive the input inside a <script>. However, when I ...

Oops! There was an error: Unable to find a solution for all the parameters needed by CountdownComponent: (?)

I'm currently working on creating a simple countdown component for my app but I keep encountering an error when I try to run it using ng serve. I would really appreciate some assistance as I am stuck. app.module.ts import { BrowserModule } from &apo ...