Skip to content

[Due for payment 2025-10-21] [$250] Track distance - No map preview when switch from offline to online mode #71764

Closed

Description

@lanitochka17

If you haven’t already, check out our contributing guidelines for onboarding and email contributors@expensify.com to request to join our Slack channel!


Version Number: 9.2.22-1
Reproducible in staging?: Y
Reproducible in production?: Y
If this was caught during regression testing, add the test name, ID and link from TestRail: https://test-management.browserstack.com/projects/2219752/test-runs/TR-1121/folder/13176744/41237142/929687227
Email or phone of affected tester (no customers): gocemate+b2154391@gmail.com
Issue reported by: Applause - Internal Team

Action Performed:

  1. Go to FAB> Track distance> Map
  2. Add Start and Stop location
  3. Verify map preview displays the map
  4. Go offline
  5. Go online

Expected Result:

Map should be displayed when user switch back online

Actual Result:

No map preview when switch to offline to online mode

Workaround:

Unknown

Platforms:

Select the officially supported platforms where the issue was reproduced:

  • Android: App
  • Android: mWeb Chrome
  • iOS: App
  • iOS: mWeb Safari
  • iOS: mWeb Chrome
  • Windows: Chrome
  • MacOS: Chrome / Safari
  • MacOS: Desktop
Platforms Tested: On which of our officially supported platforms was this issue tested:
  • Android: App
  • Android: mWeb Chrome
  • iOS: App
  • iOS: mWeb Safari
  • iOS: mWeb Chrome
  • Windows: Chrome
  • MacOS: Chrome / Safari
  • MacOS: Desktop

Screenshots/Videos

Add any screenshot/video evidence
Bug6962728_1759401614113.Recording__6165.mp4

View all open jobs on GitHub

Upwork Automation - Do Not Edit
  • Upwork Job URL: https://www.upwork.com/jobs/~021973915688319704310
  • Upwork Job ID: 1973915688319704310
  • Last Price Increase: 2025-10-03
  • Automatic offers:
    • FitseTLT | Contributor | 109076937
Issue OwnerCurrent Issue Owner: @brianlee-expensify

Activity

  1. added
    BugSomething is broken. Auto assigns a BugZero manager.
    on Oct 2, 2025
  2. melvin-bot commented on Oct 2, 2025

    @melvin-bot

    Triggered auto assignment to @brianlee-expensify (Bug), see https://stackoverflow.com/c/expensify/questions/14418 for more details. Please add this bug to a GH project, as outlined in the SO.

  3. Eskalifer1 commented on Oct 2, 2025

    @Eskalifer1
    Contributor

    Proposal

    Please re-state the problem that we are trying to solve in this issue.

    Track distance - No map preview when switch from offline to online mode

    What is the root cause of that problem?

    The problem lies in lazy import here:

    const MapViewImpl = useMemo(
    () => lazy(() => import('./MapViewImpl.website')),
    // eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps
    [errorResetKey],
    );

    The issue arises because the mapbox-gl library expects a valid DOM element (container) to be present at the time the map instance is created. In our case, after transitioning from offline to online, MapViewImpl is re-mounted via React.lazy in combination with an ErrorBoundary, which re-renders the component when resetKeys change. However, while React does remount the component, it does not guarantee that the DOM elements are already present or that the ref to the container has been set by the time mapbox-gl.Map is called.

    Since the map is initialized synchronously during the first render of the Map component (from the react-map-gl library, which internally calls new mapboxgl.Map()), if the ref is still null or the DOM has not yet been inserted, mapbox-gl throws the error: Invalid type: 'container' must be a String or HTMLElement.

    That is, the reason for the problem is that when we switch from offline to online mode, the component is rendered, but due to the use of a lazy container, it is still null, which causes an error, and even when the container appears, it is no longer re-rendered because we have an error.

    What changes do you think we should make in order to solve the problem?

    We should recreate map when coming from offline:
    Here is solutions:

    1. Add isOffline here:
        const MapViewImpl = useMemo(
            () => lazy(() => import('./MapViewImpl.website')),
            // eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps
            [errorResetKey, isOffline],
        );
    1. change key in map:
     <MapViewImpl
                            ref={ref}
                            key={errorResetKey}
                            // eslint-disable-next-line react/jsx-props-no-spreading
                            {...props}
                        />
    1. Add key to ErrorBoundary:
    key={errorResetKey}

    What specific scenarios should we cover in automated tests to prevent reintroducing this issue in the future?

    N/A

    What alternative solutions did you explore? (Optional)

    Reminder: Please use plain English, be brief and avoid jargon. Feel free to use images, charts or pseudo-code if necessary. Do not post large multi-line diffs or write walls of text. Do not create PRs unless you have been hired for this job.

  4. FitseTLT commented on Oct 2, 2025

    @FitseTLT
    Contributor

    Proposal

    Please re-state the problem that we are trying to solve in this issue.

    Track distance - No map preview when switch from offline to online mode

    What is the root cause of that problem?

    We are wrongly declaring lazy inside the component here

    const MapViewImpl = useMemo(
    () => lazy(() => import('./MapViewImpl.website')),
    // eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps
    [errorResetKey],
    );

    which can cause unexpected state reset just like mentioned in the documentation

    What changes do you think we should make in order to solve the problem?

    lazy should be declared outside component cycle to avoid this kind of issue as recommeneded in the documentation

    const MapViewImpl = lazy(() => import('./MapViewImpl.website'));
    
    

    What specific scenarios should we cover in automated tests to prevent reintroducing this issue in the future?

    N / A

    What alternative solutions did you explore? (Optional)

  5. brianlee-expensify commented on Oct 3, 2025

    @brianlee-expensify
    Contributor

    reproduced on staging.new.expensify.com - user would need to refresh in order for the map to load again

    2025-10-02_17-57-59.mp4
  6. added
    ExternalAdded to denote the issue can be worked on by a contributor
    on Oct 3, 2025
  7. changed the title [-]Track distance - No map preview when switch from offline to online mode[/-] [+][$250] Track distance - No map preview when switch from offline to online mode[/+] on Oct 3, 2025
  8. melvin-bot commented on Oct 3, 2025

    @melvin-bot
  9. added
    Help WantedApply this label when an issue is open to proposals by contributors
    on Oct 3, 2025
  10. melvin-bot commented on Oct 3, 2025

    @melvin-bot

    Triggered auto assignment to Contributor-plus team member for initial proposal review - @ahmedGaber93 (External)

  11. muhammad-ali-abbasi commented on Oct 3, 2025

    @muhammad-ali-abbasi

    Please re-state the problem that we are trying to solve in this issue.

    Track distance - No map preview when switch from offline to online mode

    What is the root cause of that problem?

    The problem lies in how the MapViewImpl lazy component is declared inside the component lifecycle:

    App/src/components/MapView/MapView.website.tsx:

    const MapViewImpl = useMemo(
         () => lazy(() => import('./MapViewImpl.website')),
         // eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps
         [errorResetKey],
     );
    

    Root Cause Analysis:

    1. React.lazy Anti-Pattern: Declaring React.lazy() inside a component with useMemo violates React's recommended
      usage. According to https://react.dev/reference/react/lazy, lazy components should be declared at the module
      level, not inside components.
    2. Component Identity Issues: When useMemo recreates the lazy component (even with the same dependency), React
      treats it as a different component type, causing unmounting/remounting issues that break the map initialization.
    3. Mapbox Container Timing: The mapbox-gl library requires a valid DOM container at initialization. When the lazy
      component is recreated and remounted during offline→online transition, there's a race condition:
      - ErrorBoundary resets when coming online
      - MapViewImpl is recreated by useMemo
      - React attempts to render but the ref container may be null
      - Mapbox throws: Invalid type: 'container' must be a String or HTMLElement
      - Error state prevents re-render even when container becomes available
    4. Missing Network State Dependency: The useMemo dependency array doesn't include isOffline, so when network
      status changes, the component doesn't properly reset to handle the new online state.

    What changes do you think we should make in order to solve the problem?

    Solution: Move lazy declaration to module level and add proper remount keys

    App/src/components/MapView/MapView.website.tsx:

    1. Move React.lazy() outside component (Line 11-12):
    // Before (incorrect - inside component with useMemo):
     function MapView({ref, ...props}: MapViewProps) {
         const MapViewImpl = useMemo(
             () => lazy(() => import('./MapViewImpl.website')),
             [errorResetKey],
         );
         // ...
    

    }

    // After (correct - at module level):
     import React, {lazy, Suspense, useEffect, useState} from 'react';
     // ... other imports
    
    // Declare lazy component outside component cycle as recommended by React documentation
     const MapViewImpl = lazy(() => import('./MapViewImpl.website'));
    
     function MapView({ref, ...props}: MapViewProps) {
         // ... component logic
     }
    
    1. Add key prop to ErrorBoundary:
    <ErrorBoundary
        key={`${errorResetKey}-${isOffline}`}
        resetKeys={[errorResetKey]}
        fallback={/* ... */}
    >
    
    1. Add key prop to MapViewImpl:
    <MapViewImpl
         ref={ref}
         key={errorResetKey}
         // eslint-disable-next-line react/jsx-props-no-spreading
         {...props}
     />
    

    What specific scenarios should we cover in automated tests to prevent reintroducing this issue in the future?
    N/A

    What alternative solutions did you explore?
    N/A

  12. 23 remaining items

  13. melvin-bot commented on Oct 14, 2025

    @melvin-bot

    The solution for this issue has been 🚀 deployed to production 🚀 in version 9.2.30-6 and is now subject to a 7-day regression period 📆. Here is the list of pull requests that resolve this issue:

    If no regressions arise, payment will be issued on 2025-10-21. 🎊

    For reference, here are some details about the assignees on this issue:

  14. melvin-bot commented on Oct 14, 2025

    @melvin-bot

    @FitseTLT / @ahmedGaber93 @brianlee-expensify The PR fixing this issue has been merged! The following checklist (instructions) will need to be completed before the issue can be closed. Please copy/paste the BugZero Checklist from here into a new comment on this GH and complete it. If you have the K2 extension, you can simply click: [this button]

  15. brianlee-expensify commented on Oct 21, 2025

    @brianlee-expensify
    Contributor

    BugZero Checklist:

    • [Contributor] The offending PR has been commented on, pointing out the bug it caused and why, so the author and reviewers can learn from the mistake.

      Link to comment:

    • [Contributor] If the regression was CRITICAL (e.g. interrupts a core flow) A discussion in #expensify-open-source has been started about whether any other steps should be taken (e.g. updating the PR review checklist) in order to catch this type of bug sooner.

      Link to discussion:

    • [Contributor] If it was decided to create a regression test for the bug, please propose the regression test steps using the template below to ensure the same bug will not reach production again.

    Regression Test Proposal Template
    • [BugZero Assignee] Create a GH issue for creating/updating the regression test once above steps have been agreed upon.

      Link to issue:

    Regression Test Proposal

    Precondition:

    Test:

    Do we agree 👍 or 👎

  16. brianlee-expensify commented on Oct 21, 2025

    @brianlee-expensify
    Contributor

    @ahmedGaber93 can you fill out the BugZero checklist above?

  17. brianlee-expensify commented on Oct 21, 2025

    @brianlee-expensify
    Contributor

    Payment Summary

    Contributor: @FitseTLT paid $250 via Upwork
    Contributor+: @ahmedGaber93 due $250 via NewDot

  18. brianlee-expensify commented on Oct 23, 2025

    @brianlee-expensify
    Contributor

    @ahmedGaber93 just wanted to bump here regarding the BZ checklist above!

  19. ahmedGaber93 commented on Oct 25, 2025

    @ahmedGaber93
    Contributor

    BugZero Checklist:

    • [Contributor] The offending PR has been commented on, pointing out the bug it caused and why, so the author and reviewers can learn from the mistake.

      Link to comment: N/A. There’s no clear offending PR — the issue appears to be in the Mapbox library itself.

    • [Contributor] If the regression was CRITICAL (e.g. interrupts a core flow) A discussion in #expensify-open-source has been started about whether any other steps should be taken (e.g. updating the PR review checklist) in order to catch this type of bug sooner.

      Link to discussion: N/A.

    • [Contributor] If it was decided to create a regression test for the bug, please propose the regression test steps using the template below to ensure the same bug will not reach production again.

    • [BugZero Assignee] Create a GH issue for creating/updating the regression test once above steps have been agreed upon.

      Link to regression test: https://github.com/Expensify/Expensify/issues/561469

    Regression Test Proposal

    Precondition:

    • this is to tested only on web, mWeb and desktop

    Test:

    1. Go to FAB> Track distance> Map
    2. Add Start and Stop location
    3. Verify map preview displays the map
    4. Go offline
    5. Go online
    6. Verify Map displays when user switch back online

    Do we agree 👍 or 👎

  20. ahmedGaber93 commented on Oct 25, 2025

    @ahmedGaber93
    Contributor

    @brianlee-expensify Checklist has been completed, sorry for the delay 🙏

  21. trjExpensify commented on Nov 6, 2025

    @trjExpensify
    Contributor

    approved $250 for @ahmedGaber93.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Awaiting PaymentAuto-added when associated PR is deployed to productionBugSomething is broken. Auto assigns a BugZero manager.DailyKSv2ExternalAdded to denote the issue can be worked on by a contributor

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions