Skip to content

Commit 2348641

Browse files
authored
Avoid creating new tabs on header state (#3371)
Solves #2825, an old bug where new tabs were created on every refresh the bug occurred when: 1. `shouldPersistHeaders` is not set to true 2. `headers` or `defaultHeaders` are provided as props 3. the user refreshes the browser
1 parent 7856b25 commit 2348641

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

.changeset/sour-maps-ring.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
'@graphiql/react': patch
3+
'graphiql': patch
4+
---
5+
6+
Solves #2825, an old bug where new tabs were created on every refresh
7+
8+
the bug occurred when:
9+
10+
1. `shouldPersistHeaders` is not set to true
11+
2. `headers` or `defaultHeaders` are provided as props
12+
3. the user refreshes the browser

packages/graphiql-react/src/editor/context.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ export function EditorContextProvider(props: EditorContextProviderProps) {
311311
defaultQuery: props.defaultQuery || DEFAULT_QUERY,
312312
defaultHeaders: props.defaultHeaders,
313313
storage,
314+
shouldPersistHeaders,
314315
});
315316
storeTabs(tabState);
316317

packages/graphiql-react/src/editor/tabs.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export function getDefaultTabState({
7272
query,
7373
variables,
7474
storage,
75+
shouldPersistHeaders,
7576
}: {
7677
defaultQuery: string;
7778
defaultHeaders?: string;
@@ -80,15 +81,23 @@ export function getDefaultTabState({
8081
query: string | null;
8182
variables: string | null;
8283
storage: StorageAPI | null;
84+
shouldPersistHeaders?: boolean;
8385
}) {
8486
const storedState = storage?.get(STORAGE_KEY);
8587
try {
8688
if (!storedState) {
8789
throw new Error('Storage for tabs is empty');
8890
}
8991
const parsed = JSON.parse(storedState);
92+
// if headers are not persisted, do not derive the hash using default headers state
93+
// or else you will get new tabs on every refresh
94+
const headersForHash = shouldPersistHeaders ? headers : undefined;
9095
if (isTabsState(parsed)) {
91-
const expectedHash = hashFromTabContents({ query, variables, headers });
96+
const expectedHash = hashFromTabContents({
97+
query,
98+
variables,
99+
headers: headersForHash,
100+
});
92101
let matchingTabIndex = -1;
93102

94103
for (let index = 0; index < parsed.tabs.length; index++) {

0 commit comments

Comments
 (0)