Use history.state to decide whether we should goBack() or go to / (fixes #247)

So far, glitch-soc used history.length to decide whether to call `goBack()` or
go to / in order to not leave the webUI. This made clicking the “Back” button
go to the “Getting started” column instead of going back in the browser's
history when such an action would leave the web UI, but also when:
- The WebUI is refreshed (F5)
- A tab is restored
- The history length reaches its maximum (e.g., 50 in Firefox)

This commit fixes these shortcomings by checking `window.history.state`.
Indeed, we only want to go back in the browser's history when the current
location has been reached from within the WebUI, which only happens via
`pushState` as far as I know. Since browser store the serialized state in
the browser history, this also survives page reload and session restoration.
lolsob-rspec
Thibaut Girka 2018-05-23 14:17:05 +02:00
parent fa980f2c95
commit 37dc96b71b
5 changed files with 13 additions and 17 deletions

View File

@ -10,10 +10,10 @@ export default class ColumnBackButton extends React.PureComponent {
handleClick = () => { handleClick = () => {
// if history is exhausted, or we would leave mastodon, just go to root. // if history is exhausted, or we would leave mastodon, just go to root.
if (window.history && (window.history.length === 1 || window.history.length === window._mastoInitialHistoryLen)) { if (window.history.state) {
this.context.router.history.push('/');
} else {
this.context.router.history.goBack(); this.context.router.history.goBack();
} else {
this.context.router.history.push('/');
} }
} }

View File

@ -10,10 +10,10 @@ export default class ColumnBackButtonSlim extends React.PureComponent {
handleClick = () => { handleClick = () => {
// if history is exhausted, or we would leave mastodon, just go to root. // if history is exhausted, or we would leave mastodon, just go to root.
if (window.history && (window.history.length === 1 || window.history.length === window._mastoInitialHistoryLen)) { if (window.history.state) {
this.context.router.history.push('/');
} else {
this.context.router.history.goBack(); this.context.router.history.goBack();
} else {
this.context.router.history.push('/');
} }
} }

View File

@ -66,10 +66,10 @@ export default class ColumnHeader extends React.PureComponent {
handleBackClick = () => { handleBackClick = () => {
// if history is exhausted, or we would leave mastodon, just go to root. // if history is exhausted, or we would leave mastodon, just go to root.
if (window.history && (window.history.length === 1 || window.history.length === window._mastoInitialHistoryLen)) { if (window.history.state) {
this.context.router.history.push('/');
} else {
this.context.router.history.goBack(); this.context.router.history.goBack();
} else {
this.context.router.history.push('/');
} }
} }

View File

@ -302,10 +302,11 @@ export default class UI extends React.Component {
} }
handleHotkeyBack = () => { handleHotkeyBack = () => {
if (window.history && window.history.length === 1) { // if history is exhausted, or we would leave mastodon, just go to root.
this.props.router.history.push('/'); if (window.history.state) {
this.context.router.history.goBack();
} else { } else {
this.props.router.history.goBack(); this.context.router.history.push('/');
} }
} }

View File

@ -28,11 +28,6 @@ function main() {
store.dispatch(registerPushNotifications.register()); store.dispatch(registerPushNotifications.register());
} }
perf.stop('main()'); perf.stop('main()');
// remember the initial URL
if (window.history && typeof window._mastoInitialHistoryLen === 'undefined') {
window._mastoInitialHistoryLen = window.history.length;
}
}); });
} }