DEV Community

Billy Le
Billy Le

Posted on • Originally published at billyle.dev on

Keep Astro Content Collection Types in Sync on Git Commit

I'm not sure when but sometimes the type definitions in the .astro folder keep going out of sync once in a while. Instead of adding the schema definitions from my collections, it replaces them with the any type.

Here is a preview of the issue from my Git history:

schema types replaced by any type

This is an issue for me since I love working with Typescript and having that safety matters when I'm developing my site.

According to Astro's docs, it runs astro sync whenever you run astro dev or astro build so somehow during development, the types become any.

The fix

We're going to re-sync our types and make sure that we get the results we want by running the astro sync command again on a Git commit.

Add a script to your package.json called sync or whatever you like and give it the value astro sync.

{
  "scripts": {
    "sync": "astro sync"
  }
}

Enter fullscreen mode Exit fullscreen mode

You'll need to have Husky installed in your project for this to work. It's relatively simple to set up and I talked about it here.

Inside your .husky/pre-commit file, add these lines anywhere in the file. I'm using pnpm. Remember to replace "pnpm" with your package manager CLI command to run scripts.

pnpm sync
git add .astro/types.d.ts

Enter fullscreen mode Exit fullscreen mode

And that should do it! Whenever you make a new commit, the pre-commit will fire and it will sync your content collections' schemas perfectly.

Thanks for reading and have a good one! 😄

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

nextjs tutorial video

Youtube Tutorial Series 📺

So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series 👀

Watch the Youtube series

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay