#`id` vs `slug`
1 messages · Page 1 of 1 (latest)
Hi @gilded arch. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
In Astro, id and slug are related but distinct concepts, especially in the context of content collections:
-
id: This is the unique identifier for a content entry, automatically generated from the filename in your collection (e.g.,hello-world.mdbecomeshello-world). Theidis always present and is used to query or reference the entry programmatically. With the Content Layer API (Astro v5+),idis the primary way to reference entries, and you should use it for generating routes and querying content entries Defining custom IDs. -
slug: This is an optional property you can add to your content entry’s frontmatter to override the defaultid. Theslugallows you to customize the URL path for an entry, including support for slashes to create nested routes (e.g.,slug: my-custom-id/supports/slashes). If aslugis provided, it will be used as the entry’sidfor routing purposes Defining custom IDs.
Key differences:
idis always present and auto-generated unless overridden.slugis optional and, when set, becomes theidfor routing and querying.- In the latest Astro content collections, you should use
idfor referencing entries, and only useslugin frontmatter if you need a custom or nested URL.
Important: In collection schemas, you should not define a slug field, as it is reserved for slug generation. Use it only in frontmatter or data objects, not in the schema definition Content Schema should not contain slug.
Summary:
- Use
idfor referencing and querying entries. - Use
slugin frontmatter to customize theid(and thus the URL) if needed. - Do not define
slugin your collection schema.
For more details, see the Astro documentation on Defining custom IDs and Content Schema should not contain slug.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@gilded arch Kapa.ai is still learning and improving, please let me know how I did by reacting below.
what does error [glob-loader] No entry type found for 10-19-concepts/index.mdx mean?
@mild lichen what does error [glob-loader] No entry type found for 10-19-concepts/index.mdx mean?
Let me check the knowledge base for you again. One moment! :books:
I'm sorry, but the knowledge sources provided do not contain any information about the error message [glob-loader] No entry type found for 10-19-concepts/index.mdx. There is no explicit mention or explanation of this error in the referenced Astro documentation. If you need more details, you may want to consult the official Astro documentation or community channels for further assistance.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@gilded arch Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@mild lichen give me an example of using the glob() loader with generateId()
Let me check the knowledge base for you again. One moment! :books:
Certainly! Here’s an example of using the glob() loader with a custom generateId() function in your src/content.config.ts file. This setup allows you to control how the id for each entry is generated:
import { defineCollection } from 'astro:content';
import { glob } from 'astro/loaders';
const authors = defineCollection({
loader: glob({
pattern: '**/*.json',
base: "./src/data/authors",
generateId: ({ entry }) => entry.replace(/\.json$/, ''), // Custom ID: remove .json extension
}),
schema: /* ... */
});