cerebral / addressbar

Makes the addressbar of the browser work just like a normal input
MIT License
84 stars 8 forks source link

document.referrer will not be updated by addressbar #19

Open bernhardreisenberger opened 6 years ago

bernhardreisenberger commented 6 years ago

Hi,

First of, i am using cerebral router for navigation. It would be neat, to access the referrer (previous visited url) from the current visited url. use-case1: i want to return to the previous site after clicking the save-button on a settings page.

  1. visit example.com/foo via addressbar
  2. enter settings via button click: goTo(example.com/settings)
  3. click on save-button at example.com/settings: save settings and redirect to referrer ( example.com/foo )

use-case2: i want to show a message after clicking the save-button on a settings page.

  1. visit example.com/settings via addressbar
  2. click on save-button at example.com/settings: save settings and show success/error message

While this is possible by saving the current location to state before going to example.com/settings, it would be easier if the new location just knows, where i came from. Using window.history.back() is not sufficient, because i only want to go back to the previous location in use-case1 Using document.referrer would be sufficient, but it is not possible, because navigation with the router will not change the document.referrer. Is this a desired behaviour?

christianalfoni commented 6 years ago

Hi @bernhardreisenberger!

I am not expert on document.referrer, but I thought this is only related to what page you came from? Not route changes on same domain. So if you came from google.com to the app, that would be the referrer. But route changes on same domain does not change document.referrer. Please prove me wrong here and I can learn something new :)

bernhardreisenberger commented 6 years ago

Example using Chromium:

  1. opening a new browser window and navigating to http://asdf.com/ will set document.referrer to ""
  2. click on "Forums" will navigate to http://forums.asdf.com/ and set document.referrer to "http://asdf.com/"
  3. click on "phpBB" will navigate to https://www.phpbb.com/ and set document.referrer to "http://forums.asdf.com/"
  4. navigating via (browser) addressbar to http://example.com/ will set document.referrer to ""

This means, route changes on same domain will be tracked with document.referrer (not only subdomain, but path changes too), but only if that change was triggered via link. This is statet on https://developer.mozilla.org/en-US/docs/Web/API/Document/referrer This results in route changes triggered via javascript, not being tracked by document.referrer.

Anyhow, document.referrer is read-only.

We could override the property using https://stackoverflow.com/a/23434948, but on the other hand, the specification does only state navigation via link, although i do not know if they had navigation via javascript in mind at the time they wrote the specification for document.referrer in 2003.

Nevertheless, in the (cerebral) addressbar code i have found a variable named prevUrl. If i understand correctly, its use is to check if an url change should happen or not. This variable prevUrl could also be used to track the previous visited url. In @cerebral/router a new function getPrevUrl() could access the value via addressbar.prevUrl That way, the @cerebral/router would always know the previous url visited.

What do you think of that idea?

bernhardreisenberger commented 6 years ago

@christianalfoni does that make sense? i can make a pull request, if you acknowledge.