Skip to content

Commit

Permalink
fix: update title after navigation (#20047) (CP: 24.4) (#20061)
Browse files Browse the repository at this point in the history
* fix: update title after navigation (#20047)

Update the page title after
navigation has ended to
not have the wrong title in
history.

Fixes #19868

* fix test mock

---------

Co-authored-by: caalador <mikael.grankvist@vaadin.com>
Co-authored-by: Mikael Grankvist <mgrankvi@vaadin.com>
  • Loading branch information
3 people authored Sep 26, 2024
1 parent a99f8a7 commit 9978b1b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -695,12 +695,8 @@ public boolean containsPendingJavascript(String containsFilter) {
*/
public void setTitle(String title) {
assert title != null;
JavaScriptInvocation invocation = new JavaScriptInvocation("""
document.title = $0;
if(window?.Vaadin?.documentTitleSignal) {
window.Vaadin.documentTitleSignal.value = $0;
}
""".stripIndent(), title);
JavaScriptInvocation invocation = new JavaScriptInvocation(
generateTitleScript().stripIndent(), title);

pendingTitleUpdateCanceler = new PendingJavaScriptInvocation(
getStateTree().getRootNode(), invocation);
Expand All @@ -709,6 +705,23 @@ public void setTitle(String title) {
this.title = title;
}

private String generateTitleScript() {
String setTitleScript = """
document.title = $0;
if(window?.Vaadin?.documentTitleSignal) {
window.Vaadin.documentTitleSignal.value = $0;
}
""";
if (getSession().getConfiguration().isReactEnabled()) {
// For react-router we should wait for navigation to finish
// before updating the title.
setTitleScript = String.format(
"if(window.Vaadin.Flow.navigation) { window.addEventListener('vaadin-navigated', function(event) {%s}, {once:true}); } else { %1$s }",
setTitleScript);
}
return setTitleScript;
}

/**
* Records the text content of the title tag in the application shell.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,16 @@ function extractPath(event: MouseEvent): void | string {
* @param search search of navigation
*/
function fireNavigated(pathname:string, search: string) {
setTimeout(() =>
window.dispatchEvent(new CustomEvent('vaadin-navigated', {
detail: {
pathname,
search
}
}))
setTimeout(() => {
window.dispatchEvent(new CustomEvent('vaadin-navigated', {
detail: {
pathname,
search
}
}));
// @ts-ignore
delete window.Vaadin.Flow.navigation;
}
)
}

Expand Down Expand Up @@ -204,6 +207,8 @@ function Flow() {
}, [navigate]);

const vaadinNavigateEventHandler = useCallback((event: CustomEvent<{state: unknown, url: string, replace?: boolean, callback: boolean}>) => {
// @ts-ignore
window.Vaadin.Flow.navigation = true;
const path = '/' + event.detail.url;
navigated.current = !event.detail.callback;
fromAnchor.current = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ public void init() {
internals = new UIInternals(ui);
AlwaysLockedVaadinSession session = new AlwaysLockedVaadinSession(
vaadinService);
session.setConfiguration(Mockito.mock(DeploymentConfiguration.class));
VaadinContext context = Mockito.mock(VaadinContext.class);
Mockito.when(vaadinService.getContext()).thenReturn(context);
Mockito.when(vaadinService.getInstantiator())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.TimeoutException;

import com.vaadin.flow.component.html.testbench.DivElement;
import com.vaadin.flow.component.html.testbench.InputTextElement;
Expand Down Expand Up @@ -52,8 +53,12 @@ public void testPageTitleClears() {
}

private void verifyTitle(String title) {
Assert.assertEquals("Page title does not match", title,
getDriver().getTitle());
try {
waitUntil(driver -> driver.getTitle().equals(title));
} catch (TimeoutException te) {
Assert.fail("Page title does not match. Expected: " + title
+ ", Actual: " + driver.getTitle());
}
}

private void updateTitle(String title) {
Expand Down

0 comments on commit 9978b1b

Please sign in to comment.