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.

The requested page still loads when making a redirection via route middleware

See original GitHub issue

Environment

  • Operating System: Darwin
  • Node Version: v16.15.1
  • Nuxt Version: 3.0.0-rc.6
  • Package Manager: yarn@1.22.19
  • Builder: vite
  • User Config: ssr, components, meta, alias, buildModules, vue, css, modules, postcss, runtimeConfig, vite
  • Runtime Modules: @nuxtjs/tailwindcss@5.0.3
  • Build Modules: @nuxtjs/eslint-module@3.1.0, @pinia/nuxt@0.3.0

Reproduction

https://codesandbox.io/s/zen-bohr-d8yhrj

Describe the bug

When a middleware which makes a redirection to an external URL executes, the requested page still loads after the middleware, which then triggers undesired behaviours as, for example, the middleware may have been guarding the page from loading if the required authentication to load the resources the page needs was not yet available to the app.

If this is somehow the expected behaviour, I would appreciate if somebody could guide me into how to stop the page from loading after an external redirect in a middleware.

Let me know if you need any further details.

Thanks for the help!

Additional context

In the repro, I’m using a helper function provided by @justin-schroeder which is a wrapper of H3’s sendRedirect() , given that we still cannot redirect using Nuxt’s navigateTo(), see #5022.

Use case: I’m trying to implement an OAuth 2.0 client middleware to get a token from my custom OAuth 2.0 server. Because of this issue, errors are triggered by the loaded page since the token has not yet being retrieved and the page attempts to load several protected resources. This and other current bugs/lack of features such as the aforementioned external redirection issue and Pinia not being able to be used inside server middleware (see https://github.com/vuejs/pinia/discussions/1212) make it quite painful to implement something so critical for corporate-sized apps such as a custom OAuth 2.0 flow.

Logs

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
larbishcommented, Sep 1, 2022

Indeed, even after a navigateTo inside a middleware, the guarded route is still called. To be more accurate, the setup function is called but not the onMounted method. This behaviour only occurs on server side.

Here is a reproduction: https://stackblitz.com/edit/github-ueekmy?file=pages%2Fabout.vue,middleware%2Fredirect.ts

In my case, this workaround works.

1reaction
larbishcommented, Sep 6, 2022
Read more comments on GitHub >

github_iconTop Results From Across the Web

Laravel 5.2 middleware auth keeps redirecting me to login page
I'm trying to build a simple admin dashboard with Laravel 5.2 and I created an admin middleware that includes 'web' and 'auth' middleware....
Read more >
How to implement redirects in Next.js - LogRocket Blog
Redirects enable users to transfer an executed URL to a new URL to reroute an incoming request from one path to another.
Read more >
Creating Redirects With Nuxt - Josh Deltener
In this post we'll dive into the different ways you can create redirects with Nuxt.
Read more >
Back to Basics: Rewriting a URL in ASP.NET Core - Rick Strahl
Any middleware registered after the rewrite sees the new URL and processes the remainder of the request with the new path. All of...
Read more >
Configure ASP.NET Core to work with proxy servers and load ...
Forwarded Headers Middleware is activated to run first in the middleware pipeline with a restricted configuration specific to the ASP.NET Core ...
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