React: Inconsistent back-navigation in incognito mode vs non-incognito mode

My react web application navigates to an external site on clicking a button. On the external site, if I click the back button in the browser, I get redirected back to my react application. Now:

  • in an incognito window, the react application maintains the state that was set before redirecting.
  • in a non-incognito window, the react application “resets” its state to the default state.

Note: I am unable to recreate this inconsistency locally (i.e. via running the react app with npm run start). I have only observed this in a production-ized build in an incognito window.

Minimum Reproducible Example

I have prepared a minimal example that does the following:

  1. Click button on page to get redirected
  2. Clicking the button toggles a state variable to true, which re-renders the page to hide the button and show “redirecting” text instead
  3. After waiting 3 seconds, redirect to external page (example.com) via window.location.href
  4. On external site, click the browser back button
  5. Return to react app

Behaviour in non-incognito window:
Here, the page gets reset and the state variable has its default value of false
Screencast from 2024-05-07 15-19-54.webm

Behaviour in incognito window:
Here, the page maintains the state before navigation, and the state variable maintains value true
Screencast from 2024-05-07 15-22-10.webm

Application code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import { useState } from 'react';
function App() {
const [redirecting, setRedirecting] = useState(false);
return (
<div className='App'>
{redirecting && <p>redirecting...</p>}
{!redirecting && (
<button
onClick={async () => {
setRedirecting(true);
setTimeout(
() => (window.location.href = 'https://example.com'),
3000
);
}}
>
Click to redirect
</button>
)}
</div>
);
}
export default App;
</code>
<code>import { useState } from 'react'; function App() { const [redirecting, setRedirecting] = useState(false); return ( <div className='App'> {redirecting && <p>redirecting...</p>} {!redirecting && ( <button onClick={async () => { setRedirecting(true); setTimeout( () => (window.location.href = 'https://example.com'), 3000 ); }} > Click to redirect </button> )} </div> ); } export default App; </code>
import { useState } from 'react';

function App() {
  const [redirecting, setRedirecting] = useState(false);

  return (
    <div className='App'>
      {redirecting && <p>redirecting...</p>}
      {!redirecting && (
        <button
          onClick={async () => {
            setRedirecting(true);
            setTimeout(
              () => (window.location.href = 'https://example.com'),
              3000
            );
          }}
        >
          Click to redirect
        </button>
      )}
    </div>
  );
}

export default App;

Browsers I have tried recreating this on:

  1. Chrome (inconsistency exists between incognito window and non-incognito window)
  2. Firefox (inconsistency exists between private window and non-private window)
  3. Safari (inconsistency exists between private window and non-private window)

React version: 17.0.2

Steps To Reproduce

As mentioned earlier, I have only been able to recreate the inconsistency in a production-ized build of my react application. I have deployed my minimal reproducible example via Netlify, the URL is: https://master–animated-lily-d6cfcf.netlify.app/

Alternately, you may deploy a version of the application yourself (see links to code example)

  1. Open productionized react app in incognito window
  2. Click button “click to redirect”, this sets the state variable value from V1 to V2
  3. Navigate to external website
  4. Click back button in browser on external website
  5. On return to react app, the state variable will maintain value V2

Link to code example:

Production-ized build: https://master–animated-lily-d6cfcf.netlify.app/

Source code: https://github.com/pranay1208/ext_nav_min

The current behavior

In incognito mode, app maintains the state it had before navigation

In non-incognito mode, app resets to default state

The expected behavior

Consistent behaviour in incognito and non-incognito windows (preferably that incognito window resets state on navigation)

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật