#`id` vs `slug`

1 messages · Page 1 of 1 (latest)

gilded arch
#

they tend to be used interchangeably. what's the difference?

mild lichenBOT
#

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.md becomes hello-world). The id is always present and is used to query or reference the entry programmatically. With the Content Layer API (Astro v5+), id is 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 default id. The slug allows 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 a slug is provided, it will be used as the entry’s id for routing purposes Defining custom IDs.

Key differences:

  • id is always present and auto-generated unless overridden.
  • slug is optional and, when set, becomes the id for routing and querying.
  • In the latest Astro content collections, you should use id for referencing entries, and only use slug in 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 id for referencing and querying entries.
  • Use slug in frontmatter to customize the id (and thus the URL) if needed.
  • Do not define slug in 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.

gilded arch
#

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?

mild lichenBOT
#

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.

gilded arch
#

@mild lichen give me an example of using the glob() loader with generateId()

mild lichenBOT
#

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: /* ... */
});

In this example, the generateId function removes the .json extension from each file name, so JohnDoe.json would have an id of JohnDoe instead of the default slugified version Astro Content Loader API: glob() loader.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: