Skip to content

Commit 592aaf2

Browse files
committed
updated hash-based routing to maintain article viewing after refresh
1 parent 247996f commit 592aaf2

File tree

1 file changed

+43
-19
lines changed

1 file changed

+43
-19
lines changed

index.html

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,33 +26,38 @@ <h1>Articles</h1>
2626
'articles/article1.js',
2727
'articles/article2.js',
2828
'articles/article3.js',
29-
// Add more files here
3029
];
3130

3231
const mainPage = document.getElementById("main-page");
3332
const articlePage = document.getElementById("article-page");
3433
const articleContainer = document.getElementById("article-container");
3534
const articleGrid = document.getElementById("article-grid");
35+
const backBtn = document.querySelector(".back-btn");
3636

3737
function goBack() {
38+
location.hash = "";
3839
articlePage.style.display = "none";
3940
mainPage.style.display = "block";
4041
articleContainer.innerHTML = "";
4142
}
4243

43-
document.querySelector(".back-btn").onclick = goBack;
44+
backBtn.onclick = goBack;
4445

4546
async function loadArticle(file) {
4647
mainPage.style.display = "none";
4748
articlePage.style.display = "block";
4849

49-
const module = await import(`./${file}`);
50-
const { layout, content, runDemo } = module;
50+
try {
51+
const module = await import(`./${file}?v=${Date.now()}`);
52+
const { layout, content, runDemo } = module;
5153

52-
if (layout === "simple") {
53-
renderSimple(content);
54-
} else if (layout === "split") {
55-
renderSplit(content, runDemo);
54+
if (layout === "simple") {
55+
renderSimple(content);
56+
} else if (layout === "split") {
57+
renderSplit(content, runDemo);
58+
}
59+
} catch (err) {
60+
articleContainer.innerHTML = `<p>Error loading article.</p>`;
5661
}
5762
}
5863

@@ -96,18 +101,37 @@ <h1>Articles</h1>
96101
};
97102
}
98103

99-
for (const file of articleFiles) {
100-
const meta = await loadMetadata(file);
101-
const card = document.createElement("div");
102-
card.className = "card";
103-
card.dataset.file = meta.file;
104-
card.innerHTML = `
105-
<img src="${meta.image}" alt="${meta.title}" />
106-
<h2>${meta.title}</h2>
107-
`;
108-
card.onclick = () => loadArticle(meta.file);
109-
articleGrid.appendChild(card);
104+
async function buildGrid() {
105+
for (const file of articleFiles) {
106+
const meta = await loadMetadata(file);
107+
const card = document.createElement("div");
108+
card.className = "card";
109+
card.dataset.file = meta.file;
110+
card.innerHTML = `
111+
<img src="${meta.image}" alt="${meta.title}" />
112+
<h2>${meta.title}</h2>
113+
`;
114+
card.onclick = () => {
115+
location.hash = meta.file;
116+
};
117+
articleGrid.appendChild(card);
118+
}
119+
}
120+
121+
async function checkHash() {
122+
const file = decodeURIComponent(location.hash.slice(1));
123+
if (articleFiles.includes(file)) {
124+
await loadArticle(file);
125+
} else {
126+
goBack();
127+
}
110128
}
129+
130+
window.addEventListener("hashchange", checkHash);
131+
window.addEventListener("DOMContentLoaded", async () => {
132+
await buildGrid();
133+
checkHash();
134+
});
111135
</script>
112136
</body>
113137
</html>

0 commit comments

Comments
 (0)