Essential CSS
JavaScript Roadmap
Learn JavaScript
Mastering Web Typography
Practical Astro
Understanding Async JS
Unorthodox Tailwind
API Masterclass
Build and Deploy
The Beginner Freelancer
Real World Node
Profile Billing Logout
Practical Astro
Lesson Plan
  1. Content Systems
    1. Introduction
    2. An import alias
    3. Using Markdown
    4. Markdown extended
    5. Markdown component
    6. Markdown component in frameworks
    7. Content collections made easy
    8. Basic blogs
    9. Blogs made easy
    10. Prev/Next navigation
    11. Pagination made easy
    12. Better tag pages
    13. Paginated tag pages
    14. Markdown layouts
    15. Partials
    16. JS over JSON Data
    17. Dynamic 404 pages
    18. Putting everything together
    19. Other useful systems
  2. Image Systems
    1. Introduction
    2. Images are complicated because of this
    3. Astro's Image component
    4. Astro's Picture component
    5. Improvements over Astro's Image and Picture Components
    6. Images in Markdown and MDX
    7. Images in framework components
    8. Background images
    9. Art Direction with Astro
    10. Videos without layout shifts
    11. GIF Replacements and background video
    12. SVGs and icons
    13. SVG backgrounds
    14. Adding noise to images
    15. Asset caching on GitHub Actions
    16. Putting everything together
    17. Other useful systems

Partials

3m:26s

Now that we’re done talking about Markdown, content collections, and all the basic stuff people associate with static site content… it’s time to turn our attention to partials.

A partial is like a component that doesn’t have properties. So you can think of splitting a long page into multiple parts, and each part can live in one partial, which makes organization a lot easier.

This is one of the best things I’ve created for handling long pages.

---
import { Partial } from '@splendidlabz/astro'
---
<Partial path="first-section" />
<Partial path="second-section" />
<Partial path="third-section" />
<Partial path="fourth-section" />
<!-- and so on ... -->

This is not Astro’s page partials

Astro has a feature called page partials where you export a partial variable. They need to be used with a frontend library like htmx.

---
// Declaring an Astro page partial
export const partial = true
---
<!-- Content goes here -->
<!-- Using an Astro page partial -->
<button
hx-post="/partials/clicked/"
hx-trigger="click"
hx-target="#parent-div"
hx-swap="innerHTML"
>
...
</button>

I don’t use Astro page partials because I don’t see a need to complicate things by including yet another library. I also don’t like the fact that it uses JavaScript to pull content when there is no real need for an async operation.

Components vs Partials

The way I am thinking about partials is very much similar to how one would think of components. There are two major differences between them.

First: partials cannot use client directives (because they’re not components).

Second: it is much easier to import a partial because we just have to import the Partial component and specify the path for each partial. Here’s an example of this difference:

Using Components
---
import ComponentOne from '@/components/ComponentOne.astro'
import ComponentTwo from '@/components/ComponentTwo.astro'
import ComponentThree from '@/components/ComponentThree.astro'
---
<ComponentOne />
<ComponentTwo />
<ComponentThree />
Using Partials
---
import { Partial } from '@splendidlabz/astro'
---
<Partial path="component-one" />
<Partial path="component-two" />
<Partial path="component-three" />

Using Partials

To use the partials components that I have created, you need to put all your partials in src/content/partials/.

Terminal window
src/content/partials/
└── partial-one.astro
└── partial-two.astro

Nested partials are supported so you can do this too.

Terminal window
src/content/partials/
└── practical-astro
└── intro.astro
└── second-section.astro
└── third-section.astro

To use a partial, you provide a path to the partial, relative to the partials folder, excluding the extension.

<Partial path="practical-astro/intro" />
<Partial path="practical-astro/second-section" />
<Partial path="practical-astro/third-section" />

Usage with md and mdx files

Partials support markdown files as well. But you need to add them to a content collection to use them.

src/content.config.js
export const partials = defineCollection({
loader: glob({
base: './src/content/partials',
pattern: '**/*.{md,mdx}',
}),
})
export const collections = {
partials,
/* Other collections ... */
}

Then use the partials like normal.

{/* Works even if intro is intro.mdx */}
<Partial path="practical-astro/intro" />

Naming patterns

I’ve realized it’s a very useful pattern to order file names with numbers, so I built that feature into Partial as well. This means you can name your files with 01.name.astro, 02.name.astro so you have an idea what the order is.

Here’s an example where I used this for Practical Astro’s course page.

You can omit the numbering when retrieving the partials. This makes it a lot easier to keep things organized. Here’s a usage example for the same course page.

<Partial path="practical-astro/intro" />
<Partial path="practical-astro/inside" />
<Partial path="practical-astro/save-yourself" />
<Partial path="practical-astro/author" />
<Partial path="practical-astro/yours" />
<Partial path="practical-astro/faq" />
<Partial path="practical-astro/final" />

Code snippet to copy-paste

Using a partial

---
import { Partial } from '@splendidlabz/astro'
---
<Partial path="basename" />

Linking to nested partials

---
import { Partial } from '@splendidlabz/astro'
---
<Partial path="folder/basename" />

Using MDX partials

For this you need to define a partials collection. Once you have this, use it as if you’re using a .astro partial.

src/content.config.js
export const partials = defineCollection({
loader: glob({
base: './src/content/partials',
pattern: '**/*.{md,mdx}',
}),
})
export const collections = {
partials,
/* Other collections ... */
}