Skip to content

Commit 165ec80

Browse files
committed
fix(search): prevent SSR errors by checking window existence
Add checks for window existence before accessing localStorage to avoid server-side rendering errors. Also remove redundant SearchIntegration instantiation and improve type safety in event target handling.
1 parent 72cb408 commit 165ec80

2 files changed

Lines changed: 10 additions & 4 deletions

File tree

src/components/search/SearchOverlay.astro

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
---
22
import { getLangFromUrl } from "@/i18n/utils";
33
import type { LanguageKey } from "@/i18n/types";
4-
import type { SearchResult } from '../../features/search/EnterpriseSearchEngine.js';
4+
import EnterpriseSearchEngine, { type SearchResult } from '../../features/search/EnterpriseSearchEngine';
5+
import { searchIntegration } from '../../features/search/searchIntegration';
56
---
67

78
<section
@@ -67,7 +68,8 @@ import type { SearchResult } from '../../features/search/EnterpriseSearchEngine.
6768
}
6869

6970
const searchEngine = new EnterpriseSearchEngine();
70-
const searchIntegration = new SearchIntegration(); // Although not directly used here, it's good to have it initialized if needed elsewhere.
71+
// Use the already exported instance of searchIntegration
72+
// const searchIntegration = new SearchIntegration();
7173

7274
let allDocuments = [];
7375

@@ -161,7 +163,7 @@ import type { SearchResult } from '../../features/search/EnterpriseSearchEngine.
161163
// Close overlay when clicking outside the content area
162164
searchOverlay.addEventListener("click", (event) => {
163165
const contentArea = searchOverlay.querySelector(".w-full.max-w-3xl");
164-
if (contentArea && !contentArea.contains(event.target as Node)) {
166+
if (contentArea && !contentArea.contains(event.target)) {
165167
toggleSearchOverlay(false);
166168
}
167169
});

src/features/search/EnterpriseSearchEngine.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ class EnterpriseSearchEngine {
100100
]);
101101

102102
constructor() {
103-
this.loadAnalyticsFromStorage();
103+
if (typeof window !== 'undefined') {
104+
this.loadAnalyticsFromStorage();
105+
}
104106
}
105107

106108
/**
@@ -634,6 +636,7 @@ class EnterpriseSearchEngine {
634636
}
635637

636638
private loadAnalyticsFromStorage(): void {
639+
if (typeof window === 'undefined') return;
637640
try {
638641
const stored = localStorage.getItem("search-analytics");
639642
if (stored) {
@@ -650,6 +653,7 @@ class EnterpriseSearchEngine {
650653
}
651654

652655
private saveAnalyticsToStorage(): void {
656+
if (typeof window === 'undefined') return;
653657
try {
654658
localStorage.setItem("search-analytics", JSON.stringify(this.analytics));
655659
} catch (error) {

0 commit comments

Comments
 (0)