Hey devs! π
Big win today! I finally got svelte-spa-router
working after struggling with it yesterday. Routing now works, and I'm feeling hyped π₯
π§ Goal for Today
- Fix the client-side routing issue with
svelte-spa-router
. - Make modular pages render correctly like
/home
,/about
, and/post/day04
. - Learn a bit more about how routing works in Svelte.
π§© The Problem (and the Fix!)
So yesterday, I installed the router package using:
bun add svelte-spa-router
β¦but nothing was rendering. No error either β just a blank page. Turns out, I wasn't setting up the route map properly.
β Created a router.js file
import Home from './pages/Home.svelte';
import About from './pages/About.svelte';
import Day04 from './pages/Day04.svelte';
const routes = {
'/home': Home,
'/about': About,
'/post/day04': Day04,
};
export default routes;
β
Updated App.svelte to use the routes
<script>
import Router from "svelte-spa-router";
import NavBar from "./pages/NavBar.svelte";
import routes from './router.js';
console.log('Active route:', routes);
</script>
<NavBar />
<main class="main-body">
<Router {routes} />
</main>
And boom β routing started working as expected! π
π‘ What I Learned
svelte-spa-router needs a properly defined route object β that was the missing link.
Having a separate router.js file makes managing routes way cleaner.
Svelte's simplicity is starting to make a lot of sense now. Loving it!
Also read an awesome article: The Programmerβs Brain β if you struggle with reading other peopleβs code, this one is for you. Highly recommend.
Every time you fix a bug or break through a blocker β it builds dev confidence. Feels good. π
live site: https://codingtheself.github.io/webdevblog/posts/day04.html
If you're also learning full-stack, exploring new tools like Svelte, or just building cool stuff β let's connect:
π¦ x.com/coding_theself
πΌ linkedin.com/in/sourav-yadav-self
π dev.to/coding_self
Top comments (1)
Did you know? Svelte's virtual DOM-free approach compiles your app to highly optimized JavaScript at build time, making it up to 40% faster at initial render than frameworks using a virtual DOM!