All files referrer.js

78.57% Statements 11/14
100% Branches 4/4
60% Functions 3/5
78.57% Lines 11/14

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 43 44 45 46 47  3x 3x         3x       20x                                   3x   28x           3x   6x 2x 2x     4x    
/* Store the previous location to be used when sending referrer on navigations */
let actualReferrer = ''
let isFirstPage = true
 
/**
 * Useful wrapper around document and window objects
 */
export const utils = {
  /**
   * @returns {string} The referrer of the document
   */
  getDocumentReferrer: () => document.referrer,
  /**
   * @returns {boolean} Tell if it's the first time we're updating referrering
   */
  checkIsFirstPage: () => isFirstPage,
  /**
   * @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 = () => {
  // first time we call the update page we don't do nothing to keep correct referrer
  if (utils.checkIsFirstPage()) {
    isFirstPage = false
    return
  }
  // mutate actualReferrer with what will be the new referrer
  actualReferrer = utils.getActualLocation()
}