Skip to content

Commit 3df0286

Browse files
committed
fix an issue where the current page would be used instead of a third party referrer
1 parent fcfdff7 commit 3df0286

2 files changed

Lines changed: 36 additions & 15 deletions

File tree

src/app/core/services/browser.referrer.service.spec.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe(`BrowserReferrerService`, () => {
1010

1111
beforeEach(() => {
1212
routeService = {
13-
getPreviousUrl: () => observableOf('')
13+
getHistory: () => observableOf([])
1414
} as any;
1515
service = new BrowserReferrerService(
1616
{ referrer: documentReferrer },
@@ -20,12 +20,22 @@ describe(`BrowserReferrerService`, () => {
2020
});
2121

2222
describe(`getReferrer`, () => {
23-
let prevUrl: string;
23+
describe(`when the history is an empty`, () => {
24+
beforeEach(() => {
25+
spyOn(routeService, 'getHistory').and.returnValue(observableOf([]));
26+
});
27+
28+
it(`should return document.referrer`, (done: DoneFn) => {
29+
service.getReferrer().subscribe((emittedReferrer: string) => {
30+
expect(emittedReferrer).toBe(documentReferrer);
31+
done();
32+
});
33+
});
34+
});
2435

25-
describe(`when getPreviousUrl is an empty string`, () => {
36+
describe(`when the history only contains the current route`, () => {
2637
beforeEach(() => {
27-
prevUrl = '';
28-
spyOn(routeService, 'getPreviousUrl').and.returnValue(observableOf(prevUrl));
38+
spyOn(routeService, 'getHistory').and.returnValue(observableOf(['/current/route']));
2939
});
3040

3141
it(`should return document.referrer`, (done: DoneFn) => {
@@ -36,13 +46,18 @@ describe(`BrowserReferrerService`, () => {
3646
});
3747
});
3848

39-
describe(`when getPreviousUrl is not empty`, () => {
49+
describe(`when the history contains multiple routes`, () => {
50+
const prevUrl = '/the/route/we/need';
4051
beforeEach(() => {
41-
prevUrl = '/some/local/route';
42-
spyOn(routeService, 'getPreviousUrl').and.returnValue(observableOf(prevUrl));
52+
spyOn(routeService, 'getHistory').and.returnValue(observableOf([
53+
'/first/route',
54+
'/second/route',
55+
prevUrl,
56+
'/current/route'
57+
]));
4358
});
4459

45-
it(`should return the value emitted by getPreviousUrl combined with the origin from HardRedirectService`, (done: DoneFn) => {
60+
it(`should return the last route before the current one combined with the origin from HardRedirectService`, (done: DoneFn) => {
4661
service.getReferrer().subscribe((emittedReferrer: string) => {
4762
expect(emittedReferrer).toBe(origin + prevUrl);
4863
done();

src/app/core/services/browser.referrer.service.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ReferrerService } from './referrer.service';
22
import { Observable } from 'rxjs';
33
import { map } from 'rxjs/operators';
4-
import { isEmpty } from '../../shared/empty.util';
4+
import { isEmpty, hasNoValue } from '../../shared/empty.util';
55
import { URLCombiner } from '../url-combiner/url-combiner';
66
import { Inject, Injectable } from '@angular/core';
77
import { DOCUMENT } from '@angular/common';
@@ -33,13 +33,19 @@ export class BrowserReferrerService extends ReferrerService {
3333
* in the store yet, document.referrer will be used
3434
*/
3535
public getReferrer(): Observable<string> {
36-
return this.routeService.getPreviousUrl().pipe(
37-
map((prevUrl: string) => {
38-
// if we don't have anything in the history yet, return document.referrer
39-
// (note that that may be empty too, e.g. if you've just opened a new browser tab)
40-
if (isEmpty(prevUrl)) {
36+
return this.routeService.getHistory().pipe(
37+
map((history: string[]) => {
38+
const currentURL = history[history.length - 1];
39+
// if the current URL isn't set yet, or the only URL in the history is the current one,
40+
// return document.referrer (note that that may be empty too, e.g. if you've just opened a
41+
// new browser tab)
42+
if (hasNoValue(currentURL) || history.every((url: string) => url === currentURL)) {
4143
return this.document.referrer;
4244
} else {
45+
// reverse the history
46+
const reversedHistory = [...history].reverse();
47+
// and find the first URL that differs from the current one
48+
const prevUrl = reversedHistory.find((url: string) => url !== currentURL);
4349
return new URLCombiner(this.hardRedirectService.getCurrentOrigin(), prevUrl).toString();
4450
}
4551
})

0 commit comments

Comments
 (0)