Skip to content

Commit 4b5c5a5

Browse files
committed
avoid injecting duplicate instances into the 404 page
1 parent 2638edd commit 4b5c5a5

1 file changed

Lines changed: 52 additions & 39 deletions

File tree

src/index.ts

Lines changed: 52 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,59 @@
11
import App from "./App.svelte";
22
import { testPaths } from "./check-url";
3-
import { CONTAINER_PARENT_SELECTOR } from "./constants";
4-
5-
const parent = document.querySelector(CONTAINER_PARENT_SELECTOR);
6-
7-
const segments = [
8-
{
9-
content: "github.com",
10-
left: 0,
11-
right: 0,
12-
hovered: false,
13-
},
14-
...window.location.pathname
15-
.split("/")
16-
.filter((segment) => segment !== "")
17-
.map((segment) => ({
18-
content: segment,
3+
import { CLASS_PREFIX, CONTAINER_PARENT_SELECTOR } from "./constants";
4+
5+
const mainClassname = CLASS_PREFIX + "main";
6+
7+
const main = () => {
8+
if (document.querySelector("." + mainClassname)) {
9+
// Already injected content, nothing to do. This can occur when navigating
10+
// with the browser back button to a 404 page.
11+
return;
12+
}
13+
14+
const div = document.createElement("div");
15+
div.classList.add(mainClassname);
16+
17+
const parent = document.querySelector(CONTAINER_PARENT_SELECTOR);
18+
parent.insertAdjacentElement("afterend", div);
19+
20+
const segments = [
21+
{
22+
content: "github.com",
1923
left: 0,
2024
right: 0,
2125
hovered: false,
22-
})),
23-
];
24-
25-
const div = document.createElement("div");
26-
parent.insertAdjacentElement("afterend", div);
27-
28-
const app = new App({
29-
target: div,
30-
props: { segments, status: "loading", index: 1 },
31-
});
32-
33-
const onPathTested = (index: number, ok: boolean) => {
34-
if (ok) {
35-
// Given index was ok, move to the next segment. Shifted by one extra due to
36-
// `github.com` prefix.
37-
app.$set({ index: index + 2, status: "loading" });
38-
} else {
39-
// Given index returned 404, mark it as errored. Shifted by one extra due to
40-
// `github.com` prefix.
41-
app.$set({ index: index + 1, status: "error" });
42-
}
26+
},
27+
...window.location.pathname
28+
.split("/")
29+
.filter((segment) => segment !== "")
30+
.map((segment) => ({
31+
content: segment,
32+
left: 0,
33+
right: 0,
34+
hovered: false,
35+
})),
36+
];
37+
38+
const app = new App({
39+
target: div,
40+
props: { segments, status: "loading", index: 1 },
41+
});
42+
43+
const onPathTested = (index: number, ok: boolean) => {
44+
if (ok) {
45+
// Given index was ok, move to the next segment. Shifted by one extra due to
46+
// `github.com` prefix.
47+
app.$set({ index: index + 2, status: "loading" });
48+
} else {
49+
// Given index returned 404, mark it as errored. Shifted by one extra due to
50+
// `github.com` prefix.
51+
app.$set({ index: index + 1, status: "error" });
52+
}
53+
};
54+
55+
// TODO catch errors here
56+
testPaths(window.location.pathname, onPathTested);
4357
};
4458

45-
// TODO catch errors here
46-
testPaths(window.location.pathname, onPathTested);
59+
main();

0 commit comments

Comments
 (0)