Renommer lib/ en state/ pour plus de clarté
All checks were successful
Deploy / Deploy to Production (push) Successful in 13s
All checks were successful
Deploy / Deploy to Production (push) Successful in 13s
- Renommage du dossier src/lib/ en src/state/ - Mise à jour de l'alias @lib vers @state dans vite.config.js - Suppression de l'alias @stores devenu obsolète - Mise à jour de tous les imports dans les composants et vues Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
a12c2df8f9
commit
9ab6344835
28 changed files with 383 additions and 179 deletions
96
src/state/router.js
Normal file
96
src/state/router.js
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
import navaid from "navaid";
|
||||
import { page } from "./page.svelte";
|
||||
import { navigation } from "./navigation.svelte";
|
||||
import { site } from "./site.svelte";
|
||||
import { locale } from "./locale.svelte";
|
||||
|
||||
export const router = navaid("/", () => {
|
||||
// Default handler
|
||||
});
|
||||
|
||||
async function loadPage(path) {
|
||||
navigation.setLoading(true);
|
||||
page.setLoading(true);
|
||||
|
||||
try {
|
||||
const response = await fetch(`${path}.json`);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to load page: ${response.status}`);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (data.site) {
|
||||
site.set(data.site);
|
||||
locale.initialize(data.site.language, data.site.languages);
|
||||
}
|
||||
|
||||
page.set({
|
||||
data,
|
||||
template: data.template || "default",
|
||||
url: path,
|
||||
loading: false,
|
||||
error: null,
|
||||
});
|
||||
|
||||
window.scrollTo(0, 0);
|
||||
} catch (error) {
|
||||
console.error("Failed to load page:", error);
|
||||
page.setError(error);
|
||||
} finally {
|
||||
navigation.setLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
// Route handlers
|
||||
router
|
||||
.on("/", () => loadPage("/home"))
|
||||
.on("/expertise", () => loadPage("/expertise"))
|
||||
.on("/portfolio", () => loadPage("/portfolio"))
|
||||
.on("/portfolio/:slug", ({ slug }) => loadPage(`/portfolio/${slug}`))
|
||||
.on("/jouer", () => loadPage("/jouer"))
|
||||
.on("/jouer/:slug", ({ slug }) => loadPage(`/jouer/${slug}`))
|
||||
.on("/a-propos", () => loadPage("/a-propos"))
|
||||
.on("/blog", () => loadPage("/blog"))
|
||||
.on("/blog/:slug", ({ slug }) => loadPage(`/blog/${slug}`))
|
||||
.on("*", (params) => {
|
||||
// Fallback for other routes
|
||||
loadPage(window.location.pathname);
|
||||
});
|
||||
|
||||
export function initRouter() {
|
||||
// Load initial page data
|
||||
loadPage(window.location.pathname);
|
||||
|
||||
// Start listening to route changes
|
||||
router.listen();
|
||||
|
||||
// Intercept internal link clicks
|
||||
document.addEventListener("click", (e) => {
|
||||
const link = e.target.closest("a");
|
||||
if (!link) return;
|
||||
|
||||
const url = new URL(link.href, window.location.origin);
|
||||
|
||||
// Only intercept same-origin links without target attribute
|
||||
if (
|
||||
url.origin === window.location.origin &&
|
||||
!link.target &&
|
||||
!link.hasAttribute("download")
|
||||
) {
|
||||
e.preventDefault();
|
||||
navigateTo(url.pathname);
|
||||
}
|
||||
});
|
||||
|
||||
// Handle browser back/forward
|
||||
window.addEventListener("popstate", () => {
|
||||
loadPage(window.location.pathname);
|
||||
});
|
||||
}
|
||||
|
||||
export function navigateTo(path) {
|
||||
window.history.pushState({}, "", path);
|
||||
loadPage(path);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue