Skip to content
This repository has been archived by the owner on Oct 1, 2024. It is now read-only.

Support for async loading in Remix/Esbuild #2546

Closed
wants to merge 4 commits into from

Conversation

ryanwilsonperkin
Copy link
Member

Description

Works together with #2538

When attempting to lazy load React components and GraphQL documents using Remix/Esbuild, we've found that the load callback from the async resolver will often be fired while the useMountedRef is returning false. This might indicate an edge case in useMountedRef, potentially because of the use of ESM in the browser, or rendering under Strict Mode.

This meant that the callback would be triggered and would resolve the async value, but because the mounted ref was set to false, we would not call the setState and so would never show the loaded value. By removing the mounted check, we get the value setting correctly.

We also believe that this is the right path forward regardless of this loading bug, because of a change introduced in React 18 to no longer warn in this scenario. Setting state when the component was unmounted would previously emit a warning, with the intent to get developers to cancel their async actions as a cleanup state when unmounting a component. Unfortunately, many async actions don't have an easy option for cancelling so it led many devs to create variants of isMounted

1. React no longer recommends this pattern and has removed the warning
   about "set state after unmounted may indicate a memory leak" because
   the intention was to help developers find places to cancel
   asynchronous actions but many can't be cancelled so this just leads
   to noise in the code.

2. This led to race conditions where a load() call is made while mounted
   is still false and the loaded result is never set in the value. The
   callback is never invoked a second time, so the value never gets set
   despite the load completing.
1. React no longer recommends this pattern and has removed the warning
about "set state after unmounted may indicate a memory leak" because
the intention was to help developers find places to cancel
asynchronous actions but many can't be cancelled so this just leads
to noise in the code.

2. This led to race conditions where a load() call is made while mounted
is still false and the loaded result is never set in the value. The
callback is never invoked a second time, so the value never gets set
despite the load completing.
@ryanwilsonperkin
Copy link
Member Author

/snapit

@github-actions
Copy link
Contributor

github-actions bot commented Feb 3, 2023

🫰✨ Thanks @ryanwilsonperkin! Your snapshots have been published to npm.

Test the snapshots by updating your package.json with the newly published versions:

yarn add @shopify/[email protected]
yarn add @shopify/[email protected]
yarn add @shopify/[email protected]
yarn add @shopify/[email protected]
yarn add @shopify/[email protected]
yarn add @shopify/[email protected]
yarn add @shopify/[email protected]
yarn add @shopify/[email protected]

@znja znja closed this Oct 1, 2024
@shopify-shipitnext
Copy link

🔎 View this PR in Shipit Next.

ℹ️ Expand to learn how to deploy and handle emergencies using Shipit Next

Overview

Shipit Next will merge your code on your behalf because this repository uses Shipit Next and its merge queue.

To ship this PR, you can either:

Comment Commands

  • /shipit: Enqueue this PR into the merge queue where it will eventually be merged and deployed.
  • /cancel: Eject this PR from the merge queue and rebuild PRs that were enqueued after this PR.
  • /shipit --jump-queue: Enqueue this PR at the top of the merge queue where it will be included in the next deploy. Use this for non-emergency situations.
    - Emergency handling procedure for this command can be found here.
  • /shipit --emergency: Merge this PR directly into main and deploy to all environments once all require_for_emergency CI checks pass. Please be aware that changes deployed with this command will not be automatically rolled back.

Commands exclusive to Deploy Before Merge

  • /cancel --emergency: Eject this PR from the merge and rollback any deployments containing this PR.

Documentation

Questions or feedback?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants