Modifying the bootstrap page

The application bootstrap page is created for you by the framework and normally there is no need to modify it. For instance Flow includes its internal JavaScripts to be able to provide its core functionality. Also it is possible to include additional JavaScripts, HTML imports and Style Sheets using annotations @JavaScript, HtmlImport and @StyleSheet (see Including Style Sheets and Importing html/javascript).

Sometimes you may want to customize the page header and add there some additional data, e.g. custom meta tags. Such markup is required to enable your web page to become a rich object in a social graph using OpenGraph protocol.

Viewport annotation

Viewport meta-tag can be set to the initial page by annotating the navigation target with @Route or the top most RouterLayout that builds the navigation target chain.

Java

  1. @Route("")
  2. @Viewport("width=device-width")
  3. public class MyApp extends Div {
  4. public MyApp() {
  5. setText("Hello world");
  6. }
  7. }

Java

  1. @Route(value = "", layout = MyLayout.class)
  2. public class MyView extends Div {
  3. public MyView() {
  4. setText("Hello world");
  5. }
  6. }
  7. @Viewport("width=device-width")
  8. public class MyLayout extends Div implements RouterLayout {
  9. }
Note
If the Viewport annotation is not on a `@Route Component or a top RouterLayout an exception will be thrown on startup.

Inline annotation

The initial page can be modified by using the @Inline annotations to add file contents to either the <head> or the <body>. Inline is repeatable, so multiple @Inline annotations can be added at once.

Inline will add the contents of a classloader resource by default as appended to the head of the initial page with type decided by the file suffix.

The configurations available for inlining contents are:

  • TargetElement [HEAD, BODY]

  • Position [APPEND, PREPEND]

  • Wrapping [AUTOMATIC, NONE, STYLESHEET, JAVASCRIPT]

Java

  1. @Route(value = "", layout = MyInline.class)
  2. public class MyRoot extends Div {
  3. public MyRoot() {
  4. }
  5. }
  6. @Inline("initialization.js")
  7. @Inline("initial_style.css")
  8. @Inline(value = "important_styles", wrapping = Inline.Wrapping.STYLESHEET)
  9. public class MyInline extends Div implements RouterLayout {
  10. }
Note
If the Inline annotation is not on a @Route Component or a top RouterLayout an exception will be thrown on startup.

PageConfigurator

To be able to modify default bootstrap page and add your custom meta tags on the page you can implement the PageConfigurator on the navigation target with @Route or the top most RouterLayout that builds the navigation target chain. The PageConfigurator gives you easy access to customize the LoadingIndicatorConfiguration, ReconnectDialogConfiguration and PushConfiguration for the initial response.

With the PageConfigurator you can prepend or append javascript, html and css to the head by giving a file on the classpath or as content string. Also supported is adding links and meta tags which can also be either prepended or appended.

Setting the viewport meta tag using InitialPageSettings::setViewport will override any viewport set through a @Viewport annotation.

By default everything is appended, but if needed you can give the position InitialPageSettings.Position.PREPEND to have the item prepended to head instead.

Here is the code for the PageConfigurator implementation on the top RouterLayout which modifies the header of the page:

Java

  1. @Route(value = "", layout = MainLayout.class)
  2. public class Root extends Div {
  3. }
  4. public class MainLayout extends Div
  5. implements RouterLayout, PageConfigurator {
  6. @Override
  7. public void configurePage(InitialPageSettings settings) {
  8. settings.addInlineFromFile(InitialPageSettings.Position.PREPEND,
  9. "inline.js", InitialPageSettings.WrapMode.JAVASCRIPT);
  10. settings.addMetaTag("og:title", "The Rock");
  11. settings.addMetaTag("og:type", "video.movie");
  12. settings.addMetaTag("og:url",
  13. "http://www.imdb.com/title/tt0117500/");
  14. settings.addMetaTag("og:image",
  15. "http://ia.media-imdb.com/images/rock.jpg");
  16. settings.addLink("shortcut icon", "icons/favicon.ico");
  17. settings.addFavIcon("icon", "icons/icon-192.png", "192x192");
  18. }
  19. }
Note
If the PageConfigurator implementation is not on a @Route Component or a RouterLayout used from a route it will not be used.

Setting the body size styles

By default, the body element in a Flow application has size properties height = "100vh", width = "100vw", which makes the page fill the entire viewport. To change the width and height properties of the body you can either use the @BodySize annotation or the PageConfigurator.

For @BodySize you just add it to the the navigation target with @Route or the top most RouterLayout that builds the navigation target chain. You can pass custom height and width properties for the annotation, or leave them out to just prevent the default size to be applied for the body:

Java

  1. @Route("")
  2. @BodySize
  3. public static class BodySizeAnnotatedRoute extends Div {
  4. }

With the PageConfigurator you can just addInlineContent like:

Java

  1. @Route("")
  2. public static class InitialPageConfiguratorBodyStyle extends Div
  3. implements PageConfigurator {
  4. @Override
  5. public void configurePage(InitialPageSettings settings) {
  6. settings.addInlineWithContents("body {width: 100vw; height:100vh;}",
  7. InitialPageSettings.WrapMode.STYLESHEET);
  8. }
  9. }
Note
Only one way should be used as else the later statement will override the earlier one. In practise this would mean that by default the PageConfigurator will override the @BodySize except if the inlining is done as a PREPEND then the @BodySize will be the deciding one.
Note
If the BodySize annotation is not on a @Route Component or a top RouterLayout an exception will be thrown on startup.
Note
When using an empty @BodySize annotation (which doesn’t apply any sizing for the UI / body), you will not be able to use relative sizing (% as unit) for any component, unless the component has a parent that has defined size using anything else than % as the unit. For that reason, it is recommended to use the default settings for the body size, by omitting the @BodySize annotation, or to declare a specific size for it.

BootstrapListener

To be able to modify default bootstrap page and add your custom meta tags on the page you should use your BootstrapListener implementation and add it to the ServiceInitEvent instance available in a VaadinServiceInitListener.

Here is the code for the BoostrapListener implementation which modifies the header of the page:

Java

  1. public class CustomBootstrapListener implements BootstrapListener {
  2. public void modifyBootstrapPage(BootstrapPageResponse response) {
  3. Document document = response.getDocument();
  4. Element head = document.head();
  5. head.appendChild(createMeta(document, "og:title", "The Rock"));
  6. head.appendChild(createMeta(document, "og:type", "video.movie"));
  7. head.appendChild(createMeta(document, "og:url",
  8. "http://www.imdb.com/title/tt0117500/"));
  9. head.appendChild(createMeta(document, "og:image",
  10. "http://ia.media-imdb.com/images/rock.jpg"));
  11. }
  12. private Element createMeta(Document document, String property,
  13. String content) {
  14. Element meta = document.createElement("meta");
  15. meta.attr("property", property);
  16. meta.attr("content", content);
  17. return meta;
  18. }
  19. }

Now this listener should be added to a ServiceInitEvent which is sent when a Vaadin service is initialized. Take a look on the ServiceInitListener tutorial on how to configure it.

Adding static HTML contents

The framework provides multiple ways of adding static content to the page. Here we cover three different ways of adding a favicon.

  • using InitialPageSettings#addLink()

Java

  1. public class Layout1 extends Div implements RouterLayout, PageConfigurator {
  2. @Override
  3. public void configurePage(InitialPageSettings settings) {
  4. HashMap<String, String> attributes = new HashMap<>();
  5. attributes.put("rel", "shortcut icon");
  6. settings.addLink("icons/favicon.ico", attributes);
  7. }
  8. }
  • using InitialPageSettings#addInlineWithContents()

Java

  1. public class Layout2 extends Div implements RouterLayout, PageConfigurator {
  2. @Override
  3. public void configurePage(InitialPageSettings settings) {
  4. settings.addInlineWithContents(
  5. "<link rel=\"shortcut icon\" href=\"icons/favicon.ico\">",
  6. InitialPageSettings.WrapMode.NONE);
  7. }
  8. }

Java

  1. public class Layout3 extends Div
  2. implements RouterLayout, BootstrapListener {
  3. @Override
  4. public void modifyBootstrapPage(BootstrapPageResponse response) {
  5. final Element head = response.getDocument().head();
  6. head.append(
  7. "<link rel=\"shortcut icon\" href=\"icons/favicon.ico\">");
  8. }
  9. }

But most commonly, you will deal with quite many files, in this case, you can see that it causes a lot of hard coding easily. To avoid this, we recommend you to move all the contents into a file, (e.g. your-content.html) and inline this file in your PageConfigurator

Java

  1. public class Layout4 extends Div implements RouterLayout, PageConfigurator {
  2. @Override
  3. public void configurePage(InitialPageSettings settings) {
  4. settings.addInlineFromFile("your-content.html",
  5. InitialPageSettings.WrapMode.NONE);
  6. }
  7. }