Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom metadata on schema.collection #385

Open
willthomson opened this issue Jul 3, 2024 · 1 comment
Open

Allow custom metadata on schema.collection #385

willthomson opened this issue Jul 3, 2024 · 1 comment
Assignees
Labels
enhancement Feature request

Comments

@willthomson
Copy link
Member

Registry, uses collections, where each collection is a separate microsite (some with several pages), most of which are showcasing a TLD, for example get.dad, get.ing etc. Each of these TLD sites (collections) has some metadata associated with it, for example:

  • TLD
  • Site root URL
  • Initiative logo (URL and alt)

I suggest adding a new optional meta option on the collection schema, where key/value pairs can be created arbitrarily like so:

export default createCollection({
  name: 'Get.ing pages',
  description: 'Landing pages for get.ing',
  url: '/get.ing/[...slug]',
  meta: {
    tld: 'ing',
    rootUrl: 'https://get.ing',
    initiativeLogo: {
      src: 'https://lh3.googleusercontent.com/hElmA3OIiqvwRaRxqSZQ5G6pVdzzk6nUf0lGcCYJCxQVMc2pHz1RVKqgudAYklWqVg8HzixOs40SEuYOI5KBvUgFLiEEZKTgKDtX2XA',
      alt: 'dot ing logo',
    },
  },
});

This could then either be exposed to the Page and be looked up through usePage(), or the collection could get its own useCollection() function.

@stevenle
Copy link
Member

stevenle commented Jul 3, 2024

I think there are a couple of ways this can be handled:

  1. Extend the root's Collection type in your project, e.g.
// utils/schema.ts

export type SiteMetadata {
  tld: string;
  rootUrl: string;
  logo: {src: string; alt: string};
}

export type CollectionWithMetadata = schemas.Collection & SiteMetadata;

export defineCollection = (collection: CollectionWithMetadata) => collection;
// collections/Foo.schema.ts

import {defineCollection} from '@/utils/schema.ts';

export default defineCollection({
  ...
});
  1. If you just need to grab metadata programmatically you can also use import.meta.glob and export the metadata as a separate variable
// collections/Foo.schema.ts

export const METADATA = {...};

export default defineCollection({...});

And with import.meta.glob:

const collectionModules = import.meta.glob('/collections/*.schema.ts', {eager: true});
const allMetadata = collectionModules.map((module) => module.METADATA);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Feature request
Projects
None yet
Development

No branches or pull requests

2 participants