question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Global Middleware on first load both side

See original GitHub issue

Environment


  • Operating System: Linux
  • Node Version: v16.14.2
  • Nuxt Version: 3.0.0-27484505.9f6eff7
  • Package Manager: yarn@1.22.18
  • Builder: vite
  • User Config: vite, privateRuntimeConfig, publicRuntimeConfig, css, buildModules, intlify, vueuse
  • Runtime Modules: -
  • Build Modules: @intlify/nuxt3@0.1.10, @pinia/nuxt@0.1.8, @vueuse/nuxt@8.2.5

Reproduction

https://stackblitz.com/edit/github-dypzgk

Describe the bug

On first load, the global middleware get executed on both side (server and client). In Nuxt 2, if the app is in universal mode, at first load the middleware get executed only by server side.

Additional context

No response

Logs

No response

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
Atinuxcommented, Nov 16, 2022

@pskclub You can use if (process.server) for example if you want to run it only on server.

1reaction
jdgjxxchycommented, Nov 19, 2022

@pskclub You can use if (process.server) for example if you want to run it only on server.

i only need to run once if the route changes, how can i do that?

I have also ran into this issue. I find a way to judge whether it is running in server side or in client.

  1. use process.client or process.server. if code is running in client side, process.client will be true. So if you want to count page enter times, you can add if (process.client) block.
  2. use useNuxtApp() . If you need to access nuxtApp by coincidence, you can use useNuxtApp().ssrContext to judge, because this property only exist in server side.
Read more comments on GitHub >

github_iconTop Results From Across the Web

Is it possible for middleware to be executed only on client side
global middleware still doesn't run clientside on initial page load. Not sure if that's just by design. My core problem may be from...
Read more >
Node Express 4 middleware after routes - Stack Overflow
The correct answer is using the res.on("finish", cb) callback. i.e.: express.use(function(req, res, next) { console.log("before"); ...
Read more >
Writing middleware for use in Express apps
Every time the app receives a request, it prints the message “LOGGED” to the terminal. The order of middleware loading is important: middleware...
Read more >
Advanced Features: Middleware - Next.js
Middleware allows you to run code before a request is completed, then based on the incoming request, you can modify the response by...
Read more >
Middleware directory - Nuxt
A middleware receives the context as the first argument. ... With ssr: false , middlewares will be called on the client-side in both...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found