Skip to content

Commit 1f7c4c8

Browse files
committed
Fix no DOM error
1 parent 39c86b5 commit 1f7c4c8

File tree

3 files changed

+46
-36
lines changed

3 files changed

+46
-36
lines changed

.npmignore

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
.tsbuildinfo*
22
tsconfig*
3+
lib/tests
34
src/tests
5+
.github
6+
.husky
7+
CODE_OF_CONDUCT.md
8+
CONTRIBUTING.md
9+
getPkgTag.js
10+
jest.config.js

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@twilio-labs/docusaurus-plugin-segment",
3-
"version": "0.1.0-pre1",
3+
"version": "0.1.0-pre2",
44
"description": "Segment plugin for Docusaurus.",
55
"main": "lib/index.js",
66
"types": "lib/index.d.ts",

src/track-search.ts

+38-35
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,48 @@
1-
// The following is for integrating with https://github.com/cmfcmf/docusaurus-search-local
1+
import ExecutionEnvironment from "@docusaurus/core/lib/client/exports/ExecutionEnvironment";
22

3-
let searchInput, timeout: NodeJS.Timeout | undefined;
3+
if (ExecutionEnvironment.canUseDOM) {
4+
// The following is for integrating with https://github.com/cmfcmf/docusaurus-search-local
5+
let searchInput, timeout: NodeJS.Timeout | undefined;
46

5-
function processEvent(event: Event): void {
6-
clearTimeout(timeout);
7-
timeout = setTimeout(() => {
8-
const query = (event?.target as HTMLInputElement)?.value.trim();
9-
if (query) {
10-
window.analytics.track("Searched", {
11-
category: "Search",
12-
label: window.location.pathname,
13-
properties: {
14-
query,
15-
},
16-
});
7+
function processEvent(event: Event): void {
8+
clearTimeout(timeout);
9+
timeout = setTimeout(() => {
10+
const query = (event?.target as HTMLInputElement)?.value.trim();
11+
if (query) {
12+
window.analytics.track("Searched", {
13+
category: "Search",
14+
label: window.location.pathname,
15+
properties: {
16+
query,
17+
},
18+
});
19+
}
20+
}, 1000);
21+
}
22+
23+
function checkSearchInput() {
24+
searchInput = document.querySelector("input[type=search]");
25+
if (searchInput) {
26+
searchInput.addEventListener("input", processEvent);
27+
return true;
1728
}
18-
}, 1000);
19-
}
2029

21-
function checkSearchInput() {
22-
searchInput = document.querySelector("input[type=search]");
23-
if (searchInput) {
24-
searchInput.addEventListener("input", processEvent);
25-
return true;
30+
return false;
2631
}
2732

28-
return false;
29-
}
33+
if (!checkSearchInput()) {
34+
// We need to wait until it gets added to the DOM
35+
const observer = new MutationObserver(() => {
36+
if (checkSearchInput()) {
37+
observer.disconnect();
38+
}
39+
});
3040

31-
if (!checkSearchInput()) {
32-
// We need to wait until it gets added to the DOM
33-
const observer = new MutationObserver(() => {
34-
if (checkSearchInput()) {
35-
observer.disconnect();
36-
}
37-
});
38-
39-
observer.observe(document.body, {
40-
subtree: false,
41-
childList: true,
42-
});
41+
observer.observe(document.body, {
42+
subtree: false,
43+
childList: true,
44+
});
45+
}
4346
}
4447

4548
export {};

0 commit comments

Comments
 (0)