Convert Figma logo to code with AI

ItalyPaleAle logosvelte-spa-router

Router for SPAs using Svelte

1,598
104
1,598
21

Top Related Projects

20,482

web development, streamlined

A declarative Svelte routing library with SSR support

1,985

Automated Svelte routes

Quick Overview

Svelte-spa-router is a lightweight, client-side router for Single Page Applications (SPAs) built with Svelte. It provides a simple and efficient way to handle routing in Svelte applications, supporting both hash-based and path-based routing.

Pros

  • Easy to set up and use with a minimal learning curve
  • Supports both hash-based and path-based routing
  • Offers advanced features like nested routes, route parameters, and programmatic navigation
  • Lightweight and performant, with no external dependencies

Cons

  • Limited to client-side routing, not suitable for server-side rendering
  • May require additional configuration for deployment on some hosting platforms
  • Documentation could be more comprehensive for advanced use cases
  • Smaller community compared to some other routing solutions

Code Examples

  1. Basic route configuration:
import Home from './routes/Home.svelte'
import About from './routes/About.svelte'
import NotFound from './routes/NotFound.svelte'

const routes = {
  '/': Home,
  '/about': About,
  '*': NotFound,
}
  1. Using the Router component:
<script>
  import Router from 'svelte-spa-router'
  import { routes } from './routes'
</script>

<Router {routes} />
  1. Programmatic navigation:
import { push, pop, replace } from 'svelte-spa-router'

// Navigate to a new route
push('/about')

// Go back to the previous route
pop()

// Replace the current route
replace('/new-route')

Getting Started

  1. Install the package:

    npm install svelte-spa-router
    
  2. Define your routes in a separate file (e.g., routes.js):

    import Home from './routes/Home.svelte'
    import About from './routes/About.svelte'
    
    export const routes = {
      '/': Home,
      '/about': About,
    }
    
  3. Use the Router component in your main App.svelte file:

    <script>
      import Router from 'svelte-spa-router'
      import { routes } from './routes'
    </script>
    
    <Router {routes} />
    
  4. Create your route components (e.g., Home.svelte, About.svelte) and start building your SPA!

Competitor Comparisons

20,482

web development, streamlined

Pros of SvelteKit

  • Full-featured framework for building server-rendered and static sites
  • Built-in development server, routing, and code-splitting
  • Seamless integration with Svelte's reactivity system

Cons of SvelteKit

  • Steeper learning curve due to its comprehensive nature
  • May be overkill for simple single-page applications
  • Requires server-side setup for full functionality

Code Comparison

svelte-spa-router:

import Router from 'svelte-spa-router'
import Home from './routes/Home.svelte'
import About from './routes/About.svelte'

const routes = {
  '/': Home,
  '/about': About
}

SvelteKit:

// src/routes/+page.svelte
<h1>Welcome to SvelteKit</h1>

// src/routes/about/+page.svelte
<h1>About Page</h1>

// File-based routing, no explicit configuration needed

svelte-spa-router is a lightweight client-side router for Svelte applications, ideal for simple single-page apps. It offers easy setup and minimal overhead.

SvelteKit, on the other hand, is a full-fledged framework that provides a more comprehensive solution for building Svelte applications. It includes server-side rendering, static site generation, and more advanced routing capabilities.

Choose svelte-spa-router for simple SPAs with client-side routing, and SvelteKit for more complex applications that require server-side rendering or static site generation.

A declarative Svelte routing library with SSR support

Pros of svelte-routing

  • Supports nested routes, allowing for more complex routing structures
  • Provides a Link component for easy navigation between routes
  • Offers a navigate function for programmatic navigation

Cons of svelte-routing

  • Less actively maintained compared to svelte-spa-router
  • Fewer stars and contributors on GitHub
  • Limited documentation and examples available

Code Comparison

svelte-routing:

import { Router, Link, Route } from "svelte-routing";

<Router>
  <nav>
    <Link to="/">Home</Link>
    <Link to="/about">About</Link>
  </nav>
  <Route path="/" component={Home} />
  <Route path="/about" component={About} />
</Router>

svelte-spa-router:

