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

[graphiql] Refreshing page can erroneously create new tabs #2825

Open
1 task done
simhnna opened this issue Oct 20, 2022 · 18 comments
Open
1 task done

[graphiql] Refreshing page can erroneously create new tabs #2825

simhnna opened this issue Oct 20, 2022 · 18 comments

Comments

@simhnna
Copy link
Contributor

simhnna commented Oct 20, 2022

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

If you set initial headers and disable shouldPersistHeaders graphiql creates a new tab although it shouldn't

Tabs aren't stored with headers, but the getDefaultTabState function doesn't know about the shouldPersistHeaders flag. So it compares a stored tab without headers with the headers that are passed to graphiql and decides to create a new tab

Expected Behavior

Refreshing the page shouldn't create a new tab

Steps To Reproduce

In this repo edit the renderExample.js setting the

var parameters = {
  headers: '{"foo": "bar"}' 
};

and shouldPersistHeaders: false,



### Module pattern

- [ ] graphiql-umd
- [ ] graphiql-esm
- [ ] graphiql-commonjs

### Environment

_No response_

### Anything else?

_No response_
@simhnna
Copy link
Contributor Author

simhnna commented Oct 20, 2022

I would propose passing shouldPersistHeaders to getDefaultTabState and using that to decide if the headers should be considered while computing the hash.

If that's accepted I'll prepare a merge request

@simhnna
Copy link
Contributor Author

simhnna commented Oct 20, 2022

Oh I just found #2145 adding that would probably also fix my issue

@acao acao moved this to Todo in GraphiQL Roadmap Nov 13, 2022
@thomasheyenbrock
Copy link
Collaborator

I would propose passing shouldPersistHeaders to getDefaultTabState and using that to decide if the headers should be considered while computing the hash.

Fixing the behavior of the headers props like you described here would still be much appreciated! Do you want to open a PR for that?

@acao
Copy link
Member

acao commented Jun 29, 2023

Here is a simple reproduction of this bug:
https://codesandbox.io/s/graphiql-bug-2825-kxx2j7?file=/index.html

it only requires setting a headers value, and refreshing the page

@acao
Copy link
Member

acao commented Jul 24, 2023

a clue - this bug goes away when shouldPersistHeaders is enabled.

it seems that the tab state context on init mismatches the previous state, and assumes the query parameters or user input of headers are indicating a new tab state

update: i have a "fix", however I can't get the e2e suite to re-create this bug!

acao added a commit that referenced this issue Jul 24, 2023
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
acao added a commit that referenced this issue Jul 24, 2023
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
@acao
Copy link
Member

acao commented Jul 24, 2023

if any users want to confirm the fix, try this canary release:

  • npm: graphiql@3.0.5-canary-f6e35dd1.0
  • unpkg: https://unpkg.com/graphiql@3.0.5-canary-f6e35dd1.0/graphiql.js

@acao
Copy link
Member

acao commented Jul 27, 2023

@markedwards can you try these patch versions and confirm the fix works for you, at least for headers creating new tabs (I will address persisting user defined settings next)? I have no one to review my PRs right now haha

acao added a commit that referenced this issue Jul 29, 2023
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
@acao acao mentioned this issue Jul 29, 2023
@markedwards
Copy link

markedwards commented Aug 1, 2023

Sorry, haven't got to it yet. I'll try to test today or tomorrow.

It seems to be fixed on https://deploy-preview-3377--graphiql-test.netlify.app

@markedwards
Copy link

@acao Does not seem to be fixed when using https://unpkg.com/graphiql@3.0.5-canary-f6e35dd1.0/graphiql.js.

@acao
Copy link
Member

acao commented Aug 1, 2023

@markedwards the fix has already been released, can you try it with the latest? I was able to reproduce the bug and eliminate the bug with this release

@markedwards
Copy link

markedwards commented Aug 1, 2023

Okay, well I can no longer reproduce multiple <untitled> tabs spawning (one per refresh). However, I still get one <untitled> tab on refresh. My expectation is, if I already have tabs, the most recent one should be shown on refresh instead of spawning an empty one.

