Skip to content

Commit 0bd9ef6

Browse files
authored
Merge pull request Expensify#89149 from TaduJR/fix-Web-Document-title-flickers-when-navigating-away-from-expense-report
fix: Web - Document title flickers when navigating away from expense report
2 parents f0a5414 + 0ef83cd commit 0bd9ef6

1 file changed

Lines changed: 15 additions & 17 deletions

File tree

  • src/libs/UnreadIndicatorUpdater/updateUnread

src/libs/UnreadIndicatorUpdater/updateUnread/index.ts

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,32 +26,30 @@ Onyx.connectWithoutView({
2626
*/
2727
function setPageTitle(title: string) {
2828
currentPageTitle = title;
29-
// Immediately update the document title when page title changes
3029
updateDocumentTitle();
3130
}
3231

3332
/**
34-
* Update the actual document title and favicon
33+
* Synchronous on purpose. Deferring (setTimeout/queueMicrotask) loses a race with React Navigation's
34+
* createMemoryHistory popstate handler, which captures and re-asserts document.title — re-applying
35+
* the stale value if our write hasn't landed yet.
3536
*/
3637
function updateDocumentTitle() {
38+
if (typeof document === 'undefined') {
39+
return;
40+
}
3741
const hasUnread = unreadTotalCount !== 0;
38-
// This setTimeout is required because due to how react rendering messes with the DOM, the document title can't be modified synchronously, and we must wait until all JS is done
39-
// running before setting the title.
40-
setTimeout(() => {
41-
// There is a Chrome browser bug that causes the title to revert back to the previous when we are navigating back. Setting the title to an empty string
42-
// seems to improve this issue.
43-
document.title = '';
4442

45-
// Use page-specific title if available, otherwise use the default SITE_TITLE
46-
const baseTitle = currentPageTitle || CONFIG.SITE_TITLE;
47-
const titleWithUnread = hasUnread ? `(${unreadTotalCount}) ${baseTitle}` : baseTitle;
48-
document.title = shouldShowBranchNameInTitle && __GIT_BRANCH__ ? `[${__GIT_BRANCH__}] ${titleWithUnread}` : titleWithUnread;
43+
// Chrome reverts the tab title to the previous entry on back navigation; blanking it first forces a refresh.
44+
document.title = '';
45+
const baseTitle = currentPageTitle || CONFIG.SITE_TITLE;
46+
const titleWithUnread = hasUnread ? `(${unreadTotalCount}) ${baseTitle}` : baseTitle;
47+
document.title = shouldShowBranchNameInTitle && __GIT_BRANCH__ ? `[${__GIT_BRANCH__}] ${titleWithUnread}` : titleWithUnread;
4948

50-
const favicon = document.getElementById('favicon');
51-
if (favicon instanceof HTMLLinkElement) {
52-
favicon.href = hasUnread ? CONFIG.FAVICON.UNREAD : CONFIG.FAVICON.DEFAULT;
53-
}
54-
}, 0);
49+
const favicon = document.getElementById('favicon');
50+
if (favicon instanceof HTMLLinkElement) {
51+
favicon.href = hasUnread ? CONFIG.FAVICON.UNREAD : CONFIG.FAVICON.DEFAULT;
52+
}
5553
}
5654

5755
/**

0 commit comments

Comments
 (0)