Skip to content

Commit

Permalink
fix(history-service): do not ignore consumer history changes on root …
Browse files Browse the repository at this point in the history
…location push (#720)
  • Loading branch information
unstubbable committed Sep 27, 2022
1 parent 0f16746 commit e14568c
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/history-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@feature-hub/core": "^3.0.0",
"@feature-hub/logger": "^3.0.0",
"@feature-hub/server-request": "^3.0.0",
"fast-deep-equal": "^3.1.3",
"resolve-pathname": "^3.0.0"
},
"devDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions packages/history-service/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1359,7 +1359,7 @@ describe('defineHistoryService', () => {
search: '',
hash: '',
state: undefined,
key: 'default',
key: expect.any(String),
},
'PUSH',
],
Expand Down Expand Up @@ -2337,7 +2337,7 @@ describe('defineHistoryService', () => {
search: '',
hash: '',
state: undefined,
key: 'default',
key: expect.any(String),
},
action: 'PUSH',
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import equal from 'fast-deep-equal';
import * as history from 'history';
import {ConsumerHistory} from './consumer-history';
import {HistoryMultiplexer} from './history-multiplexer';
Expand Down Expand Up @@ -64,7 +65,7 @@ export class BrowserConsumerHistory extends ConsumerHistory {
this.historyKey
);

if (this.location.key === location.key) {
if (this.matches(location)) {
return;
}

Expand All @@ -73,4 +74,12 @@ export class BrowserConsumerHistory extends ConsumerHistory {

this.notifyListeners();
}

private matches(location: history.Location): boolean {
if (history.createPath(location) !== history.createPath(this.location)) {
return false;
}

return equal(location.state, this.location.state);
}
}
3 changes: 2 additions & 1 deletion packages/history-service/src/internal/consumer-history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ export abstract class ConsumerHistory implements history.History {
search,
hash,
state,
key,
} = historyMultiplexer.getConsumerLocation(historyKey);

this.location = {
pathname: pathname.startsWith('/') ? pathname : `/${pathname}`,
search,
hash,
state,
key: 'default',
key,
};

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/history-service/src/internal/create-key.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// source: https://github.com/remix-run/history/blob/485ebc1/packages/history/index.ts#L1043
export function createKey(): string {
return Math.random().toString(36).substr(2, 8);
return Math.random().toString(36).slice(2, 8);
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export class HistoryMultiplexer {

const {state, key} = this.rootLocation.state?.[historyKey] || {
state: undefined,
key: 'default',
key: createKey(),
};

return {
Expand Down

0 comments on commit e14568c

Please sign in to comment.