Incorporating '@vite-pwa/nuxt' into a nuxt 3 project leads to hydration issues

I have a Nuxt 3 website that I want to make PWA enabled. To achieve this, I am utilizing '@vite-pwa/nuxt'.

However, after adding the package and enabling PWA in my nuxt.config.ts file, I encountered two issues:

  1. Hydration errors occur when refreshing any page due to class mismatch.
  2. Refreshing from any route redirects back to the homepage instead of staying on the same page.

Below is a snippet of my nuxt.config.ts file for reference:

export default defineNuxtConfig({
  css: ['bootstrap/dist/css/bootstrap.min.css','~/assets/css/main.css',],
  devtools: { enabled: true },
  modules: [
    '@vite-pwa/nuxt',
      '@pinia/nuxt',
      'nuxt-swiper',
    '@vee-validate/nuxt',
    'nuxt-mdi',
  ],
  plugins:[
      "~/plugins/bootstrap.client.ts",
  ],
  app: {
    head: {
    }
  },
  pwa: {
    devOptions: {
      enabled: true,
      type: 'module'
    },
    manifest: {
      name: 'My Nuxt App',
      short_name: 'My Nuxt App',
      description: 'My incredible Nuxt app',
      theme_color: '#ffffff',
      lang: 'en'
    }
  }
})

The hydration error specifically occurs in the following HTML code where active class is conditionally added:

<li v-for="(navItem, index) in store.getNavItems" :key="index" class="nav-item me-4 me-xl-5 position-relative" :class="{ 'dropdown': !!navItem.links , 'active': $route.path.includes(navItem.route) }">

The error message reads:

[Vue warn]: Hydration class mismatch on <li class=​"nav-item me-4 me-xl-5 position-relative dropdown" data-v-inspector=​"components/​common/​Navbar.vue:​76:​13">…​</li> 
  
  - rendered on server: class="dropdown nav-item me-4 me-xl-5 position-relative"
  - expected on client: class="nav-item me-4 me-xl-5 position-relative dropdown active"
  Note: this mismatch is check-only. The DOM will not be rectified in production due to performance overhead.
  You should fix the source of the mismatch. 
  at <Navbar> 
  at <Default ref=Ref< undefined > > 
  at <LayoutLoader key="default" layoutProps= {ref: RefImpl} name="default" > 
  at <NuxtLayoutProvider layoutProps= {ref: RefImpl} key="default" name="default"  ... > 
  at <NuxtLayout> 
  at <App key=3 > 
  at <NuxtRoot>

I’ve searched for solutions but haven’t found one yet. Any insights into what might be causing these issues would be greatly appreciated.

Answer №1

The problem arises only when the development mode is enabled in PWA options. To resolve this issue, it is recommended to deactivate PWA during development.

Everything runs smoothly in the production environment. For those interested in investigating further, you can find the related GitHub issue at the following link:

GitHub Issue

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

Refine objects based on their properties without removing them from the dataset

I have a specific object structured as follows: var myObj: { 2:"None", 20:"A", 31:"A", 32:"A", Social:"B", Method:"None" } My goal is to retrieve the object without the properties 'Social' and 'Method'. Initia ...

The Socket.io authorization feature is failing to refresh session information

I am currently working on implementing Socket.IO's authorization function to retrieve session data. However, I have encountered an issue where even after logging out and destroying the session, Socket.IO retains the old session information, which is l ...

What is the best way to ensure my php variable is easily accessed?

Recently, I've been working on implementing a timer and came across the idea in a post on Stack Overflow. <?php if(($_SERVER['REQUEST_METHOD'] === 'POST') && !empty($_POST['username'])) { //secondsDif ...

The necessity of utilizing a dummy object for storing events in JavaScript is evident in certain situations