import Router from 'svelte-spa-router'
import { link } from 'svelte-spa-router'

const routes = {
  '/': Home,
  '/about': About
}

<Router {routes} />
<a href="/" use:link>Home</a>
<a href="/about" use:link>About</a>

Both libraries provide routing solutions for Svelte applications, but they differ in their approach and feature set. svelte-routing offers a more React-like syntax with nested routes support, while svelte-spa-router provides a simpler API with better performance for single-page applications. The choice between the two depends on the specific needs of your project and personal preference.

1,985

Automated Svelte routes

Pros of Routify

  • Automatic route generation based on file structure
  • Built-in support for nested layouts and route transitions
  • More comprehensive documentation and examples

Cons of Routify

  • Steeper learning curve due to more advanced features
  • Potentially more complex setup for simple projects
  • Larger bundle size compared to svelte-spa-router

Code Comparison

Routify:

<script>
  import { Router } from "@roxi/routify";
  import { routes } from "../.routify/routes";
</script>

<Router {routes} />

svelte-spa-router:

<script>
  import Router from 'svelte-spa-router';
  import { routes } from './routes';
</script>

<Router {routes} />

Both libraries offer similar basic usage, but Routify's file-based routing system can lead to more concise route definitions. svelte-spa-router requires manual route configuration, which can be more explicit but potentially more verbose for larger applications.

Routify provides a more feature-rich solution with automatic route generation and nested layouts, making it suitable for complex applications. However, this comes at the cost of a steeper learning curve and potentially larger bundle size.

svelte-spa-router offers a simpler, lightweight solution that may be preferable for smaller projects or developers who prefer more manual control over their routing configuration.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

svelte-spa-router: hash-based router for Svelte 5

npm

📚 Read the docs

This package is a router for Svelte 5 applications, specifically optimized for Single Page Applications (SPA).

Main features:

  • Leverages hash-based routing, which is optimal for SPAs and doesn't require any server-side processing
  • Insanely simple to use, and has a minimal footprint
  • Uses the tiny regexparam for parsing routes, with support for parameters (e.g. /book/:id?) and more

This package is released under MIT license.

For support for Svelte 3 and 4, please use the (v4 branch)

Video

"So you want to pick a router?" talk by @ItalyPaleAle at Svelte Summit 2020. Includes an explanation of the two kinds of routers and a demo of svelte-spa-router.
(Click on the cover image to play the video on YouTube)

Click to play video: 'So you want to pick a router?'

Hash-based routing

With hash-based routing, navigation is possible thanks to storing the current view in the part of the URL after #, called "hash" or "fragment".

For example, if your SPA is in a static file called index.html, your URLs for navigating within the app look something like index.html#/profile, index.html#/book/42, etc. (The index.html part can usually be omitted for the index file, so you can just create URLs that look like http://example.com/#/profile).

When I created this component, other routers for Svelte apps implemented navigation using the HTML5 history API. While those URLs look nicer (e.g. you can actually navigate to http://example.com/profile), they are not ideal for static Single Page Applications. In order for users to be able to share links or even just refresh the page, you are required to have a server on the backend processing the request, and building fully-static apps is much harder as a consequence.

Hash-based routing is simpler, works well even without a server, and it's generally better suited for static SPAs, especially when SEO isn't a concern, as is the case when the app requires authentication. Many popular apps use hash-based routing, including GMail!

Sample code

Check out the code in the examples folder for some usage examples.

To run the samples, clone the repository, build the package, then start a sample:

# From the repo root: install deps and build the package
pnpm install
pnpm run build

# Navigate to a sample
cd examples/…
# For example
cd examples/basic-routing

# Start a development server
pnpm install
pnpm run dev

The sample will be running at http://localhost:5173

Supported browsers

svelte-spa-router aims to support modern browsers, including recent versions of:

  • Chrome
  • Edge ("traditional" and Chromium-based)
  • Firefox
  • Safari

Support for Internet Explorer is not a goal for this project. Some users have reportedly been able to use svelte-spa-router with IE11 after transpilation (e.g. with Babel), but this is not guaranteed.

Documentation

All documentation lives on the website.

Quick links:

NPM DownloadsLast 30 Days