From b468dc076341addd2a56c53aadfd6cfc4e8e7186 Mon Sep 17 00:00:00 2001 From: thecodrr Date: Mon, 20 Apr 2020 12:39:39 +0500 Subject: [PATCH] navigation: persist history as well previously we were only persisting the last route but this caused the back button to disappear when the page was refreshed. Hence, I have added history persistence as well. This can probably be optimized a little bit more. Known Issue: JSON.stringify doesn't preserve the route component so we have to retrieve it manually using `this.getRoute(key).component`. --- apps/web/src/navigation/index.js | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/apps/web/src/navigation/index.js b/apps/web/src/navigation/index.js index 0be43a826..8607d5637 100644 --- a/apps/web/src/navigation/index.js +++ b/apps/web/src/navigation/index.js @@ -16,8 +16,12 @@ class Navigator { } onLoad = () => { - const route = Config.get(this.root, this.getRoute(this.options.default)); - this.navigate(route.key, route.params, true); + const opts = Config.get(this.root, { + history: [], + lastRoute: this.getRoute(this.options.default), + }); + this.history = opts.history; + this.navigate(opts.lastRoute.key, opts.lastRoute.params, true); }; getRoute(key) { @@ -30,7 +34,10 @@ class Navigator { // NOTE: we delete the navigator key if any so it's always new across refreshes const copy = { ...route, params: { ...route.params } }; if (copy.params.navigator) delete copy.params.navigator; - Config.set(this.root, copy); + Config.set(this.root, { + history: this.history, + lastRoute: copy, + }); } getRoot() { @@ -89,9 +96,9 @@ class Navigator { goBack(params = {}) { let route = this.history.pop(); - if (!route) { - return false; - } + if (!route) return false; + if (!route.component) route.component = this.getRoute(route.key).component; + this.setLastRoute(route); return this.renderRoute(this._mergeParams(route, params)); }