How can I refresh the whole page on link visit in NuxtJS?
Asked Answered
C

8

8

I have a Nuxt project and for the SEO purpose, I need to reload the pages every time I do visit any page. Is there any way I can do it on Routers?

Currently, my app is with the default behavior of NUXT " renders only the required components on visit". But I need to completely reload the pages.

Crassus answered 8/10, 2019 at 9:51 Comment(3)
Doesn't make any sense. For seo it will follow link in HTML and it will be server rendered each time. What you trying to do is to spoil use experienceMireyamiriam
What is that "SEO purpose"?Iowa
Reference DOCSerna
L
7

Check this.

this.$nuxt.refresh()
Laraelaraine answered 31/3, 2022 at 19:13 Comment(0)
Y
4

I do believe that reloading the page cleares (and re-initializes) your vuex store, if you are using that

I don't know how to reload the page but I have a few suggestions that you could test

Suggestion 1 Wrap your page with a v-if. Then it wont render until you visit it

<template>
  <app v-if="someBoolean">
     .
     .
  </app>
</template>
<scripts>
  export default {
    computed: {
      someBoolean () {
        return blabla 
      }
    }
  }
</scripts>

Suggestion 2

this.$forceUpdate();

Suggestion 3 Bind your component to a key

<template>
  <yourComponent :key="componentKey" />
</template>

And change that key whenever you need re-rendering

Yarak answered 8/10, 2019 at 10:32 Comment(0)
D
4

Try: window.location.reload(true)

Dubbing answered 10/10, 2021 at 8:40 Comment(0)
S
2

If you want to refresh the page manually you can call - this.$router.app.refresh()

Slipover answered 12/7, 2020 at 17:47 Comment(0)
L
1

I had the same issue, but this solved it well, just watch changes in the $route() method then refresh appropriately in the layouts/default.vue file.

<template>
  <div>
     <Nuxt keep-alive />
  </div>
</template>

<scripts>
    export default {
        watch: {
            $route() {
                location.reload();
            },
        }
    }
</scripts>
Liquid answered 27/10, 2021 at 9:59 Comment(0)
M
0

In your layout nuxtjs, bind key property of router view to fullPath which always change on router change. It help rerender router:

 <nuxt :key="$route.fullPath"></nuxt>
Maxon answered 28/10, 2021 at 9:56 Comment(0)
B
0

If you want to refresh every page you move to.. Just use regular anchor tag <a href="/myRoute"> instead of <nuxt-link to="/myRoute">

Bastard answered 2/3, 2022 at 22:14 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Thaine
U
0

This worked for me: this.$router.go(0)

window.location.reload() causes error if it used inside html template, works only as method

Unmindful answered 5/1 at 20:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.