astro-typesafe-routes
v2.0.7
Published
Astro Integration for typesafe URLs
Downloads
5
Readme
Installation
- Add integration
npx astro add astro-typesafe-routes
- Start the Astro development server if it's not already running to run code generation
npm run dev
Manual Installation
- Install package
npm install -D astro-typesafe-routes
- Add integration to
astro.config.mjs
import { defineConfig } from 'astro/config';
import astroTypesafeRoutes from "astro-typesafe-routes"
export default defineConfig({
integrations: [
astroTypesafeRoutes()
]
});
- 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.