Skip to content

Commit

Permalink
fix #173
Browse files Browse the repository at this point in the history
  • Loading branch information
GianlucaGuarini committed Nov 10, 2023
1 parent a782a4d commit 4635aed
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
4 changes: 4 additions & 0 deletions demos/components/app.riot
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
<app>
<router>
<nav>

<a href="/hello">Hello</a>
<a href="/user">User</a>
<a href="/user/gianluca">Username</a>
<a href="/user/gianluca/#anchor">Username with anchor</a>
<a href="/goodbye">goodbye</a>
</nav>
<route path="(.*)">
Every route :)
</route>
<route path="/hello">hello</route>
<route path="/user">user</route>
<route path="/user/:username/([?#].*)?">
Expand Down
8 changes: 7 additions & 1 deletion src/components/route-hoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,14 @@ export const routeHoc = ({ slots, attributes }) => {
this.callLifecycleProperty('onUnmounted', route)
},
onRoute(route) {
const prevRoute = this.state.route
this.state.route = route
this.mountSlot()

// if this route component was already mounted we need to update it
if (prevRoute) this.slot.update({}, this.context)
// this route component was never mounted so we need to create its DOM
else this.mountSlot()

// emulate the default browser anchor links behaviour
if (route.hash) $(route.hash)?.[0].scrollIntoView()
},
Expand Down
22 changes: 22 additions & 0 deletions test/components.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import HistoryRouterApp from './components/history-router-app.riot'
import NestedUpdates from './components/nested-updates.riot'
import RecursiveUpdatesBugRouter from './components/recursive-updates-bug-router.riot'
import StaticBasePath from './components/static-base-path.riot'
import SameRouteMatches from './components/same-route-matches.riot'
import { component } from 'riot'
import { expect } from 'chai'
import { router, defaults } from '../src/index.js'
Expand Down Expand Up @@ -83,4 +84,25 @@ describe('components', function () {

comp.unmount()
})

it('Routes matched multiple times do not render twice (bug 173) ', async function () {
const el = document.createElement('div')
const comp = component(SameRouteMatches)(el)

expect(comp.$$('p')).to.have.length(1)

router.push('/foo')

await sleep()

expect(comp.$$('p')).to.have.length(1)

router.push('/')

await sleep()

expect(comp.$$('p')).to.have.length(1)

comp.unmount()
})
})
21 changes: 21 additions & 0 deletions test/components/same-route-matches.riot
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<same-route-matches>
<router>
<route path="(.*)">
<p>{state.message}</p>
</route>
</router>

<script>
import { Router, Route } from '../../src/index.js'
export default {
components: {
Router,
Route,
},
state: {
message: 'hello',
}
}
</script>
</same-route-matches>

0 comments on commit 4635aed

Please sign in to comment.