Skip to content

Commit ca651d7

Browse files
committed
Optimize location handling with faster default zip code fallback
1 parent 18ea168 commit ca651d7

File tree

1 file changed

+85
-23
lines changed

1 file changed

+85
-23
lines changed

src/components/Explorer.vue

Lines changed: 85 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,40 +1029,75 @@ export default {
10291029
this.manualZip = localStorage.getItem("manualZip") === "true";
10301030
this.filterValues["States"] = [localStorage.getItem("state")];
10311031
1032+
// Pre-set the default zip code to ensure immediate response
1033+
if (!this.zipCode) {
1034+
this.zipCode = "19355";
1035+
}
1036+
10321037
if (!this.manualZip && "geolocation" in navigator) {
1038+
// Set loading state
1039+
this.isLoadingLocation = true;
1040+
1041+
// Use a timeout to limit how long we wait for geolocation
1042+
const geolocationTimeout = setTimeout(() => {
1043+
if (this.isLoadingLocation) {
1044+
// If still loading after timeout, use default
1045+
this.isLoadingLocation = false;
1046+
this.setDefaultZipCode();
1047+
}
1048+
}, 2000); // 2 second timeout
1049+
10331050
navigator.geolocation.getCurrentPosition(
10341051
async (position) => {
1052+
clearTimeout(geolocationTimeout);
1053+
10351054
const data = {
10361055
latitude: position.coords.latitude,
10371056
longitude: position.coords.longitude,
10381057
};
10391058
1040-
const response = await fetch("/get-zip", {
1041-
method: "POST",
1042-
headers: {
1043-
"Content-Type": "application/json",
1044-
},
1045-
body: JSON.stringify(data),
1046-
});
1047-
let json = await response.json();
1048-
this.zipCode = json.code;
1049-
this.filterValues["States"] = [json.state];
1050-
this.displayLocation = `${json.city}, ${json.state}`;
1051-
1052-
localStorage.setItem("zipCode", this.zipCode);
1053-
localStorage.setItem("state", json.state);
1054-
localStorage.setItem("displayLocation", this.displayLocation);
1055-
localStorage.removeItem("manualZip");
1056-
},
1057-
() => {
1058-
if (!this.zipCode) {
1059-
this.zipCode = "19355";
1059+
try {
1060+
const response = await fetch("/get-zip", {
1061+
method: "POST",
1062+
headers: {
1063+
"Content-Type": "application/json",
1064+
},
1065+
body: JSON.stringify(data),
1066+
});
1067+
1068+
if (!response.ok) {
1069+
throw new Error('Network response was not ok');
1070+
}
1071+
1072+
let json = await response.json();
1073+
this.zipCode = json.code;
1074+
this.filterValues["States"] = [json.state];
1075+
this.displayLocation = `${json.city}, ${json.state}`;
1076+
1077+
localStorage.setItem("zipCode", this.zipCode);
1078+
localStorage.setItem("state", json.state);
1079+
localStorage.setItem("displayLocation", this.displayLocation);
1080+
localStorage.removeItem("manualZip");
1081+
} catch (error) {
1082+
console.error("Error fetching zip code:", error);
1083+
this.setDefaultZipCode();
1084+
} finally {
1085+
this.isLoadingLocation = false;
10601086
}
1061-
}
1087+
},
1088+
(error) => {
1089+
clearTimeout(geolocationTimeout);
1090+
console.error("Geolocation error:", error);
1091+
this.isLoadingLocation = false;
1092+
1093+
// Use default zip code if geolocation fails
1094+
this.setDefaultZipCode();
1095+
},
1096+
{ timeout: 3000, maximumAge: 60000 } // 3s timeout, cache for 1 minute
10621097
);
1063-
} else if (!this.zipCode) {
1098+
} else if (!this.displayLocation) {
10641099
// Geolocation not available or user chose manual zip
1065-
this.zipCode = "19355";
1100+
this.setDefaultZipCode();
10661101
}
10671102
10681103
await this.determineFilterCountsAndSubmit();
@@ -1072,6 +1107,33 @@ export default {
10721107
document.body.removeEventListener("click", this.bodyClick);
10731108
},
10741109
methods: {
1110+
setDefaultZipCode() {
1111+
// Set default zip code to 19355 (Malvern, PA)
1112+
this.zipCode = "19355";
1113+
1114+
// Get city and state info for this zip code
1115+
fetch("/get-city", {
1116+
method: "POST",
1117+
headers: {
1118+
"Content-Type": "application/json",
1119+
},
1120+
body: JSON.stringify({ zipCode: this.zipCode }),
1121+
})
1122+
.then(response => response.json())
1123+
.then(json => {
1124+
this.filterValues["States"] = [json.state];
1125+
this.displayLocation = `${json.city}, ${json.state}`;
1126+
1127+
// Save to localStorage
1128+
localStorage.setItem("zipCode", this.zipCode);
1129+
localStorage.setItem("state", json.state);
1130+
localStorage.setItem("displayLocation", this.displayLocation);
1131+
})
1132+
.catch(error => {
1133+
console.error("Error fetching city data:", error);
1134+
});
1135+
},
1136+
10751137
forceUpdate() {
10761138
// Force Vue to re-render by directly calling submit and manipulating the DOM
10771139
this.submit();

0 commit comments

Comments
 (0)