bridge icon indicating copy to clipboard operation
bridge copied to clipboard

[Middleware] To and From are always one route late when using automatic middleware mapping (01,02...etc)

Open rrochaTG opened this issue 1 year ago • 2 comments

Environment

  • Operating System: Linux
  • Node Version: v18.17.1
  • Nuxt Version: 2.17.3
  • CLI Version: 3.10.1
  • Nitro Version: 2.8.1
  • Package Manager: [email protected]
  • Builder: webpack
  • User Config: ssr, head, css, plugins, buildModules, modules, axios, app, build, serverHandlers, devServerHandlers, devServer, typescript, nitro, bridge
  • Runtime Modules: @nuxtjs/[email protected]
  • Build Modules: (), @nuxtjs/[email protected], @nuxt/[email protected]

Reproduction

2.0 With Bridge: https://github.com/rrochaTG/reproduction-error-middleware-bridge 3:0: https://github.com/rrochaTG/reproduction-correct-vue-3

Describe the bug

Go to localhost:3000/clients and then click the link. You will see that from is undefined (there is already an issue for that: https://github.com/nuxt/bridge/issues/954), but after clicking the link both from and to have the same route. Changing routes again and is always a route late.

This issue has caused problems in some of our systems during the upgrade process, so I'm keen to determine if it's truly a bug or a misconfiguration on our end. It seems to be a bug, as in Vue 3, it works as expected: https://github.com/rrochaTG/reproduction-correct-vue-3

Additional context

No response

Logs

No response

rrochaTG avatar Mar 06 '24 19:03 rrochaTG

It would be easier to investigate if you could describe what kind of logs you expect to get. 🙏

wattanx avatar Mar 07 '24 13:03 wattanx

Good point, sorry about that. I would expect the same in the Vue 3 version.

If you are in /clients and go to /categories, I would expect the names of the routes to be: to: "categories" from: "clients"

However, what I get is this: image

This is different from what I get in the Nuxt 3 version (The 3.0 example I posted has different URLs, but it shows it working as expected):

image

Now if I keep going in the 2.0 version to '/products' what I get is this: image

Which it is the correct answer the route before this one (when we were in /categories), but dons't make sense here, since whe are going to '/products'. What I would expect in this case is: to: 'products' from: 'categories'

rrochaTG avatar Mar 07 '24 14:03 rrochaTG

the global middleware can't get the real value,different from nuxt3

auth.global.ts

export default defineNuxtRouteMiddleware((to, from) => {
  console.log('to', to) // when navigateTo other page from index.vue, to.path always be /
  console.log('from', from)

  const { token } = useMainStore()

  if (to.path !== '/') {
    if (!token && to.path === '/requiredAuth') {
      return navigateTo('/login')
    }

    if (token && to.path === '/login') {
      return navigateTo('/requiredAuth')
    }
  }
})

change auth.global.ts to auth.tsto.path get the real value

Ttou avatar Apr 17 '24 16:04 Ttou