Skip to content

Commit

Permalink
fix: set page title to documentTitleSignal (#19329) (#19359)
Browse files Browse the repository at this point in the history
`UIInternals#setTitle(String)` sets page title via JavaScript to `document.title` and optionally `window.Vaadin.documentTitleSignal.value` where documentTitleSignal is expected but not limited to be Signal<string> type with a value field. This allows Hilla main layout, when used, being kept in sync with the Flow page title even when set via PageTitle annotation or HasDynamicTitle interface.

Fixes: #19200

Co-authored-by: Tomi Virtanen <tltv@vaadin.com>
Co-authored-by: Mikhail Shabarov <61410877+mshabarov@users.noreply.github.com>
  • Loading branch information
3 people committed May 13, 2024
1 parent 83ccb0c commit d834d71
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -692,8 +692,12 @@ public boolean containsPendingJavascript(String containsFilter) {
*/
public void setTitle(String title) {
assert title != null;
JavaScriptInvocation invocation = new JavaScriptInvocation(
"document.title = $0", title);
JavaScriptInvocation invocation = new JavaScriptInvocation("""
document.title = $0;
if(window?.Vaadin?.documentTitleSignal) {
window.Vaadin.documentTitleSignal.value = $0;
}
""".stripIndent(), title);

pendingTitleUpdateCanceler = new PendingJavaScriptInvocation(
getStateTree().getRootNode(), invocation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,35 @@ public void isDirty_pendingJsInvocationNotReadyToSend_returnsFalse() {
internals.isDirty());
}

@Test
public void setTitle_titleAndPendingJsInvocationSetsCorrectTitle() {
internals.setTitle("new title");
Assert.assertEquals("new title", internals.getTitle());

Assert.assertEquals("one pending JavaScript invocation should exist", 1,
internals.getPendingJavaScriptInvocations().count());

var pendingJavaScriptInvocation = internals
.getPendingJavaScriptInvocations().findFirst().orElse(null);
Assert.assertNotNull("pendingJavaScriptInvocation should not be null",
pendingJavaScriptInvocation);
Assert.assertEquals("new title", pendingJavaScriptInvocation
.getInvocation().getParameters().get(0));
Assert.assertTrue("document.title should be set via JavaScript",
pendingJavaScriptInvocation.getInvocation().getExpression()
.contains("document.title = $0"));
Assert.assertTrue(
"window.Vaadin.documentTitleSignal.value should be set conditionally via JavaScript",
pendingJavaScriptInvocation.getInvocation().getExpression()
.contains(
"""
if(window?.Vaadin?.documentTitleSignal) {
window.Vaadin.documentTitleSignal.value = $0;
}
"""
.stripIndent()));
}

private PushConfiguration setUpInitialPush() {
DeploymentConfiguration config = Mockito
.mock(DeploymentConfiguration.class);
Expand Down

0 comments on commit d834d71

Please sign in to comment.