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

feat: skip pinging the server when the tab is not shown #12698

Merged

Conversation

sapphi-red
Copy link
Member

@sapphi-red sapphi-red commented Apr 2, 2023

Description

This PR makes @vite/client to skip pinging the server when the tab is not shown.
By doing this, we can reduce non-necessary pings (#5228) and thus reduce the logs output in browser console (#12693).

close #5228
close #12693
similar to #6791

Additional context


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the PR Title Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@sapphi-red sapphi-red added the p2-nice-to-have Not breaking anything but nice to have (priority) label Apr 2, 2023
@stackblitz
Copy link

stackblitz bot commented Apr 2, 2023

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

bluwy
bluwy previously approved these changes Apr 3, 2023
Copy link
Member

@bluwy bluwy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I like this change. Looks like we still support safari14, which visibilitychange event is only supported since 14.1, but the worst case is that the visibilitychange wait promise never resolves, and I don't think it's a big problem.

@sapphi-red
Copy link
Member Author

Oh, I didn't notice that. It seems Safari 14.0 supports visibilitychange event partially. Safari 14.0 doesn't fire the event when navigating away. But that's fine for this usecase.
https://developer.mozilla.org/en-US/docs/Web/API/Document/visibilitychange_event#browser_compatibility

}
await wait(ms)
} else {
await Promise.race([wait(ms), waitForWindowShow()])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will register a new event handler for visibilitychange every second while the visibility doesn't change (then resolve all of them at the same time. I think it would be good to create a single waitForWindowShow promise and use it here until it resolves.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! I fixed this 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't notice this too, I thought it would wait for window show altogether before continuing the while loop, maybe we could so a simple await waitForWindowShow() here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, you're right. I don't know why I wrote it like this 😅

@sapphi-red
Copy link
Member Author

/ecosystem-ci run

@vite-ecosystem-ci
Copy link

vite-ecosystem-ci bot commented Apr 3, 2023

📝 Ran ecosystem CI: Open

suite result
astro ✅ success
histoire ❌ failure
iles ✅ success
ladle ✅ success
laravel ✅ success
marko ✅ success
nuxt ❌ failure
previewjs ❌ failure
qwik ✅ success
rakkas ✅ success
sveltekit ✅ success
vite-plugin-ssr ❌ failure
vite-plugin-react ✅ success
vite-plugin-react-pages ✅ success
vite-plugin-react-swc ✅ success
vite-plugin-svelte ❌ failure
vite-plugin-vue ✅ success
vite-setup-catalogue ✅ success
vitepress ✅ success
vitest ✅ success
windicss ✅ success

@patak-dev patak-dev merged commit bedcd8f into vitejs:main Apr 3, 2023
@sapphi-red sapphi-red deleted the feat/skip-ping-when-tab-is-not-shown branch April 3, 2023 15:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p2-nice-to-have Not breaking anything but nice to have (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Exponential Backoff for Failed Pings Vite tries to reconnect to websocket every second
4 participants