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
Is it possible to show one route over another? #408
Comments
This is something you could get working with a custom route action, but I do see an easy way to achieve that when using the { path: "/users/:id",
action: async(context, commands) => {
// simulates an async fetch operation
await new Promise(resolve => setTimeout(resolve, 1000));
if (context.params.id === 'sam') {
return commands.component('profile-view');
} else {
return commands.component('not-found-view');
}
}
}, Here is a demo: https://glitch.com/edit/#!/408-dynamic-route-componenet?path=script.js:11:9 The downside of this approach is that the router displays a blank page while the async action is in progress. An alternative solution would be to change the |
Yes, leaving the screen blank while the request is not resolved is not a valid option for us. 😕 In fact, right now we are making the data requests from the const GET_USER = gql`
query GetUser($id: ID!) {
user(id: $id) {
username
fullName
}
}
`;
@customElement('page-user')
export class PageUser extends connectApollo(client)(PageElement) {
// ...
protected onBeforeEnter(location: Router.Location) {
// When `this.requestQuery` resolves, it populates `data` and `loading` properties
this.requestQuery({
query: GET_USER,
variables: {
id: location.params.id
}
});
}
protected shouldUpdate() {
if (this.data && this.data.user && !this.loading) {
// TODO: Show page-not-found element
}
return true;
}
} |
The approach you describe looks very reasonable to me. The The router has no knowledge of the internal logic of the component, it renders the Do you have a specific feature request in mind? I am not sure how a router can help you here? |
With this scenario, I have two ideas in mind:
Sometimes it will be interesting to have a custom not-found page, but other times it will be interesting to share the same not-found page. |
The feature as I understand it is about managing the route component's state: loading / loaded OK / loaded not OK. I can see how this feature may by useful in many apps, so it could be considered for the Router roadmap in the future. As a workaround now I would suggest explicitly re-using the render() {
if (this.loading) {
return html`Loading...`;
} else if (this.user) {
return html`
<p>ProfileView</p>
...
`;
} else {
return html`<not-found-view></not-found-view>`;
}
} See a demo here: https://stackblitz.com/edit/408-three-state-component?file=views%2Fprofile-view.js |
Thank you @vlukashov! For now I will do that. I imagine something like |
One thing to consider is that navigation should probably still point to the correct page, see the example: As you can see, unlike the real 404 page, user still has the visual context (the "dashboard" view is active). This can be achieved differently, depending on the way the app is implemented:
In the above example (the app I used to work on), the second approach is used. IMO it is more flexible, because it allows to use different layouts, e.g. with or without drawer, depending on the view - for example, in case of unauthorised user. |
My use case is:
I have a
page-user
element for the route/user/:id
.In this element, I request the user info in the
onBeforeEnter
callback using thelocation.params.id
.When the request return the user, everything is fine. But if the user doesn't exits, I want to show the
page-not-found
element (a 404 page) without having to redirect or change the route.Is this possible to do?
The text was updated successfully, but these errors were encountered: