Skip to content

router: delay mounting RouterRoot until the first route is resolved (fixes #2621)#3030

Merged
dead-claudia merged 7 commits into
MithrilJS:mainfrom
kfule:delay-router-mount
Jun 24, 2025
Merged

router: delay mounting RouterRoot until the first route is resolved (fixes #2621)#3030
dead-claudia merged 7 commits into
MithrilJS:mainfrom
kfule:delay-router-mount

Conversation

@kfule
Copy link
Copy Markdown
Contributor

@kfule kfule commented Jun 15, 2025

Description

This PR delays the initial mounting of the router component until after the route has been resolved.
Since the component that displays nothing is no longer mounted first, the following improvements are made.

In addition, cleanup code has been added to RouterRoot's onremove to pass the test.

Motivation and Context

fixes #2621, and enables "loading..." like #2490

How Has This Been Tested?

npm run test with new tests

I have also confirmed that replacing the bundle improved the behavior of the following flems.

  • flems #2621
    • Replacing with the new bundle will cause the error to be forwarded to window.onerror.
  • flems like #2490
    • Replacing with the new bundle will cause "loading..." to be displayed.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

@kfule kfule requested a review from a team as a code owner June 15, 2025 10:42
Copy link
Copy Markdown
Member

@dead-claudia dead-claudia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you provide tests for the new behavior?

kfule added a commit to kfule/mithril.js that referenced this pull request Jun 16, 2025
The following tests were added.
- Existing content is cleared after the route is resolved.
- Errors are not caught by m.mount when the route component is mounted for the first time.
@kfule
Copy link
Copy Markdown
Contributor Author

kfule commented Jun 16, 2025

@dead-claudia
Thank you for your comment.
I added some tests for the new behavior introduced by this PR.

@kfule kfule requested a review from dead-claudia June 16, 2025 11:51
kfule added a commit to kfule/mithril.js that referenced this pull request Jun 16, 2025
The following tests were added.
- Existing content is cleared after the route is resolved.
- Errors are not caught by m.mount when the route component is mounted for the first time.
@kfule kfule force-pushed the delay-router-mount branch from 2369c9b to b27020d Compare June 16, 2025 11:53
@dead-claudia dead-claudia added patch and removed minor labels Jun 18, 2025
Copy link
Copy Markdown
Member

@dead-claudia dead-claudia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also add tests to ensure it shakes out with render? I would like tests for the same stuff, but I also want to know the precise tree state to expect during the first call to m.render.

I assume the tree would be cleared before it's called, as that's how it's currently architected, but I want this enshrined in tests so we know what specifically is supposed to be happening in that edge case.

@kfule
Copy link
Copy Markdown
Contributor Author

kfule commented Jun 18, 2025

@dead-claudia
I've added assertions to check the tree state. Could you let me know if this is what you were looking for?

If there are any other test cases you'd like me to add, please let me know with some details. That would be very helpful🙂

@kfule kfule requested a review from dead-claudia June 18, 2025 11:12
@dead-claudia
Copy link
Copy Markdown
Member

@kfule I'm okay with those tests, but I was really looking in particular for two things:

  • In route resolvers' render method, I wanted confirmation they 1. executed after onmatch resolves and 2. still rendered the correct tree. Both of these are likely already tested, so if they exist (I can't remember), you can just point me to them.
  • I wanted to know the tree state while route resolvers' render methods are being called, specifically on first call. I believe they should be empty, but I want this double-checked.

@kfule
Copy link
Copy Markdown
Contributor Author

kfule commented Jun 20, 2025

@dead-claudia
Thank you for your kind comments.

  • I added a test case to check the state of the tree during the route resolver's method call.
  • I also added a test to check the order in which the route resolver's methods (onmatch and render) are called.

I think these cases could probably be indirectly confirmed in previous tests, but I didn't immediately find any test cases that directly corresponded to them.

kfule added 7 commits June 22, 2025 18:22
…fixes MithrilJS#2621)

This commit delays the initial mounting of the router component until after the route has been resolved. The actual component is initially mounted, which fixes MithrilJS#2621. In addition, since the root DOM is not manipulated until the actual mounting, it makes easy to display something like "loading..." when using `onmatch` (cf. MithrilJS#2490).
Also, this commit adds cleanup code to RouterRoot's `onremove`. This keeps all existing tests pass.
This reduces the bundle size slightly.
The following tests were added.
- Existing content is cleared after the route is resolved.
- Errors are not caught by m.mount when the route component is mounted for the first time.
In addition, text has been added to make the changes to the tree clearer.
@dead-claudia dead-claudia merged commit 7f21dd7 into MithrilJS:main Jun 24, 2025
7 checks passed
@JAForbes JAForbes mentioned this pull request Jun 24, 2025
@kfule kfule deleted the delay-router-mount branch September 6, 2025 00:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Errors are not forwarded to window.onerror

2 participants