All files referrer.js

75% Statements 9/12
75% Branches 3/4
50% Functions 2/4
75% Lines 9/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42  1x         1x                                   1x   13x           1x   4x 1x 1x     3x    
/* Store the previous location to be used when sending referrer on navigations */
let actualReferrer = ''
 
/**
 * Useful wrapper around document and window objects
 */
export const utils = {
  /**
   * @returns {String} The referrer of the document
   */
  getDocumentReferrer: () => document.referrer,
  /**
   * @returns {String} The actual location with protocol, domain and pathname
   */
  getActualLocation: () => {
    const {origin, pathname} = window.location
    return `${origin}${pathname}`
  }
}
 
/**
 * Get the correct page referrer for SPA navigations
 * @returns {String} referrer
 */
export const getPageReferrer = () => {
  // get the referrer from the previous location if available, fallback to the document
  return actualReferrer || utils.getDocumentReferrer()
}
 
/**
 * Update page referrer for SPA navigations
 */
export const updatePageReferrer = () => {
  // if it's first time we're executing, the actualReferrer is the document.referrer
  if (!actualReferrer) {
    actualReferrer = utils.getDocumentReferrer()
    return
  }
  // mutate actualReferrer with what will be the new referrer
  actualReferrer = utils.getActualLocation()
}