Oddly, I no longer reproduce the multiple tab issue, even on https://unpkg.com/graphiql@3.0.0/graphiql.min.js. Did the behavior of 3.0.0 change?

@acao
Copy link
Member

acao commented Aug 1, 2023

hmm, it seems the issue wasn't completely resolved then. 3.0.0 was released a while ago so the behavior shouldn't have changed. it would've been the last graphiql patch version that resolves the issue, 3.0.5

@acao
Copy link
Member

acao commented Aug 1, 2023

@markedwards what happens when you try deleting all the tabs and history, and then try to reproduce the bug? or better yet, if you clear local storage state and try reproducing it? lingering local storage states may cause this issue to seem like it's still an issue. I cannot seem to get a tab to appear on refresh anymore

@markedwards
Copy link

Wiping local storage doesn't seem to have any effect, the behavior is exactly the same. I see no difference between 3.0.0 and 3.0.5, they both spawn empty tabs on refresh. I can sometimes get more than one to spawn on both versions actually, but I'm not exactly sure when this happens. I think it has something to do with persistent headers.

@acao
Copy link
Member

acao commented Aug 2, 2023

@markedwards this example clears it up:

No new tabs are created when shouldPersistHeaders is set to true:

https://codesandbox.io/s/graphiql-bug-2825-kxx2j7?file=/index.html

But when you disable this value, and refresh, you get new headers. So the issue is not fixed.

My bugfix works for the first case, but for the second case, causes a mismatch, because headers are set statically, but not persisted.

@acao
Copy link
Member

acao commented Aug 2, 2023

I think the best option is to remove shouldPersistHeaders as an option and always make it true, and remove the user setting.

also this issue is not apparent when you use defaultHeaders instead of headers. headers is meant for programatically setting the current headers state, whereas defaultHeaders works like defaultQuery, and is ignored when you already have values set.

if you want to set default headers for every query, and make them a transparent choice, set the headers in your fetcher statically

@markedwards
Copy link

I’m reproducing this without any settings being overridden at all. I’m not setting headers, or changing any default setting.

@fragm3
Copy link

fragm3 commented Sep 20, 2023

Even I am running into the same issue without any settings being changed.

ramnivas added a commit to exograph/exograph that referenced this issue Jan 15, 2024
GraphiQL interface resets the value of "shouldPersistHeaders" to `false` upon refreshing the browser (see
graphql/graphiql#3369). As a result, on a second refresh, the header value is lost.

The fix reads the value of the `localStorage` key `graphiql:shouldPersistHeaders` and passes that
value to the `shouldPersistHeaders` prop of the GraphiQL component.

This change has an unwanted side-effect of creating new tabs if:
- The user has provided a header value
- The user refreshes the browser

See graphql/graphiql#2825 for more details. However, we don't observe the
"new tabs" behavior when the UI refreshes automatically on "exo yolo" or "exo dev", so this is still
an overall improvement for the typical user of Exograph.

When the mentioned issues are fixed, we should be able to revert this change.
ramnivas added a commit to exograph/exograph that referenced this issue Jan 16, 2024
GraphiQL interface resets the value of "shouldPersistHeaders" to `false` upon refreshing the browser (see
graphql/graphiql#3369). As a result, on a second refresh, the header value is lost.

The fix reads the value of the `localStorage` key `graphiql:shouldPersistHeaders` and passes that
value to the `shouldPersistHeaders` prop of the GraphiQL component.

This change has an unwanted side-effect of creating new tabs if:
- The user has provided a header value
- The user refreshes the browser

See graphql/graphiql#2825 for more details. However, we don't observe the
"new tabs" behavior when the UI refreshes automatically on "exo yolo" or "exo dev", so this is still
an overall improvement for the typical user of Exograph.

When the mentioned issues are fixed, we should be able to revert this change.

Fixes #997 997
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Todo
Development

No branches or pull requests

5 participants