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.4) #20061

Merged
merged 2 commits 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 @@ -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