Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update title after navigation (#20047) (CP: 24.5) #20060

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -699,12 +699,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 @@ -713,6 +709,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 @@ -151,13 +151,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 @@ -255,6 +258,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 @@ -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