#From within a component, how do I get the collection name of the page being rendered?

3 messages · Page 1 of 1 (latest)

plucky wind
#

Instead of having to write const query = await getCollection('docs');

I'd like to find the collection name during render, so I can get the approriate collection via getCollection.

eternal wolfBOT
#

Hi @plucky wind. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

I'm sorry, but the knowledge sources do not provide a built-in way to dynamically determine the collection name of the current page from within a component at render time. The documented usage of getCollection() requires you to explicitly provide the collection name as a string argument, such as getCollection('docs') or getCollection('blog') Content Collections API Reference.

There is no mention in the official documentation of a global variable, context property, or API that exposes the current collection name during render. All examples and references require you to know and specify the collection name ahead of time Querying Collections.

If you need to make your component generic, you could pass the collection name as a prop from the parent or page, but this pattern is not described in the knowledge sources.

If you have a specific use case, you may need to architect your data flow to provide the collection name where needed, as there is no automatic way to infer it during render according to the current documentation.

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