allow Grist front-end to function when location history is unavailable (#596)

* allow Grist front-end to function when location history is unavailable

When the Grist front-end is embedded in an iframe, using a srcdoc
attribute, history.pushState and similar methods are unavailable.
Currently, that makes it impossible to navigate between Grist pages,
since an access error is thrown (behavior may be browser dependent).
With this change, navigation succeeds.

* give unrelated possibly slow test a little more time
This commit is contained in:
Paul Fitzpatrick 2023-07-27 12:02:17 +01:00 committed by GitHub
parent ab6807c342
commit f0b9e1f7e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 7 deletions

View File

@ -77,6 +77,7 @@ export class UrlState<IUrlState extends object> extends Disposable {
if (samePage) { if (samePage) {
await this._stateImpl.delayPushUrl(prevState, newState); await this._stateImpl.delayPushUrl(prevState, newState);
try {
if (options.replace) { if (options.replace) {
this._window.history.replaceState(null, '', newUrl); this._window.history.replaceState(null, '', newUrl);
} else { } else {
@ -84,6 +85,15 @@ export class UrlState<IUrlState extends object> extends Disposable {
} }
// pushState/replaceState above do not trigger 'popstate' event, so we call loadState() manually. // pushState/replaceState above do not trigger 'popstate' event, so we call loadState() manually.
this.loadState(); this.loadState();
} catch (e) {
// If we fail, we may be in a context where Grist doesn't have
// control over history, e.g. an iframe with srcdoc. Go ahead
// and apply the application state change (e.g. switching to a
// different Grist page). The back button won't work, but what
// it should do in an embedded context is full of nuance anyway.
log.debug(`pushUrl failure: ${e}`);
this.state.set(this._stateImpl.decodeUrl(new URL(newUrl)));
}
} else { } else {
this._window._urlStateLoadPage!(newUrl); this._window._urlStateLoadPage!(newUrl);
} }

Binary file not shown.

View File

@ -4,7 +4,7 @@ import * as gu from 'test/nbrowser/gristUtils';
import {server, setupTestSuite} from 'test/nbrowser/testUtils'; import {server, setupTestSuite} from 'test/nbrowser/testUtils';
describe('SelectByRefList', function() { describe('SelectByRefList', function() {
this.timeout(60000); this.timeout(80000);
setupTestSuite(); setupTestSuite();
addToRepl('gu2', gu); addToRepl('gu2', gu);
gu.bigScreen(); gu.bigScreen();