Skip to content

Commit

Permalink
Correct bug in isStaging method. (#1125)
Browse files Browse the repository at this point in the history
This PR corrects a bug in the `isStaging` method. If you were referred to a Production
site from a Staging link, `isStaging` would incorrectly report `true`. This is because
the `includes` check on the `href` would catch the Staging `referrerPageUrl`. Simply
comparring `window.location.hostname` instead of `window.location.href` fixes this.
I also did some code cleanup in this method and its test suite.

J=SLAP-2546
TEST=auto
  • Loading branch information
tmeyer2115 committed Jan 9, 2023
1 parent f87864a commit b3d626f
Show file tree
Hide file tree
Showing 6 changed files with 228 additions and 38 deletions.
161 changes: 152 additions & 9 deletions package-lock.json

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

6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@
"modulePathIgnorePatterns": [
"/test-site/"
],
"transformIgnorePatterns": [
"!node_modules/escape-string-regexp"
],
"testPathIgnorePatterns": [
"/fixtures/",
"/test-utils/",
Expand All @@ -91,5 +94,8 @@
"/wcag/",
"/acceptance/"
]
},
"dependencies": {
"escape-string-regexp": "^5.0.0"
}
}
17 changes: 14 additions & 3 deletions static/js/is-staging.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
import escapeStringRegexp from 'escape-string-regexp';

/**
* Determines whether the current url is staging or production.
*/
export function isStaging(stagingDomains) {
const defaultStagingDomains = ['127.0.0.1', 'localhost', 'office.yext.com'];
const _stagingDomains = defaultStagingDomains.concat(stagingDomains);
const currentUrl = window.location.href;
return _stagingDomains.some(domain => currentUrl.includes(domain));
const _stagingDomains = stagingDomains
? defaultStagingDomains.concat(stagingDomains)
: defaultStagingDomains;
const currentDomain = window.location.hostname;

return _stagingDomains.some(domain => {
const regexEscapedDomain = escapeStringRegexp(domain);
// The RFC for subdomains says they can contain a maximum of 63 characters. The leading and trailing characters must be alpha numeric.
const isSubdomainRegex = new RegExp(`[A-Za-z0-9]?[A-Za-z0-9\-]{0,61}[A-Za-z0-9]?\.${regexEscapedDomain}`);

return domain === currentDomain || isSubdomainRegex.test(currentDomain);
});
}

0 comments on commit b3d626f

Please sign in to comment.