kriasoft / universal-router

A simple middleware-style router for isomorphic JavaScript web apps
https://www.kriasoft.com/universal-router/
MIT License
1.7k stars 105 forks source link

Middlware approach context.next and redirection #190

Closed ndrean closed 3 years ago

ndrean commented 3 years ago

I'm submitting a ... bug?

Notes:

Bug reports

A bug is a demonstrable problem that is caused by the code in the repository. Good bug reports are extremely helpful - thank you!

Guidelines for bug reports:

  1. Use the GitHub issue search — check if the issue has already been reported.

DONE

  1. Check if the issue has been fixed — try to reproduce it using the latest master or development branch in the repository.

  2. Isolate the problem — ideally create a reduced test case and a live example.

A good bug report shouldn't leave others needing to chase you up for more information. Please try to be as detailed as possible in your report. What is your environment? What steps will reproduce the issue? What browser(s) and OS experience the problem? What would you expect to be the outcome? All these details will help people to fix any potential bugs.

Example:

Short and descriptive example bug report title

A summary of the issue and the browser/OS environment in which it occurs. If suitable, include the steps required to reproduce the bug.

I include the example: https://stackblitz.com/edit/demo-universal-router-react

I use your middleware approach with "context.next" to build the routes. In this manner, I can't redirect. To see this: select the "routeNext" in the "renderRoute" method, and choose "mode:A" for redirection, and anything else if not. This fails. I included the "good" example of redirection without "next". For this, choose "route" in the renderRoute, and you can play with the "mode:A" for redirecting or not. Not sure of what the parser does with the condition "if (true) return {redirect: "/"}". It seems to only append the key "redirect" but does not replace them all. The error message you get is: "Objects are not valid as a React child (found: object with keys {redirect})"

  1. This is the first step
  2. This is the second step
  3. Further steps, etc.

<url> - a link to the reduced test case

Any other information you want to share that is relevant to the issue being reported. This might include the lines of code that you have identified as causing the bug, and potential solutions (and your opinions on their merits).

Feature requests

Feature requests are welcome. But take a moment to find out whether your idea fits with the scope and aims of the project. It's up to you to make a strong case to convince the project's developers of the merits of this feature. Please provide as much detail and context as possible.

frenzzy commented 3 years ago

You are seeng the errors because you are actually trying to render <NavNext>{{ redirect: "" }}</NavNext> which is not allowed in React.js. The "next" function of a middleware action gets a child route result as is:

- const component = await next(true);
+ const component = { redirect: "" };

We have some docs about redirects here: https://github.com/kriasoft/universal-router/blob/master/docs/redirects.md

ndrean commented 3 years ago

Ah thks, yes stupid! I did not look at what component looked like...

const component = await next(true); 
if (component.redirect) { return component; } 
return component && <NavNext>{component}</NavNext>; },

works because I add condition in router.resolve. Thks for the help. Le samedi 19 décembre 2020 à 22:13:03 UTC+1, Vladimir Kutepov notifications@github.com a écrit :

You are seeng the errors because you are actually trying to render {{ redirect: "" }} which is not allowed in React.js. The "next" function of a middleware action gets a child route result as is:

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.