Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

document.referrer will not be updated by addressbar #19

Open
bernhardreisenberger opened this issue Mar 22, 2018 · 3 comments
Open

document.referrer will not be updated by addressbar #19

bernhardreisenberger opened this issue Mar 22, 2018 · 3 comments

Comments

@bernhardreisenberger
Copy link

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?

@bernhardreisenberger bernhardreisenberger changed the title bug(addressbar): document.referrer will not be updated by addressbar document.referrer will not be updated by addressbar Mar 22, 2018
@christianalfoni
Copy link
Member

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
Copy link
Author

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants