Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 23 additions & 16 deletions tensorboard/webapp/app_routing/effects/app_routing_effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
navigationRequested,
stateRehydratedFromUrl,
} from '../actions';
import {areRoutesEqual} from '../internal_utils';
import {areRoutesEqual, getRouteId} from '../internal_utils';
import {Location} from '../location';
import {ProgrammaticalNavigationModule} from '../programmatical_navigation_module';
import {RouteConfigs} from '../route_config';
Expand Down Expand Up @@ -229,8 +229,26 @@ export class AppRoutingEffects {
withLatestFrom(this.store.select(getActiveRoute)),
filter(([, route]) => Boolean(route)),
map(([navigatedAction, route]) => {
// The URL hash can be set via HashStorageComponent (which uses
// Polymer's tf-storage). DeepLinkProviders also modify the URL when
// a provider's serializeStateToQueryParams() emits. These result in
// the URL updated without the previous hash. HashStorageComponent
// makes no attempt to restore the hash, so it is dropped.

// This results in bad behavior when refreshing (e.g. lost active
// plugin) and when changing dashboards (e.g. lost tagFilter).

// TODO(b/169799696): either AppRouting should manage the URL entirely
// (including hash), or we make the app wait for AppRouting to
// initialize before setting the active plugin hash.
// See https://github.com/tensorflow/tensorboard/issues/4207.
const oldRoute = navigatedAction.before;
const preserveHash =
oldRoute === null ||
getRouteId(oldRoute.routeKind, oldRoute.params) ===
getRouteId(route!.routeKind, route!.params);
return {
isFirstNavigation: navigatedAction.before === null,
preserveHash,
route: route!,
};
}),
Expand All @@ -240,25 +258,14 @@ export class AppRoutingEffects {
queryParams: this.location.getSearch(),
});
}),
tap(({isFirstNavigation, route}) => {
// AppRouting's effect to dispatch the initial navigated action is
// asynchronous. It is possible to see a race condition, where the
// network request for plugins_listing returns first, which sets the
// URL hash to '#scalars', only to be discarded by the replaceState
// below. Preserving the hash upon initial navigation is a workaround.

// TODO(b/169799696): either AppRouting should manage the URL entirely
// (including hash), or we make the app wait for AppRouting to
// initialize before setting the active plugin hash.
// See https://github.com/tensorflow/tensorboard/issues/4207.
const shouldPreserveHash = isFirstNavigation;
tap(({preserveHash, route}) => {
if (route.navigationOptions.replaceState) {
this.location.replaceState(
this.location.getFullPathFromRouteOrNav(route, shouldPreserveHash)
this.location.getFullPathFromRouteOrNav(route, preserveHash)
);
} else {
this.location.pushState(
this.location.getFullPathFromRouteOrNav(route, shouldPreserveHash)
this.location.getFullPathFromRouteOrNav(route, preserveHash)
);
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,45 @@ describe('app_routing_effects', () => {
expect(replaceStateSpy).toHaveBeenCalledWith('/experiments#foo');
});

it('does not preserve hash upon replace for non-initial navigation', () => {
// This hash preservation spec may become obsolete. If we enable app_routing
// to properly set the URL hash, and all TB embedders use app_routing, then
// this spec can be removed.
it('preserves hash upon navigations to the same route id', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in this behavioral spec, maybe specify that this is a temporary behavior until all apps use the router?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

const activeRoute = buildRoute({
routeKind: RouteKind.EXPERIMENT,
pathname: '/experiment',
queryParams: [],
navigationOptions: {
replaceState: true,
},
});
const nextActiveRoute = buildRoute({
routeKind: RouteKind.EXPERIMENT,
pathname: '/experiment',
queryParams: [{key: 'q', value: 'new_value'}],
navigationOptions: {
replaceState: true,
},
});
store.overrideSelector(getActiveRoute, nextActiveRoute);
store.refreshState();
getHashSpy.and.returnValue('#foo');
getPathSpy.and.returnValue('meow');
getSearchSpy.and.returnValue([]);

action.next(
actions.navigated({
before: activeRoute,
after: nextActiveRoute,
})
);

expect(replaceStateSpy).toHaveBeenCalledWith(
'/experiment?q=new_value#foo'
);
});

it('discards hash upon navigations to a new route id', () => {
const activeRoute = buildRoute({
routeKind: RouteKind.EXPERIMENTS,
pathname: '/experiments',
Expand All @@ -663,6 +701,8 @@ describe('app_routing_effects', () => {
const nextActiveRoute = buildRoute({
routeKind: RouteKind.EXPERIMENT,
pathname: '/experiment',
// Changing route params produces a new route id.
params: {experimentId: '123'},
queryParams: [],
navigationOptions: {
replaceState: true,
Expand Down