Skip to content
Source
Sign inSign up
TanStack/query

fix(query-core): replace scheduleMicrotask with queueMicrotask #6592

ClosedNathanHuisman(GitHub profile, opens in a new tab)wants to merge 2 commits intomain fromrefs/pull/6592/headopened View on GitHub

scheduleMicrotask was implemented incorrectly with setTimeout, causing the passed callback to run in the next task instead of a microtask. This sometimes resulted in a layout shift in my solid project. Fix this by replacing scheduleMicrotask with queueMicrotask, which is supported by all browsers query supports.12

Footnotes

  1. https://tanstack.com/query/v5/docs/react/installation#requirements ↩

  2. https://developer.mozilla.org/en-US/docs/Web/API/queueMicrotask#browser_compatibility ↩

    1. The latest updates on your projects. Learn more about Vercel for Git ↗︎

      1 Ignored Deployment
      NameStatusPreviewCommentsUpdated (UTC)
      query⬜️ Ignored (Inspect)Visit PreviewDec 26, 2023 3:46pm
    2. This pull request is automatically built and testable in CodeSandbox.

      To see build info of the built libraries, click here or the icon next to each commit SHA.

      Latest deployment of this branch, based on commit e78f591e2bda3e3b5b122f58088260a11491f229:

      SandboxSource
      @tanstack/query-example-angular-basicConfiguration
      @tanstack/query-example-react-basic-typescriptConfiguration
      @tanstack/query-example-solid-basic-typescriptConfiguration
      @tanstack/query-example-svelte-basicConfiguration
      @tanstack/query-example-vue-basicConfiguration
    3. ☁️ Nx Cloud Report

      CI is running/has finished running commands for commit e78f591e2bda3e3b5b122f58088260a11491f229. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

      📂 See all runs for this CI Pipeline Execution


      🟥 Failed Commands
      nx affected --targets=test:eslint,test:lib,test:types,test:build,test:bundle

      Sent with 💌 from NxCloud.

    4. There are different tradeoffs here. In react, this will likely result in more re-renders because fewer things can be batched together. This is likely shown in the failing tests. I think to not make this breaking, we could offer a batching strategy as an option somewhere, like timeout (default, current version), queueMicroTask and maybe even requestAnimationFrame that the user can choose from. I think redux toolkit has such an option: https://redux-toolkit.js.org/api/autoBatchEnhancer#autobatchenhancer-1

    5. Hmm, I did not consider that, requestAnimationFrame could also be interesting. Maybe a setter on the notify manager like this:

      function setBatchType(config: { type: 'raf' | 'tick' } | { type: 'timer', timeout?: number}) { /* ... */ }

      But then I'm not really sure how to expose this to the api surface in solid-query, react-query, etc.

      Edit: I guess since they're re-exporting query-core that's not necessary

    6. That looks really good. I think we'd need some docs and tests, too please

    7. I'm closing this pull request in favor of opening a new one with the new changes I made

Sign in to join this conversation.