Skip to content

Commit

Permalink
fix: back & forward didn't track history correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobrosenberg committed Mar 25, 2024
1 parent 8bcea2e commit c40b0ed
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
5 changes: 5 additions & 0 deletions lib/runtime/Route/Route.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ export class Route {
redirect: null,
}

/** @type {Route} */
prevRoute
/** @type {Route} */
nextRoute

/**
* @param {Router} router
* @param {string} url
Expand Down
37 changes: 27 additions & 10 deletions lib/runtime/Router/Router.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,12 +316,12 @@ export class Router {
// fix null state
state = state || {}

const oldRoute = this.history.get(state.id)
const stackedRoute = this.history.get(state.id)

if (oldRoute?.meta.history) {
oldRoute.mode = 'popState'
oldRoute.state.cached = true
this.setActiveRoute(oldRoute)
if (stackedRoute?.meta.history) {
stackedRoute.mode = 'popState'
stackedRoute.state.cached = true
this.setActiveRoute(stackedRoute, null)
return true
}

Expand Down Expand Up @@ -356,7 +356,7 @@ export class Router {
// pending route could have changed while awaiting route.loadRoute() above
if (this.pendingRoute.get() === route) this.pendingRoute.set(null)

if (didLoadRoute) this.setActiveRoute(route)
if (didLoadRoute) this.setActiveRoute(route, stackedRoute)

// TODO Wait a tick for component rendering. There's probably be a better way to handle this.
await new Promise(resolve => setTimeout(resolve))
Expand All @@ -365,7 +365,12 @@ export class Router {
}
}

setActiveRoute(route) {
/**
*
* @param {Route} route
* @param {Route} stackedRoute the route that was stacked in history with the same id
*/
setActiveRoute(route, stackedRoute) {
// the route made it through all pretasks, lets set it to active
this.log.debug('set active route', this) // ROUTIFY-DEV-ONLY

Expand All @@ -375,6 +380,14 @@ export class Router {

// lastRoute can't be the last entry in history, since we could be navigating back
this.lastRoute = this.activeRoute.get()

if (route.mode !== 'popState' && this.lastRoute) {
this.lastRoute.nextRoute = route
route.prevRoute = this.lastRoute
} else if (stackedRoute) {
route.nextRoute = stackedRoute.nextRoute
route.prevRoute = stackedRoute.prevRoute
}
this.activeRoute.set(route)

this.afterUrlChange.run({
Expand Down Expand Up @@ -423,9 +436,13 @@ export class Router {
this.go(1)
}
go(count) {
const history = [...this.history.values()]
const index = history.findIndex(r => r === this.activeRoute.get())
const newRoute = history[index + count]
const prop = count > 0 ? 'nextRoute' : 'prevRoute'
let newRoute = this.activeRoute.get()
while (count) {
newRoute = newRoute[prop]
count += count > 0 ? -1 : 1
}
if (!newRoute) return
this._setUrl(newRoute.url, 'popState', false, newRoute.state)
}
}
Expand Down

0 comments on commit c40b0ed

Please sign in to comment.