Skip to content

Commit

Permalink
Fix locator bundle race condition (#786)
Browse files Browse the repository at this point in the history
Fix the race condition which occurs when the search query loads before the locator bundle

This fix ensures that the locator bundle executes after Answers loads, but before Answers initializes. The race condition issue was first found on Theme 1.20.1, and it caused the cards to break when the page loaded. I put a fix up in #739, which stopped the page from breaking, but it did not fix the underlying cause of the race condition. This PR fixes the root problem by making the execution order of the javascript consistent.

The race condition lead to an issue where two queries were ran on page load. This became a problem in a techops (Zendesk 405748) because the page would load and return no results while being zoomed into Kansas. This PR prevents the double queries from being sent if the locator bundle takes a while to load. There still may be some situations where two queries are sent on page load, but those issues are out of scope of this particular issue.  I also zoomed out the map by default so that if no default initial search is supplied, the map will show the entire US rather than be zoomed into Kansas.

J=SLAP-1329
TEST=manual

Test both Google Maps and Mapbox. Use fiddler to artificially delay the locator bundle. Test iframe and non-iframe experiences.
  • Loading branch information
cea2aj authored May 24, 2021
1 parent ab7481a commit 4fb6398
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 36 deletions.
1 change: 1 addition & 0 deletions layouts/html.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
<script>
document.addEventListener('DOMContentLoaded', function () {
{{> script/on-document-load}}
initAnswers();
});
</script>
<script src="{{relativePath}}/bundle.js" data-webpack-inline></script>
Expand Down
2 changes: 0 additions & 2 deletions script/partials/sdk-js-script-tags.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
<script
src="{{> script/partials/sdk-url sdkAsset="answers-modern.min.js" locale=locale sdkVersion=sdkVersion}}"
type="module"
onload="initAnswers()"
></script>
<script
src="{{> script/partials/sdk-url sdkAsset="answers.min.js" locale=locale sdkVersion=sdkVersion}}"
nomodule
onload="initAnswers()"
defer
></script>
2 changes: 1 addition & 1 deletion static/js/theme-map/VerticalFullPageMapOrchestrator.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class VerticalFullPageMapOrchestrator extends ANSWERS.Component {
* The default zoom level for the map
* @type {number}
*/
this.defaultZoom = this.providerOptions.zoom || 14;
this.defaultZoom = this.providerOptions.zoom || 4;

/**
* The current zoom level of the map
Expand Down
30 changes: 5 additions & 25 deletions templates/vertical-full-page-map/page-setup.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,11 @@
function addFullPageMap() {
{{> theme-components/theme-map/script}}
{{> theme-components/vertical-full-page-map/script}}
}

if (window.locatorBundleLoaded) {
addFullPageMap();
} else {
const locatorBundleScript = document.querySelector('script#js-answersLocatorBundleScript');
locatorBundleScript.onload = () => {
window.locatorBundleLoaded = true;
locatorBundleScript.dispatchEvent(new Event('vertical-full-page-map-bundle-loaded'));
addFullPageMap();
}
}
{{> theme-components/theme-map/script}}
{{> theme-components/vertical-full-page-map/script}}

/**
* Registers listeners on the card once the locator bundle is loaded
* Registers listeners on the card
*
* @param {ANSWERS.Component} card A location card
*/
function registerVerticalFullPageMapCardListeners(card) {
if (window.locatorBundleLoaded) {
new VerticalFullPageMap.CardListenerAssigner({card: card}).addListenersToCard();
return;
}
const verticalFullPageMapScript = document.querySelector('script#js-verticalFullPageMapScript');
verticalFullPageMapScript.addEventListener('vertical-full-page-map-bundle-loaded', () => {
new VerticalFullPageMap.CardListenerAssigner({card: card}).addListenersToCard();
});
function registerVerticalFullPageMapCardListeners(card) {
new VerticalFullPageMap.CardListenerAssigner({card: card}).addListenersToCard();
}
3 changes: 1 addition & 2 deletions templates/vertical-full-page-map/page.html.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
{{> templates/vertical-full-page-map/script/locationbias modifier="main" }}
{{> templates/vertical-full-page-map/script/locationbias modifier="mobileMap" }}
{{/script/core }}
<script id="js-answersLocatorBundleScript" src="{{relativePath}}/locator-bundle.js"
onload="window.locatorBundleLoaded = true;" defer></script>
<script id="js-answersLocatorBundleScript" src="{{relativePath}}/locator-bundle.js" defer></script>
<div class="Answers AnswersVerticalMap CollapsibleFilters VerticalFullPageMap VerticalFullPageMap--mobileListView js-answersVerticalFullPageMap">
<div class="Answers-content">
<div class="Answers-contentWrap js-locator-contentWrap" role="region" aria-label="{{ translate phrase="Main location search" }}">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--- locations_full_page_map_with_filters.html.hbs
+++ locations_full_page_map_with_filters.html.hbs
--- templates/vertical-full-page-map/page.html.hbs 2021-05-24 14:53:55.000000000 -0400
+++ test-site/pages/locations_full_page_map_with_filters.html.hbs 2021-05-24 14:58:17.000000000 -0400
@@ -1,7 +1,7 @@
{{#> layouts/html pageWrapperCss="YxtPage-wrapper--mobileListView" }}
{{#> script/core }}
Expand Down
4 changes: 0 additions & 4 deletions tests/script/partials/sdk-js-script-tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,20 @@ const hbs = require('../../test-utils/hbs');
const defaultOutput = `<script
src="https://assets.sitescdn.net/answers/v1.8/answers-modern.min.js"
type="module"
onload="initAnswers()"
></script>
<script
src="https://assets.sitescdn.net/answers/v1.8/answers.min.js"
nomodule
onload="initAnswers()"
defer
></script>`;

const jaOutput = `<script
src="https://assets.sitescdn.net/answers/v1.8/ja-answers-modern.min.js"
type="module"
onload="initAnswers()"
></script>
<script
src="https://assets.sitescdn.net/answers/v1.8/ja-answers.min.js"
nomodule
onload="initAnswers()"
defer
></script>`;

Expand Down

0 comments on commit 4fb6398

Please sign in to comment.