Skip to content

Commit

Permalink
fix: add in try catch when trying to parse search message
Browse files Browse the repository at this point in the history
  • Loading branch information
timkim committed Feb 14, 2025
1 parent 693439c commit f894ae2
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions packages/gatsby-theme-aio/src/components/Search/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,24 +287,29 @@ const Search = ({ algolia, indexAll, indexPrefix, showSearch, setShowSearch, sea
if (isIFramed) {

window.addEventListener("message", (e) => {
const message = JSON.parse(e.data);
if (message.localPathName) {
let localPathName = message.localPathName;
if (localPathName !== "/") {
// make sure path name has a slash at start/end to match path-prefix format
if (!localPathName.startsWith('/')) { localPathName = `/${localPathName}` }
if (!localPathName.endsWith('/')) { localPathName = `${localPathName}/` }
const localProduct = indexAll.find(product => product.productIndices.some(idx => {
return localPathName.startsWith(idx.indexPathPrefix);
}));

if (localProduct?.productName) {
setSearchIndex([localProduct.productName, ...searchIndex]);
try {
const message = JSON.parse(e.data);
if (message.localPathName) {
let localPathName = message.localPathName;
if (localPathName !== "/") {
// make sure path name has a slash at start/end to match path-prefix format
if (!localPathName.startsWith('/')) { localPathName = `/${localPathName}` }
if (!localPathName.endsWith('/')) { localPathName = `${localPathName}/` }
const localProduct = indexAll.find(product => product.productIndices.some(idx => {
return localPathName.startsWith(idx.indexPathPrefix);
}));

if (localProduct?.productName) {
setSearchIndex([localProduct.productName, ...searchIndex]);
}
}

const reply = JSON.stringify({ received: message.localPathName });
parent.postMessage(reply, "*");
}

const reply = JSON.stringify({ received: message.localPathName });
parent.postMessage(reply, "*");
} catch (e) {
console.log(`Unable to retrieve message:`);
console.log(e);
}
});
};
Expand Down

0 comments on commit f894ae2

Please sign in to comment.