flow: Flow page title is not propagated to the top part of the app layout in a Hilla main layout

Description of the bug

When using a Hilla main menu from start.vaadin.com, the page title of a Flow view (set using @PageTitle or HasDynamicTitle) is applied to document.title but the same value is not shown in the top area above the view (the <h2 slot="navbar"> element). Instead, the title in the navbar falls back to the application name. This is inconsistent with how it works for Hilla views in the same application and with how it works in an application with a main layout implemented in Flow.

Even though this should probably be ultimately fixed in Hilla or Start, I’m filing it against Flow since I expect that some new feature will have to be added to Flow first and then only enabled through Hilla or Start.

Expected behavior

Expected that the Flow view title is shown in the same way as Hilla page titles.

Minimal reproducible example

  1. Create an application at start.vaadin.com with one Hilla view and one Flow view. Give each view a distinct name (e.g. “Hilla” and “Flow”)
  2. mvn
  3. Navigate to the Hilla view. Observe that that the name of the view is shown in the top of the main layout.
  4. Navigate to the Flow view (not that it’s not in the menu so you need to type the URL or go thorough the dev mode 404 view). Observe that the value shown in the top of the main layout is the name of the app rather than the name of the view.

Versions

  • Vaadin / Flow version: Vaadin 24.4.0.alpha22 (or alpha21 which is what you get from Start today)

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 18 (16 by maintainers)

Commits related to this issue

Most upvoted comments

All the actual logic for interpreting the value that Flow sets in the signal instance is in @layout.tsx which is owned by the application. The application developer can remove all the new logic from that file and then everything will work like in Vaadin 24.3. Or they can modify the logic to e.g. combine the documentTitleSignal value with the value of an application-specific signal to form the actual document.title value.

To summarize all so far, following @layout.tsx works with the start.vaadin.com app with #19329 change in Flow:

import { createMenuItems, useViewConfig } from '@vaadin/hilla-file-router/runtime.js';
import { AppLayout, DrawerToggle, Icon, SideNav, SideNavItem } from '@vaadin/react-components';
import { Suspense } from 'react';
import { Outlet, useLocation, useNavigate } from 'react-router-dom';
import { Signal, signal, effect } from '@vaadin/hilla-react-signals';

const vaadin = window.Vaadin as {
  documentTitleSignal: Signal<string>;
};
vaadin.documentTitleSignal = signal("");
effect(() =>  document.title = vaadin.documentTitleSignal.value);

export default function MainLayout() {
  const currentTitle = useViewConfig()?.title ?? "";
  const navigate = useNavigate();
  const location = useLocation();

  vaadin.documentTitleSignal.value = currentTitle;

  return (
    <AppLayout primarySection="drawer">
      <div slot="drawer" className="flex flex-col justify-between h-full p-m">
        <header className="flex flex-col gap-m">
          <h1 className="text-l m-0">My App</h1>
          <SideNav onNavigate={({ path }) => navigate(path!)} location={location}>
            {createMenuItems().map(({ to, title, icon }) => (
              <SideNavItem path={to} key={to}>
                {icon ? <Icon src={icon} slot="prefix"></Icon> : <></>}
                {title}
              </SideNavItem>
            ))}
          </SideNav>
        </header>
      </div>

      <DrawerToggle slot="navbar" aria-label="Menu toggle"></DrawerToggle>
      <h2 slot="navbar" className="text-l m-0">
        {vaadin.documentTitleSignal}
      </h2>

      <Suspense>
        <Outlet />
      </Suspense>
    </AppLayout>
  );
}

I don’t think there’s any reason to have vaadin.documentTitleSignal.value = currentTitle; inside useEffect. All that useEffect does in that case is to avoid running the callback again if currentTitle hasn’t changed but that’s something that is already handled by the documentTitleSignal instance.

For defaultTitle, I’m starting suspect that it’s never correct to update the title in the layout’s render function. But we should maybe accept the flickering for now and then separately check with the Hilla team to see if they have some idea for how to better update the title for Hilla views.

That example might work in this particular case but it can be improved for clarity and to work as expected also in other cases.

The first problem is that the signal instance is now owned by a MainLayout component instance but it’s exported to a wider scope (since window.Vaadin may outlive the MainLayout instance). The signal instance should instead be created outside the render function (but it can still be in the same module for now).

The second problem is that document.title is updated only in a React useEffect which means that it’s updated only when the main layout is re-rendered. If the application has this kind of signal available, then it would be quite natural for a view to e.g. update the signal value based on updates received asynchronously from the server. With the current implementation, that would update the value in the layout but it wouldn’t update document.title. The update should be done in a signal effect rather than in a React effect.

With those two adjustments, we would have these lines of code before the render function:

import { signal, effect } from '@vaadin/hilla-react-signals';

const vaadin = window.Vaadin as {
  documentTitleSignal: Signal<string>;
};
vaadin.documentTitleSignal = signal("");
effect(() =>  document.title = vaadin.documentTitleSignal.value);

With those changes, the React useEffect would be redundant and we could instead directly assign the signal value in the render function (vaadin.documentTitleSignal.value = currentTitle) since the signal implementation already takes care of change detection.

The menu title does not necessarily have anything to do with the page title, especially in the case of HasDynamicTitle and views that aren’t even directly shown in the menu.

The view title needs to be propagated to the <h2> in the main layout at runtime in a way that is consistent with how document.title is set rather than based on the declarative menu configuration.

Furthermore, the page title should be updated even if the application doesn’t use the automatic main menu feature at all but instead defines the menu manually.