Co-located TanStack Router, TanStack Query, and Shadcn Sidebar in Astro
Table of Contents
- Introduction
- Integrating TanStack Router
- Setting Up TanStack Query
- Adding Shadcn Sidebar for Navigation
- Conclusion
- References
Introduction
As of writing this article on November 3, 2024, I’m using the latest versions of the following packages:
Before diving into the integration process, let’s briefly overview the tools we’ll be using:
- Astro: A modern framework for building fast, content-focused websites.
- TanStack Router: A powerful routing library that allows co-locating routes with components for better organization.
- TanStack Query: An asyc-state management library that simplifies data management with features like caching and synchronization.
- Shadcn Sidebar: A customizable sidebar component from Shadcn’s UI library for seamless navigation.
Integrating TanStack Router
TanStack Router offers a co-located routing approach, allowing you to define routes alongside your components. This promotes better organization and scalability.
Installation
First, install TanStack Router and its dependencies:
Configuration
TL;DR To create a URL like https://goastro.website/dashboard, organize your project folders as illustrated in the screenshot below:
Key Points:
-
Must to have requirement from Tanstack Router: The red part in screenshot must be same in this case the “/dashboard” route.
-
Better to have requirement from Astro: The yellow part can be anything, but if you want the url show at the address bar to be consistent, you should create the folder named “dashboard”.
-
Modify astro.config.mjs as following:
Click to expand the code
Setting Up TanStack Query
Click to expand the example code
Adding Shadcn Sidebar for Navigation
Shadcn provides a customizable and accessible sidebar component that integrates seamlessly with your Astro application.
Installation
Install the Shadcn Sidebar component:
Configuration
Important: Modify components/ui/sidebar.tsx
by adding the type
keyword before VariantProps
to prevent rendering issues in Astro.
Common Error:
You will encounter the following error if the type
keyword is missing at the time of writing:
Adding type
ensures proper type imports and prevents such errors.
Conclusion
By implementing the methods discussed in this article, I successfully built my website with an integrated toolbox within my blog. This toolbox includes handy utilities like a Datetime Converter and a GitHub Raw Link Converter. If you’re interested in exploring these tools, simply click on the Toolbox link in the navigation bar. You’ll find detailed instructions and interactive demonstrations that showcase how each tool works.
Integrating TanStack Router, TanStack Query, and Shadcn’s Sidebar into your Astro project not only enhances the functionality of your website but also improves the overall user experience. Whether you’re developing a personal blog, a corporate site, or a complex web application, this approach provides a scalable and maintainable foundation for your projects.
References
Happy Coding!