I am confused as to why in some instances a dummy object is needed in JavaScript to store events, like in the following code: Metal.Gold = function() { var temp = $("<div>"); //dummy object this.Submit = function(url1, method, data){ ...

AngularJS allows you to dynamically disable a button based on a value in an array

I have an array containing letters from A to Z and I want to create a list of buttons using them. $scope.alphabet = "abcdefghijklmnopqrstuvwxyz".split(""); I also have another array: $scope.uniqChar = ['a', ' ...

Is Jquery compatible with your Wordpress theme?

My current wordpress version is 3.4.1 and I want to include jQuery in my custom wordpress theme. Despite trying multiple solutions found online, I have been unsuccessful in implementing it convincingly. Can someone please provide a simple example to help ...

Securing API access by limiting it to verified domains with Express.js

I am currently enhancing the security of my Node.js/Express.js application by implementing restrictions on access to an authlist (authorized list) of domains. Note: The opposite of an authlist is a denylist. Overview: Users are able to create a Proje ...

MongoDB does not treat aggregate match pipeline as equal to in comparisons

I've been tackling an aggregate pipeline task for MongoDB where I need to retrieve items that do not have a specific user ID. Despite my efforts, I'm struggling to get it right. I attempted using $not, $ne, and $nin in various ways but couldn&ap ...

Creating a replicated Dropdown menu with Jquery and inserting it into a changing Div

I currently have a dropdown list on my page with the ID of "mainDDL" and some pre-existing data. Now, I am looking to duplicate this dropdown list and add it to another specific div element. To accomplish this, I used the following code snippet: var dd ...

Is there a way to encase each v-if statement in my Vue code with a transition effect?

Writing the code for transitions can be quite laborious. <transition name="fade"> <div v-if="condition"> </div> </transition> Is there a simpler way to automatically wrap each v-if with a transition? ...

Utilizing clip-path polygons for effective styling on Firefox and iOS

I have been working on a plugin to create animated modal boxes by utilizing the clip-path property. However, I have encountered an issue where this code only seems to work in Chrome. You can view the codepen demo here. Unfortunately, it appears that Firef ...

How to create select options in Angular.js without using the ng-option directive

I receive a JSON object from a service and I am using some of its fields to populate my select option list. However, when I try to print the selected value in my controller, the output response is "undefined". Can someone help me figure out what I'm ...

What is the method to conceal an element after detecting and locating a specific class using jQuery?

Can someone please assist me today with the following task: Step 1: <div id="htmlData"> <div class="col-md-12"> <div class="pull-left"> <h3>Report: <strong>Travel </strong></h3> ...

Streamlining Complex Javascript IF Statements

I came across a suggestion online for making an if statement more concise: if([1,5,7,22].indexOf(myvar)!=-1) alert('yeah') Instead of the longer version like this: if( myvar==1 || myvar==5 || myvar==7 || myvar==22 ) alert('yeah') Is ...

Tips for navigating to an element with Nightwatch

I am currently using nightwatch for end-to-end testing of my application. One of the tests is failing because it seems unable to scroll to the element that needs to be tested. I am wondering if scrolling is necessary, or if there is another workaround. Her ...

The markers fail to display on the leaflet map

My current project involves displaying coordinates from an API. I have successfully fetched the data, but the markers are not visible when shown on the map. Upon checking the console, I encountered the following error: Uncaught (in promise) TypeError: C ...

Utilizing ExpressJS to save uploaded files using the FileReader object from the front-end

Despite researching multiple posts on this topic, I am still unable to successfully upload a file after numerous adjustments. My React form includes a PDF file upload component which looks like this: <Input onChange={(e) => this.handleFileUpload(e) ...

Using Javascript to emphasize Ajax Response

I am faced with a challenge of highlighting specific words within text received from an ajax response, before generating HTML code and inserting it into the DOM. At the moment, I am utilizing the following code snippet: function highlightWords(line, word, ...

Executing the JavaScript function on a batch of 6 IDs at once (should return every 6 calls?)

I'm curious if there's a function available that can group called data into sets of 6. Here's the expanded version of the code var currentResults; function init() { getProducts(); } function getProducts() { $.ajax({ url:" ...

Calling a Coldfusion function from JavaScript/jQuery using a remote server connection

Although I am well-versed in JavaScript/jQuery, I am a newcomer to ColdFusion. After extensive research, I still can't figure out what seems like it should be a simple problem. My goal is to trigger a server-side call to a ColdFusion function from wi ...