Your own answer seems quite correct to me. Some comments:
Here's how I would create the collection (very similar to your solution):
config.addCollection('tagsList', (collectionApi) => {
const tagsSet = new Set()
collectionApi.getAll().forEach((item) => {
if (!item.data.tags) return
item.data.tags.forEach((tag) => tagsSet.add(tag))
})
return tagsSet
})
Usage in templates would be the same.
Bonus: with filtering (excludes tags "foo" and "bar") and alphabetical sorting:
config.addCollection('tagsList', (collectionApi) => {
const tagsSet = new Set()
collectionApi.getAll().forEach((item) => {
if (!item.data.tags) return
item.data.tags
.filter((tag) => !['foo', 'bar'].includes(tag))
.forEach((tag) => tagsSet.add(tag))
})
return [...tagsSet].sort((a, b) => b.localeCompare(a))
})