Skip to content

🛟 An Astro integration for typesafe URL generation and routing, ensuring you never have broken links.

License

Notifications You must be signed in to change notification settings

feelixe/astro-typesafe-routes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

74 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Astro Typesafe Routes

Astro Integration for typesafe URLs

Usage demo

Installation

  1. Add integration
npx astro add astro-typesafe-routes
  1. Start the Astro development server if it's not already running to run code generation
npm run dev

Manual Installation

  1. Install package
npm install -D astro-typesafe-routes
  1. Add integration to astro.config.mjs
import { defineConfig } from 'astro/config';
import astroTypesafeRoutes from "astro-typesafe-routes"

export default defineConfig({
    integrations: [
        astroTypesafeRoutes()
    ]
});
  1. Start the Astro development server if it's not already running to run code generation
npm run dev

Usage

Import the path function and use it as a drop-in replacement on links and anywhere else you would use a URL.

---
import { $path } from "astro-typesafe-routes";
---

<a href={$path("/posts/[postId]", { params: { postId: "1" } })}>
  Blog Post
</a>

The path function also accepts the optional fields search, hash and trailingSlash.

---
import { $path } from "astro-typesafe-routes";
---

<a
  href={$path("/posts/[postId]", {
    params: { postId: "1" },
    hash: "header",
    search: { filter: "recent" },
    trailingSlash: true
  })}
>
  Blog Post
</a>

Options

The Astro integration accepts some optional options.

  • outputPath - Path to the declaration file that will be generated (defaults to ./node_modules/astro-typesafe-routes.d.ts).
  • pagesDir - Directory of your Astro pages (defaults to ./src/pages).

Credit

Inspiration taken from yesmeck/remix-routes.

About

🛟 An Astro integration for typesafe URL generation and routing, ensuring you never have broken links.

Resources

License

Stars

Watchers

Forks

Packages

No packages published