Supplying the initial contents for new files

Last modified: June 1st, 2023

When you create a file for a collection in CloudCannon, the initial contents and file extension are copied from a schema file configured for the collection.

Follow these steps to create and configure a schema for your collection.

Here's an example schema file and the required configuration for a posts collection:

schemas/post.md
copied
---
title:
author:
image:
---
cloudcannon.config.yaml
copied
collections_config:
  posts:
    schemas:
      default:
        path: schemas/post.md
cloudcannon.config.json
copied
{
  "collections_config": {
    "posts": {
      "schemas": {
        "default": {
          "path": "schemas/post.md"
        }
      }
    }
  }
}
cloudcannon.config.js
copied
module.exports = {
  collections_config: {
    posts: {
      schemas: {
        default: {
          path: "schemas/post.md"
        }
      }
    }
  }
};

Without a schema, the contents of another content file in the collection is used instead. The extension is copied as-is, and the contents are copied with some inputs cleared.

Creating a file from a schema adds a _schema field to the front matter or data to keep track of what schema the file was created from. Schemas also keep data consistent by updating the available inputs when editing existing files so it's important that this is set correctly.

The default schema is used if this field does not exist. You can change the field from _schema with the schema_key option when defining your collections.

content/posts/my-new-post.md
copied
---
_schema: default
title: My new post
author: /authors/jane
image: /images/posts/header.png
---
This is the content of my new post!

Schemas are also used to manage and keep data consistent across files in a collection, as well as providing the default add_options for creating new files.

Related Articles

Open in a new tab