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
Issue: Support NotFound Routes in IonRouterOutlet, old title: bug: Routing fall-through is not working on latest React build #20105
Comments
Hi @ebk46, This is by design and functions similar to how BrowserRouter works. By default, any route that matches will be rendered. To fine grain control which routes you want to render in an IonRouterOutlet, use However, I do realize that this does not solve the NonFound use case you are going for here. With BrowserRouter, you would put your routes into a Switch statement and have the last route not have a path attribute, so if no routes match it would it the route like so: <BrowserRouter>
<Switch>
<Route exact path="/page1" component={Page1} />
<Route exact path="/page1/details" component={Page1Details} />
<Route component={NotFound} />
</Switch>
</BrowserRouter> However, switches are not supported in IonRouterOutlets, because we need to control and keep pages around in the dom even if a user navigates away from them for page transition and state purposes. So doing a NotFound route is not possible inside of an IonRouterOutlet. You would have to do it outside of the IonRouterOutlet and that would require you to setup routing in a way that might not be ideal for your app. I'll keep this issue open and repurpose it to investigate a way to do NotFound routes inside of IonRouterOutlet. |
So essentially, you would need to start with a |
You could stick with IonReactRouter, but you would have to define those initial routes outside of IonRouterOutlet like so: <IonReactRouter>
<Switch>
<Route path='/app' render={() => (
<IonRouterOutlet>
<Route exact path='/app/page1' component={Page1} />
<Route path='/app/page1/details' component={Page1Details} />
<Redirect from="/app" to="/app/page1" exact />
</IonRouterOutlet>
)} />
<Route component={NotFound} />
</Switch>
</IonReactRouter> Downside here is you have to have un otherwise unneeded sub path ( /app here) that your IonRouterOutlet would listen to, and any unknown routes to /app (like /app/blah) wouldn't be covered by the NotFound route. full example: |
I see. This is essentially what I thought you had meant, just simplified with the /app route predicate. I tested your solution and it seems to work on my project, thanks! |
I thought of another solution to this problem. Basically I check and see if the user is currently using Capacitor or not, and if so, then I render the Ionic router. Otherwise, I use the standard React router with a switch component to enable fallback on browsers, while retaining Ionic specific functionality for native apps.
EDIT
|
Not round routes are now available in 4.11.9. To use one, setup one of the routes below inside of the
or
|
Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Ionic, please create a new issue and ensure the template is fully filled out. |
Bug Report
Ionic version:
[x] 4.11.7
Current behavior:
When setting up sequential routes, all routes are rendered in layers rather than just the most specific.
Expected behavior:
Only the most specific route should be matched and rendered.
Steps to reproduce:
Set up multiple routes with different specificity. Standard routing indicates that the first matching route (or most specific route) should be matched and rendered. The Stackblitz included below has "/page1" followed by a catchall "/" for any routes that don't match.
Route to the first/most specific route ("/page1" in the case of the stackblitz)
Note that the second route is rendered "over" the first, making it seem like the first isn't rendered at all.
Related code:
https://stackblitz.com/edit/ionic-v4-11-7-route-not-found
In the stackblitz, in addition to seeing the NotFound page rendered when the path is "/page1", you can look at the console to see that both routes are actually being rendered. Further, if you swap the order of the routes, you can see that the page appears to render correctly but both routes are still being rendered (NotFound is just underneath Page1).
Other information:
Ionic info:
The text was updated successfully, but these errors were encountered: