Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid creating new tabs on header state #3371

Merged
merged 1 commit into from
Jul 29, 2023
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
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