Flow: Add API for accessing the current browser location

Created on 26 Jun 2017  路  10Comments  路  Source: vaadin/flow

Add new API to Page for accessing the location as shown in the browser's address bar. Some things to take into account:

  • This should reflect the actual location shown in the browser's address field, which might not be exactly the same as what's available through the servlet API in cases when e.g. a frontend proxy is used. One additional challenge is that this information isn't available during the initial request, so the API would either have to return e.g. null or only an approximation before the value has been verified from the browser.
  • The corresponding API in FW8 uses java.net.URI for tracking the location, but this is not optimal since there are some weird cases that browsers accept but the URI class rejects.
DX enhancement

Most helpful comment

Still relevant. Do we have a clear use case to base the implementation? Is this something Router solves?

Logically, getting the page title is similar case:
https://vaadin.com/forum/thread/17979510/tip-how-to-get-page-title-and-view-of-current-page

All 10 comments

Setting might be different with all the side effects, but getting the page current URL is very typical scenario in application code. Similar API approach than page title sound most intuitive.

We are also missing Page.setLocation(String url) & Page.reload() or similar, which is necessary in lots of cases, like after logging out from the App.

Current workaround is to use e.g. Page.executeJavaScript("window.location.href = 'myurl'); but this cumbersome.

Page.reload() is in #3299

To implement this feature Page.getLocation() could just use this:

public static URI getLocation(){
    VaadinServletRequest request = (VaadinServletRequest) VaadinService.getCurrentRequest();
    StringBuffer uriString = request.getRequestURL();
    return new URI(uriString.toString());
}

@gsedlacz. That approach works during initial rendering, but not from a background thread (where there is no current request) nor for incremental update requests since the URL of those are always the common XHR handler endpoint location and not the location that the browser is navigated to.

In 2.x versions, there is still the internal API https://vaadin.com/api/platform/14.1.3/com/vaadin/flow/server/VaadinServlet.html#getApplicationUrl-HttpServletRequest- which constructs the URL based on what was in the given request.

..but as described in the issue description, the URL might be different due to a frontend proxy, thus the JS window.location.href would be most reliable, and that could be now fetched using a Page.executeJs that provides a PendingJavaScriptResult since 2.0.

As for the use case described in #1894, there would be a need for keeping the "updated" value for saving it when a logout happens, and for that it could be sufficient to have a getInitialPageLocation() in ExtendedClientResponse and then another method in Page which would combine that to the latest routing changes, to be available immediately without a roundtrip for the logout usage.

Still relevant. Do we have a clear use case to base the implementation? Is this something Router solves?

Logically, getting the page title is similar case:
https://vaadin.com/forum/thread/17979510/tip-how-to-get-page-title-and-view-of-current-page

I could be wrong, but this kinda looks like (just another way of approaching it / could be a part of)

All three tickets (including this one) are in the top 30 upvoted open tickets in flow.

Acceptance Criteria

  • Provide the full URL to the user asynchronously - not caching the value in Page
  • Documentation to https://github.com/vaadin/docs/tree/latest/articles/flow/browser - how to get the URL (noting that ExtendedClientDetails is not documented at the moment)
  • As window.location.href will probably never be removed and we have ITs JS execution feature, only an unit test is needed
Was this page helpful?
0 / 5 - 0 ratings

Related issues

pleku picture pleku  路  4Comments

TatuLund picture TatuLund  路  3Comments

anezthes picture anezthes  路  4Comments

manolo picture manolo  路  3Comments

stefanuebe picture stefanuebe  路  4Comments