Backbone Marionette 应用程序和后退按钮上的复杂 url 历史记录

Complex url history on a Backbone Marionette application and back button

有 2 个 Web 应用程序,我们的和另一个团队的。另一个团队的网络应用程序是 http://otherteam.com and our application is http://myteam.com

http://otherteam.com webpage, they have an href link pointing to our page which is http://myteam.com/config?lang=en. When our web application(actually Marionette AppRouter's task) receives that kind of route or path, it will parse it and set the language configuration and then we have a code to redirect the user to the final webpage which is http://myteam.com/landingpage

我们使用的代码是

Backbone.history.navigate('landingpage', {replace: true});

将用户重定向到最终目的地。

不幸的是,当用户单击浏览器的后退按钮时,它不会返回到 http://otherteam.com. It will go back to http://myteam.com/config?lang=en which is still our own application. What happens is that the Marionette app router will parse it again similar to how I described it above. The user will just be brought back to http://myteam.com/landingpage

所以我改变了

// Backbone.history.navigate('landingpage', {replace: true})

现在我正在使用

history.replaceState({}, '', 'landingpage');

当我单击“后退”按钮时,顶部栏上的 url 变成了 http://myteam.com/config?lang=en, but it doesn't reload our landingpage anymore which is good. However, nothing happens to the page until I click Back button again. After making the second click on the Back button, I'm back to http://otherteam.com,这很好,但我不得不单击“后退”按钮两次。

问题是您正在将用户从 /config?lang=en 重定向到另一个页面,因此当您单击后退时,他们会登陆上一个页面,然后再次被重定向回登陆页面。

这里最简单的解决方案是 otherteam.com 到 link 到您的目标网页,然后传递要在那里使用的参数 - http://myteam.com/landingpage?lang=en。多次重定向用户从来都不是一个好主意,而且几乎总是不必要的。

收到 lang 配置参数后,最好将其保存到本地存储,以便在整个应用程序的任何需要时都可以检索它。