astro/.changeset/neat-eagles-trade.md
Matthew Phillips be901dc98c
Rename getEntry to getEntryBySlug (#5893)
* Rename getEntry to getEntryBySchema

* Improve entrySlug types and return undefined

* Add changeset

* Update packages/astro/src/content/template/types.d.ts

Co-authored-by: Ben Holmes <hey@bholmes.dev>

* Update the types to accept both raw string and known value

* Add comment on the implementation not currently being O(1)

Co-authored-by: Ben Holmes <hey@bholmes.dev>
2023-01-19 08:34:27 -05:00

24 lines
627 B
Markdown

---
'astro': major
---
Move getEntry to getEntryBySlug
This change moves `getEntry` to `getEntryBySlug` and accepts a slug rather than an id.
In order to improve support in `[id].astro` routes, particularly in SSR where you do not know what the id of a collection is. Using `getEntryBySlug` instead allows you to map the `[id]` param in your route to the entry. You can use it like this:
```astro
---
import { getEntryBySlug } from 'astro:content';
const entry = await getEntryBySlug('docs', Astro.params.id);
if(!entry) {
return new Response(null, {
status: 404
});
}
---
<!-- You have an entry! Use it! -->
```