Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NuxtJS Page is created twice

I am currently facing an Issue in NuxtJS where a method is called twice and a request is therefore sent twice.

This happens in a page and the method which is called twice is created().

I open the page with a parameter like:

http://localhost:3000/mypage?token=123123123

And in the created() Method of the page I call a store dispatch.

created() {
  if (this.token === undefined || this.token === null) {
    this.$router.push('/login')
  } else {
    console.log('called created() and sent dispatch')
    this.$store.dispatch('thirdPartyLogin', {
      token: this.token
    })
  }
},

The token is parsed via the data property:

data() {
  return {
    token: this.$nuxt.$route.query.token
  }
},

The problem with this is that it is a One Time Token, which means that it is invalid after one use. So after the second call no more success of the request can take place.

Why is the page created twice or created() called twice?

Dev-tools screenshot

like image 405
Markus G. Avatar asked Feb 26 '20 10:02

Markus G.


People also ask

Does Nuxt use Babel?

Usage. Nuxt allows to customize the babel configuration for the build. But this does not take module files or nuxt. config.

Does Nuxt use webpack?

Nuxt use webpack-bundle-analyzer to let you visualize your bundles and how to optimize them.

How Nuxt middleware works?

A middleware receives the context as the first argument. In universal mode, middlewares will be called once on server-side (on the first request to the Nuxt app, e.g. when directly accessing the app or refreshing the page) and on the client-side when navigating to further routes.

What does Nuxt build do?

With nuxt build you enable rendering-on-the-fly. When a user visits a specific route, the Node. js server will quickly fetch the data, render it, and send it as a static HTML page to the client. Soon after, the application gets hydrated and becomes a single page application and SSR is no longer required.


2 Answers

created() and beforeCreate() are two lifehooks which are called on the server side and client side both. (you will see one console log in your terminal too, because server is firing i)

If you want to do this once you can :

a) use mounted() hook

b) if action needs to be done earlier than mounted u have to use if statement inside created method process.client. This if statement will check if you are on the client(browser side) if so, do the action

created(){
  if(process.client){
    //...your action here
  }
}
like image 178
BigKamil5 Avatar answered Sep 21 '22 21:09

BigKamil5


This is how it works:

Nuxt.js runs created() once on the server side then on the client side.

The Nuxt SSR shows the console.log message of your server and the second console.log is the message on the client side.

You have 2 Options:

Run this on the serverside:

Wrap it in:

   if(process.server){
   }

Or run it once on the client side:

   if(!process.server){
   }
like image 35
Ifaruki Avatar answered Sep 21 '22 21:09

Ifaruki