Randomize the array of images in Nuxt every time the page is reloaded to display a variety of logos in the carousel

I've set up a carousel of logos, with the intention to randomize their order every time the page reloads. The challenge is that my layout only allows for 10 images to be displayed at once.

While it seems like everything is functioning correctly, I've encountered an issue: although the original images array is successfully shuffled and a computed value is generated to loop through the newly shuffled images, the desired effect only happens when navigating away from the page and returning. Simply reloading the page results in the same image sequence being displayed.

I've also observed that the original array is being shuffled despite having a specific computed value and property designated to store the new sorted array.

<template>
  <div class="site-section--partners__images" role="list">
    <div
      v-for="(logo, index) in SelectedLogos"
      :key="index"
      class="site-section--partners__logo"
      role="listitem"
    >
      <a :href="logo.url" target="_blank">
        <img
          class="site-section--partners__image"
          :src="logo.image"
          :alt="logo.alt"
          :title="logo.alt"
        />
      </a>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      partnersLogos: [ 
        // Array of logo objects 
      ],
      ShuffledLogos: null,
    }
  },
  computed: {
    SelectedLogos() {
      return this.ShuffledLogos.slice(0, 10)
    },
  },
  created() {
    this.ShuffledLogos = this.selectRandomLogo(this.partnersLogos)
  },
  methods: {
    selectRandomLogo(items) {
      // Method to shuffle items array randomly
    },
  },
}
</script>

Despite implementing a method to shuffle the original array (partnersLogos) and storing the shuffled logos in the ShuffledLogos array, which is then utilized to create a computed value and loop through, displaying the randomized images upon each page refresh is proving to be challenging.

Answer №1

After exploring various avenues, it became evident that @ErsinDemirtas' insightful solution in the comments was the most effective approach. Therefore, I have decided to directly replicate his comment here as he rightfully deserves all the recognition:

It seems like the issue lies within your build configuration. It appears that the computed property is being executed only once and then treated as a static resource. To address this, you need to verify if Server-Side Rendering (SSR) is enabled on your production environment. This essentially means that the computed data is computed just once and subsequent refreshes will yield the same outcome. Ideally, you want the shuffle operation to be performed solely on the client side. In my opinion, I would encapsulate this component within a client-only context. For more detailed information on this, please refer to: nuxtjs.org/docs/features/nuxt-components/...

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

The majority of my next.js website's content being indexed by Google consists of JSON and Javascript files

I’m facing an issue with Google indexing on Next.js (utilizing SSR). The challenge lies in ensuring that .HTML files are effectively indexed for SEO purposes. However, it seems that Googlebot predominantly indexes JSON and JavaScript files. To illustra ...

Set boundaries for the width and height of an image in the input field

Looking to restrict the size of an image in an input field using HTML, CSS, and JavaScript/jQuery. Goal is to maintain a perfect square aspect ratio for profile photo uploads (for example, 200x200 or 300x300). ...

Seeking an efficient localStorage method for easy table modification (HTML page provided)

Currently, I am in the process of developing a custom 'tool' that consists of a main page with a menu and several subpages containing tables. This tool is intended for composing responses using prewritten components with my fellow colleagues at w ...

How to efficiently transfer data between Node and Angular 7 using Electron

After setting up an Angular 7 application running on http://localhost:4200, I developed a Node JS application responsible for authenticating users on Facebook, accessible at http://localhost:3000. The callback redirection functions correctly within the No ...

VueJS Custom Tag Input and Checkbox Component

Currently, I am developing a feature that involves the use of Laravel and VueJS. In the image provided, you can see that on the right-hand side there are two checkbox groups: 1. Size and 2. Color. On the left side, there is an Input Tag Text Area or Text ...

Refreshing data causes the data to accumulate and duplicate using the .append function in Jquery

I am currently working with a dynamic table that is being populated using AJAX and jQuery. Everything seems to be functioning correctly, however, I am encountering an issue when trying to reload the data as it just continues to stack up. Below is the func ...

saving user's scroll position in a Vue 3 app

Encountering an issue with my Vue application. It comprises more than 40 pages, and the problem lies in the browser caching the scroll position. As a result, when users navigate from one page to another, the browser displays the scroll position of the prev ...

Tips for optimizing the processing speed of large XML files using jQuery, Javascript, and PHP

Currently, I am developing a store overview page that displays about 20 products per page. The data for this page is sourced from a zipped (gzip) XML file (*.xml.gz). You can find the feed here: . Every day, I download this file to my server using PHP and ...

Encountered a TypeError in Angular printjs: Object(...) function not recognized

I'm currently working on integrating the printJS library into an Angular project to print an image in PNG format. To begin, I added the following import statement: import { printJS } from "print-js/dist/print.min.js"; Next, I implemented the pri ...

Is there a way to display the true colors of a picture thumbnail when we click on it?

In my project, I attempted to create a dynamic color change effect when clicking on one of four different pictures. The images are displayed as thumbnails, and upon clicking on a thumbnail, it becomes active with the corresponding logo color, similar to t ...

Switch up a JSON string using JavaScript

I received a JS string through an AJAX API call containing data like this: {"Task":"Hours per Day","Slep":22,"Work":25,"Watch TV":15,"Commute":4,"Eat":7,"Bathroom":17} My goal ...

Retrieve the updated file name from the mini file upload form

I have implemented the Mini AJAX Upload Form from this tutorial to upload files to a server. I made modifications to add a timestamp at the end of the file name. How can I return the updated file name (with the timestamp) back to the client for future refe ...

How to access Bootstrap's modal initial data-* upon closing

When displaying a bootstrap modal, a convenient method to retrieve associated data is by using the following code snippet: $('#myModal').on('show.bs.modal', function (e) { var id = $(e.relatedTarget).data('id'); alert( ...

Execute unit tests for the nodejs project

Looking to execute the tests for this project on GitHub? Head over to the test folder on https://github.com/node-opcua/node-opcua. However, I'm unsure about the testing framework that was utilized and how to run all the tests. Any guidance would be mu ...

Why is the Slick Slider displaying previous and next buttons instead of icons?

I'm currently working on creating a slider using the slick slider. Everything seems to be functioning properly, but instead of displaying arrows on the sides, it's showing Previous and Next buttons stacked vertically on the left side. It appears ...

The mouse scurries away once the div height has been adjusted

How can I make the height of #header change when hovering over #hoverme, and then revert back to its original height when the mouse leaves #hoverme? If anyone knows a solution, please check out my jsfiddle as it's not working as I intended. Here is ...

Encountered a 'Require() of ES Module' Error while Implementing Visx with Nextjs

Currently, I am utilizing the Visx library for chart creation within Nextjs. The scales provided by Visx are imported in this manner: import { scaleBand, scaleLinear, scaleOrdinal } from "@visx/scale" It is important to note that internally, Vi ...

Implementing a two-column infinite scrolling feature using ISCroll

I am looking to incorporate multi-column infinite scrolling using IScroll infinite scrolling. I want the content of my HTML to appear as follows: <ul> <li>A</li> <li>B</li> <li>C</li> <li>D</li> ...

How can I display different divs based on a selected option?

I'm encountering difficulties while attempting to develop the code for this specific task. My goal is to display or hide divs based on the selection made in a select option, with a total of 4 options available. There are 2 select elements <select ...

HTML TABS: Make the first TAB automatically selected

I've been experimenting with tabbing in HTML using a tutorial I found on W3SCHOOLS. While the source code provided works fine, I'm encountering an issue with automatically selecting the first tab by default. The tutorial doesn't cover this, ...