Skip to content

Commit 630c2e9

Browse files
committed
fix: always wait for navigated event for title
When running with react-router always wait for vaadin-navigated event before updating the title. Fixes #21171
1 parent 66316f1 commit 630c2e9

File tree

2 files changed

+11
-4
lines changed
  • flow-server/src/main

2 files changed

+11
-4
lines changed

flow-server/src/main/java/com/vaadin/flow/component/internal/UIInternals.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,7 @@ private String generateTitleScript() {
748748
// For react-router we should wait for navigation to finish
749749
// before updating the title.
750750
setTitleScript = String.format(
751-
"if(window.Vaadin.Flow.navigation) { window.addEventListener('vaadin-navigated', function(event) {%s}, {once:true}); } else { %1$s }",
751+
"if(window.Vaadin.Flow.navigation) { window.addEventListener('vaadin-navigated', function(event) {%s}, {once:true}); } else { %1$s console.trace();}",
752752
setTitleScript);
753753
}
754754
return setTitleScript;

flow-server/src/main/resources/com/vaadin/flow/server/frontend/Flow.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -426,8 +426,6 @@ function Flow() {
426426

427427
const vaadinNavigateEventHandler = useCallback(
428428
(event: CustomEvent<{ state: unknown; url: string; replace?: boolean; callback: boolean }>) => {
429-
// @ts-ignore
430-
window.Vaadin.Flow.navigation = true;
431429
// clean base uri away if for instance redirected to http://localhost/path/user?id=10
432430
// else the whole http... will be appended to the url see #19580
433431
const path = event.detail.url.startsWith(document.baseURI)
@@ -448,13 +446,22 @@ function Flow() {
448446
[navigate]
449447
);
450448

451-
useEffect(() => {
449+
const flowNavigation = () => {
450+
// @ts-ignore
451+
window.Vaadin.Flow.navigation = true;
452+
};
453+
454+
useEffect(() => {
455+
// @ts-ignore
456+
window.addEventListener("popstate", flowNavigation);
452457
// @ts-ignore
453458
window.addEventListener('vaadin-router-go', vaadinRouterGoEventHandler);
454459
// @ts-ignore
455460
window.addEventListener('vaadin-navigate', vaadinNavigateEventHandler);
456461

457462
return () => {
463+
// @ts-ignore
464+
window.removeEventListener("popstate", flowNavigation);
458465
// @ts-ignore
459466
window.removeEventListener('vaadin-router-go', vaadinRouterGoEventHandler);
460467
// @ts-ignore

0 commit comments

Comments
 (0)