Reacting to URLs
We will now replace the go
function of the Router
go: (route, addToHistory=true) => {
if (addToHistory) {
history.pushState({ route }, '', route);
}
let pageElement = null;
switch (route) {
case "/":
pageElement = document.createElement("h1");
pageElement.textContent = "Menu";
break;
case "/order":
pageElement = document.createElement("h1");
pageElement.textContent = "Menu";
break;
default:
if (route.startsWith("/product-")) {
pageElement = document.createElement("h1");
pageElement.textContent = "Details";
pageElement.dataset.productId = route.substring(route.lastIndexOf("-")+1);
}
break;
}
if (pageElement) {
document.querySelector("main").innerHTML = "";
document.querySelector("main").appendChild(pageElement);
}
window.scrollX = 0;
}