Skip to content

Commit

Permalink
Avoid creating new tabs on header state (#3371)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
acao committed Jul 29, 2023
1 parent 7856b25 commit 2348641
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
12 changes: 12 additions & 0 deletions .changeset/sour-maps-ring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
'@graphiql/react': patch
'graphiql': patch
---

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 change: 1 addition & 0 deletions packages/graphiql-react/src/editor/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ export function EditorContextProvider(props: EditorContextProviderProps) {
defaultQuery: props.defaultQuery || DEFAULT_QUERY,
defaultHeaders: props.defaultHeaders,
storage,
shouldPersistHeaders,
});
storeTabs(tabState);

Expand Down
11 changes: 10 additions & 1 deletion packages/graphiql-react/src/editor/tabs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export function getDefaultTabState({
query,
variables,
storage,
shouldPersistHeaders,
}: {
defaultQuery: string;
defaultHeaders?: string;
Expand All @@ -80,15 +81,23 @@ export function getDefaultTabState({
query: string | null;
variables: string | null;
storage: StorageAPI | null;
shouldPersistHeaders?: boolean;
}) {
const storedState = storage?.get(STORAGE_KEY);
try {
if (!storedState) {
throw new Error('Storage for tabs is empty');
}
const parsed = JSON.parse(storedState);
// if headers are not persisted, do not derive the hash using default headers state
// or else you will get new tabs on every refresh
const headersForHash = shouldPersistHeaders ? headers : undefined;
if (isTabsState(parsed)) {
const expectedHash = hashFromTabContents({ query, variables, headers });
const expectedHash = hashFromTabContents({
query,
variables,
headers: headersForHash,
});
let matchingTabIndex = -1;

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

0 comments on commit 2348641

Please sign in to comment.