Skip to content

Commit

Permalink
PLATUI-2829: Revert change to display backlink when referer is empty (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
kyle-bowden committed Mar 28, 2024
1 parent d9de6d0 commit 9329dd1
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 23 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

## [6.13.0] - 2024-03-27

### Changed

- Revert change to retain the back link even when the referrer is empty

## [6.12.0] - 2024-03-26

### Changed
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "hmrc-frontend",
"version": "6.12.0",
"version": "6.13.0",
"description": "Design patterns for HMRC frontends",
"scripts": {
"start": "gulp dev",
Expand Down
34 changes: 15 additions & 19 deletions src/components/back-link-helper/back-link-helper.js
Expand Up @@ -5,27 +5,23 @@ function BackLinkHelper($module, window, document) {
}

BackLinkHelper.prototype.init = function init() {
const isReferrerExternal = () => {
// If there is no referrer, consider it on the same domain
if (!this.document.referrer) {
return false;
}

try {
const currentDomain = new URL(this.window.location.href).hostname;
const referrerDomain = new URL(this.document.referrer).hostname;

// Check if the referrer is not the same as the current domain
return currentDomain !== referrerDomain;
} catch (err) {
return false;
}
};

// do nothing if History API is absent
if (this.window.history) {
// hide the backlink if the referrer is on a different domain
if (isReferrerExternal()) {
// eslint-disable-next-line max-len
/* TODO: It remains unclear whether a check for the same domain is necessary for security reasons.
There may be user research suggesting considerations regarding the visibility of the
back link on refresh.
Currently, a page refresh sets the referer to empty, leading to the back link being hidden
under our existing logic.
*/
// eslint-disable-next-line max-len
const referrerNotOnSameDomain = () => {
const referer = this.document.referrer;
return !referer || referer.indexOf(this.window.location.host) === -1;
};

// hide the backlink if the referrer is on a different domain or the referrer is not set
if (referrerNotOnSameDomain()) {
this.$module.classList.add('hmrc-hidden-backlink');
} else {
// prevent resubmit warning
Expand Down
11 changes: 9 additions & 2 deletions src/components/back-link-helper/back-link-helper.test.js
Expand Up @@ -71,11 +71,18 @@ describe('/components/back-link-helper', () => {
expect(mockWindow.history.back).toHaveBeenCalled();
});

it('does not add .hmrc-hidden-backlink class to backlink when document referer is empty', () => {
it('adds .hmrc-hidden-backlink class to backlink when document referer is empty', () => {
const sut = new BackLinkHelper(mockAnchorTag, mockWindow, { referrer: '' });
sut.init();

expect(mockAnchorTag.classList.add.mock.calls).toHaveLength(0);
expect(mockAnchorTag.classList.add.mock.calls[0][0]).toBe('hmrc-hidden-backlink');
});

it('adds .hmrc-hidden-backlink class to backlink when document referer is null', () => {
const sut = new BackLinkHelper(mockAnchorTag, mockWindow, { referrer: null });
sut.init();

expect(mockAnchorTag.classList.add.mock.calls[0][0]).toBe('hmrc-hidden-backlink');
});

it('adds .hmrc-hidden-backlink class to backlink if document referer is on a different domain', () => {
Expand Down

0 comments on commit 9329dd1

Please sign in to comment.