Is there a way to pre-load images from component B within component A using Vue?

In a scenario where I have two Vue files, A.vue and B.vue, the issue arises when navigating from A to B. B contains numerous images fetched from Firebase realtime database, resulting in slow loading times upon first visit. To address this, I aim to preload these images in A.vue to ensure they are ready for display in B.vue when accessed.

My current approach involves utilizing a getUrls() method within the mounted() hook of A.vue to extract image URLs and store them in localStorage. Subsequently, in B.vue, the mounted() hook triggers the setImage() method with a callback function argument to set the images.

Although I've considered using router.BeforeEach() navigation guards methods, I require guidance on implementation and whether it resolves the delay issue.

Code Samples:

A.vue 
<template>
   <div>
   </div>
</template>

<script>
export default {
 methods:{
  getUrls: function(path, localStorage_id){
      var storage = Firebase.storage();
      var storageRef = storage.ref();
      var pathReference = storageRef.child(path);
      pathReference.getDownloadURL().then(function(url) {  
          let localStorageId = localStorage_id;
          localStorage.setItem( localStorageId, url);
      }).catch(function(error) {

      });
   }
 },
 mounted(){
  this.getUrls("path/to/img", "img_id");
 },
}
</script>

B.vue
<template>
   <div>
    <img id="img_id"> 
   </div>
</template>

<script>
export default {
 methods:{
  setImage: function(localStorageId, imgId, setSrc){
      var imgURL = localStorage.getItem(localStorageId);
      console.log(imgURL);
      setSrc(imgId, imgURL);            
  },
  // callback function
  setSrc: function(imgId, imgURL){
      var img = document.getElementById(imgId);
      img.src = imgURL; 
  }
 },
 mounted(){
  this.setImage("localStorage_id", "img_id", this.setSrc); 
 },
}
</script>

(style tags omitted for simplicity)

Despite implementing the above methodology, I am yet to observe significant improvements in image loading speed. Any suggestions on how to enhance performance in this context?

Answer №1

Summary: To speed up image loading, save them locally.

Facing a challenge with slow image rendering in my app, I found a simple yet effective solution. Recognizing the importance of images in the application, I decided to store them locally instead of retrieving them from the Firebase realtime database. This change significantly improved the loading time. I created a directory within my app to house these image files and adjusted my methods accordingly:

   setImage: function(my_imgURL, imgId, setSrc){
    var imgURL = my_imgURL; 
    console.log(imgURL);
    setSrc(imgId, imgURL);          
    },
    setSrc: function(imgId, imgURL){
        var img = document.getElementById(imgId);
        img.src = imgURL; 

    }

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

if else statement fails to work in ajax-based login form

I tried to create the code below by referencing processes.php, submit form, and ajax from various sources on the internet. However, my code doesn't seem to be working properly. proccess.php <?php session_start(); ini_set('display_errors&apos ...

Unable to administer AuthenticationController

I am attempting to inject a controller into my app.run function, but I keep encountering the following error: Uncaught Error: [$injector:unpr] http://errors.angularjs.org/1.2.10/$injector/unpr?p0=AuthenticationControllerProvider%20%3C-%20AuthenticationCon ...

Tabbed form design using Twitter Bootstrap

Currently, I am working on a website using the Bootstrap framework. My goal is to create a single page that features a pop-up form with two or three tabs. Specifically, I envision these tabs as "The Markup," "The CSS," and "The JavaScript." While I have ma ...

Unable to access parameters from Pug template when using onclick event

I am facing an issue with my pug template test.pug. It contains a button that, when clicked, should call a function using parameters passed to the template from the rendering endpoint. Below is the structure of my template: doctype html html head tit ...

Pressing the submit button will not successfully forward the form

I've encountered an issue with my submit buttons - when I click on them, nothing happens. They were functioning properly before, but now they seem to be unresponsive. What could be causing this problem? <form id="contact" name="updateorder" acti ...

Customize marker icon in Vue when clicked

Is it possible to change the marker icon by clicking on it using vue2-google-maps? I've tried writing some code, but it's not working as expected. Here is the template: <gmap-map ref="mainMap" :center="startLocation" ...

Enhance data granularity in Jquery Flot by zooming in as the user interacts

I'm currently working on creating an interactive chart that allows users to zoom in for a more detailed view of the data. For instance, when viewing data over a default zoom period of 3 months, there would be one data point displayed every 24 hours. H ...

The Angular application must remain logged in until it is closed in the browser

Currently, my Angular app includes authentication functionality that is working smoothly. The only issue is that the application/session/token expires when there is no user activity and the app remains open in the browser. I am looking for a solution wher ...

What are the steps to make ng-show functional in an AngularJS application?

I am attempting to implement a hover effect where an image is displayed over another image upon hovering over its container. I have been trying to achieve this using Angular and ng-show, but for some reason, the image with the ng-show attribute remains hid ...

How is it that the identical group is unable to produce an accurate line chart and its corresponding range chart?

Check out my code snippet on JSFiddle here: https://jsfiddle.net/8yf7j3k6/11/ I am currently working on replicating a similar visualization of my data for a range chart, allowing me to scrub through the chart while utilizing tooltips in the line chart rep ...

The JS variable text consistently displays as undefined

I have come across multiple posts on this topic, but none of them seem to be getting through to me or they are slightly different. This issue has been causing me confusion for quite some time. Despite my efforts to find a solution, I am met with conflicti ...

"Successfully uploading files with Ajax on Postman, however encountering issues when attempting to do so in

I am currently working on allowing specific users to upload videos to the API using Ajax. The process works smoothly in Postman, however, when attempting the same action from a web browser, I encounter a 500 Internal Server Error. Unfortunately, I do not ...

What is the process of passing a JavaScript variable to PHP with AJAX?

In the given scenario, I have an HTML file that is intended to display the content of a text file at div id="myDiv". The text file content will be read by PHP. However, there seems to be an issue in retrieving the content from the text file. I need assis ...

Retrieve the data of the current component from the slot template

This specific vue component showcases a CardGroup template with a headerRight slot. The content of the slot includes a message displaying the total number of items, accessed through the this.total data property. <template> <CardGroup> ...

Troubleshooting Problems with Cookie and Session Management

I'm encountering an issue while trying to set cookies and session values during login on the backend, which is built in node js. However, when react calls the API to verify these cookies or session values, they are returning as undefined... My middle ...

Every time I attempt to destructure the state object in react typescript, I encounter the error message stating 'Object is possibly undefined'

Whenever I attempt to destructure my state object in react typescript, I encounter an error stating Object is possibly 'undefined'. When I try using optional chaining, a different error pops up saying const newUser: NewUser | undefined Argument o ...

Output JSON data using Javascript

Here is some JSON data I am working with: { "lang": [ { "SECTION_NAME": { "english": "My title" }, "SECTION_NAME_2": { "english": "My title" } } ] } I ...

Determine the total number of nested objects within a JSON structure using Javascript

Consider the JSON structure below: { "id": "foo", "list": [ { "id": "A", "list": [ { "id": "B", "list": [ { "id": "C", "list": [ { ...

"Can you share how to send an array of integers from a Jade URL to an Express.js route

I am currently working on creating an array of integers in JavaScript using the Jade template engine. My goal is to send this array to an Express.js route when a button is clicked. I have attempted the following code: Jade File: //Passing the ID to fu ...

Retrieve the heading from a pop-up box

This jQuery tooltip allows for popups from another HTML page to be created. UPDATE: I have provided an example HERE The issue arises when trying to retrieve the title from the popup. Currently, using document.title displays the title of the current page ...