Home > OS >  Is there a way to hook in to page reload in Nuxt?
Is there a way to hook in to page reload in Nuxt?

Time:11-14

I have an issue where I need to call a function every time the user reloads the page. Normally this would work by just using the created hook on App.vue, but since this project autogenerates the base component I'm not able to do this here.

I don't want to call this function when the user is navigating between routes, only when the page is reloaded/refreshed.

CodePudding user response:

I think that you do have not many options here, as far as I can see, because we can know that the page has been refreshed only from the browser, which means that you need to find a cross-browser solution.

Vue way: But you could keep this component alive and execute your code in created hook, and the code that should be executed when route changes is executed in route change hooks (beforeRouteEnter, etc.).

JS way: I suppose to think that PerformanceNavigationTiming API is the right what you need. Here you can find a detailed description of this method.

P.S. - Pay attention to browser support and that a lot of people use Apple devices.

CodePudding user response:

You can call it in a beforeMount() in the default layout.

CodePudding user response:

The approach @kissu proposed worked, by hooking in to beforeMount() on the default layout I was able to achieve what I wanted.

  • Related