Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

navigateTo has no effect when a plugin is declared #14771

Closed
Baloche opened this issue Sep 2, 2022 · 7 comments
Closed

navigateTo has no effect when a plugin is declared #14771

Baloche opened this issue Sep 2, 2022 · 7 comments

Comments

@Baloche
Copy link

Baloche commented Sep 2, 2022

Environment


  • Operating System: Darwin
  • Node Version: v16.15.1
  • Nuxt Version: 3.0.0-rc.8
  • Package Manager: npm@8.11.0
  • Builder: vite
  • User Config: runtimeConfig, css, vite, typescript, modules, unocss
  • Runtime Modules: @unocss/nuxt@^0.44.7, @vueuse/nuxt@9.1.1
  • Build Modules: -

Reproduction

https://codesandbox.io/s/nuxt-3-navigateto-with-usefetch-15ihel

Describe the bug

To intercept every 401 error I receive from my API, I created a wrapper method of useFetch :

export function useApi(request) {
  return useFetch(request, {
    async onResponseError(ctx) {
      if (ctx.response.status === 401) {
        await navigateTo("/login", { replace: true, redirectCode: 401 });
      }
    }
  });
}

But somehow, it only works if I don't have any plugins declared.
If you comment every line of this file, the redirection works well, but as long as the defineNuxtPlugin function is called, the redirection does not work. Why ?

Additional context

No response

Logs

No response

@Baloche
Copy link
Author

Baloche commented Sep 5, 2022

I was hoping rc.9would fix it, but the issue is still there 😢
Codesandbox updated !

@pi0
Copy link
Member

pi0 commented Sep 5, 2022

Hi @Baloche sorry for answering late. I have made updated working example for you:

https://stackblitz.com/edit/github-yjetdx?file=composables%2Findex.ts

Please see #14269 and #14723 for better context.

@Baloche
Copy link
Author

Baloche commented Sep 5, 2022

Thanks for the answer @pi0 :)
Unfortunately, it is working because there is no plugin 😄
It doesn't work if you create the file plugins/guilty.ts

export default defineNuxtPlugin(() => {
  return {
    provide: {
      hello: (msg: string) => `Hello ${msg}!`,
    },
  };
});

@pi0
Copy link
Member

pi0 commented Sep 5, 2022

Right! Try this updated sandbox: https://stackblitz.com/edit/github-yjetdx?file=composables%2Findex.ts (#14805)

@Baloche
Copy link
Author

Baloche commented Sep 5, 2022

Thank you! I was trying the same lead ! It works!

@pi0
Copy link
Member

pi0 commented Sep 5, 2022

Happy hearing this! Let's track improvements from linked issues.

@pi0 pi0 closed this as completed Sep 5, 2022
@danielroe danielroe added the 3.x label Jan 19, 2023
@danielroe danielroe transferred this issue from nuxt/framework Jan 19, 2023
@maximepvrt
Copy link
Contributor

@pi0, I've encountered an issue that I could use your help with. Everything seems to be perfect, but I'm running into an error on the pages/index.vue page when I try to reference a user attribute. I thought the redirection was supposed to take place before the template generation, right? Additionally, I'm curious if callWithNuxt functions the same as nuxtApp.runWithContext. Your insights would be greatly appreciated! Thank you.

https://codesandbox.io/p/sandbox/nuxt-3-navigateto-with-usefetch-forked-r4rz6m

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants