Crash Course into TypeScript for content from headless CMS

Rate this content
Bookmark

In this workshop, I’ll first show you how to create a new project in a headless CMS, fill it with data, and use the content in your project. Then, we’ll spend the rest of time in code, we will:

- Generate strongly typed models and structure for the fetched content.

- Use the content in components

- Resolve content from rich text fields into React components

- Touch on deployment pipelines and possibilities for discovering content-related issues before hitting production

Ondrej Polesny
Ondrej Polesny
118 min
24 May, 2022

Comments

Sign in or register to post your comment.

Video Summary and Transcription

This workshop provides an introduction to working with content in TypeScript using CMS. It covers topics such as generating TypeScript models, implementing Next.js, resolving rich text fields, and improving type safety and code maintainability. The workshop also discusses importing content packages, troubleshooting, and automatic model generation. It emphasizes the importance of generating strongly typed models for content and demonstrates how to resolve rich text elements into React components.

1. Introduction to CMS and Workshop Overview

Short description:

Hello everyone! I'm Andrey, a developer evangelist for Contentby. Today, I'll show you how to work with content in TypeScript using CMS. We'll cover topics like Strongly Attacked Models, generating content models in TypeScript, implementing Next.js, resolving rich text fields, and more. We'll also discuss deployment pipelines and content verification. If you're interested, let's get started! You can either watch or join the hands-on workshop. I'll provide a link to a repository you can clone, and I'll guide you through the implementation. No registration is required, but if you want to use the headless CMS, there's a free trial available. Let's dive in!

So, hello, everyone, once again. I hope that the majority of attendees who want to join here today are here. Let me first introduce myself and I hope that you can all see my screen, so I'll show you the second slide of the presentation where I'll outline what we're going to go through today. I'm Andrey, I work as a developer evangelist for Contentby. I'm going to show you a bit of CMS. I want to emphasize that I'm not doing a product pitch today, I'm just trying to show you how we can work with content in TypeScript, and hopefully the principles that I'll show you are applicable to other CMSs, provided that they have similar tools, which I hope they do.

I used to work for a customer success department for quite some time, so I think that I have an experience with how people are struggling with what happens when their project grows, and when their content models change. I don't know for all developers, but I would say for most of us. You know that when you're setting up your models, your databases, everything at the start of your project, they never stay the same. You always end up changing a lot of things, sometimes even replatforming your whole project, so I'll show you how the Strongly Attacked Models can help you make that more seamless process. It will never be seamless, but we can at least try to be optimistic about the change.

Alright, so let me switch here. Today, the workshop is estimated to be three hours. I will try to be a bit faster than that. We'll see how it goes. What I wanna do at the start is we actually have an import package that I'm gonna show you how to import it into a new project. Then I'm gonna show you how we can generate the content model in code, in TypeScript. So, I'm gonna show you the content in the CMS. I'm gonna show you the Next.js implementation. I've prepared a bundle for you that you can get from clone, from GitHub. We're gonna generate the TypeScript models. And then we're gonna implement one additional page in the Next.js implementation that will fetch content, stronger type content. And we're gonna resolve rich text fields into React components. So, we're gonna keep the stronger type models with the content. And we're gonna resolve even rich text so that marketers can work on their landing pages without your help, and you can keep having strong types in your implementation. At the end, we can also discuss the deployment pipelines and content verification discussion. This is based on our own experience when we've had a lot of issues and inconsistencies between website implementations and what was actually in the CMS. And when the CMS content model changes, your website and your website is not prepared for the change. It can end up with a nasty error. So I can tell you what we are currently working on, and what are some of the ways how we can avoid these errors. Cool.

So, if this sounds good, we can move to the first step of the workshop. Now, I aim to do this as a hands-on workshop, so you can, if you have a Visual Studio and an Internet connection, that's all you really need. But if you just want to watch, feel free to do that. If you want to use my project from the content, from the headless CMS, that's also possible. I'll share the project ID with you, so you don't have to work with content. You can stick to your Visual Studio code and work with the content there. So no need to register. If you don't want that, if you do, you can do that too. So this is a link for a repository that you can clone. Let me actually go there for a second. It should be public, so you should be able to access it. The only thing you need to do is do a git clone in this repository. Maybe someone can put it in the chat, or I can do that? Yes. Oh, thank you. Thank you very much. So feel free to clone that. I'm going to go through the implementation in a second. The only thing I wanted to tell you is that this is a Next.js-based site. Next.js is the most used framework for most of our clients. It gives you a lot of flexibility and lots of new features while keeping the implementation maintainable. We use it on our own site, so it's the easiest stepping stone for me as well. So when you clone this, we can take a look at it in VS Code. I'm going to open that here. I will make it bigger a bit. Let me know if you can read it okay. Otherwise I can make it even bigger. When you download or clone the project, there is one important thing, and that is the content workshop import package. If you don't want to register for content, and just want to use My Project, that's fine. If you do want to register for content, then there is a trial. If you go to content.ai. Here you can get a free developer plan. There is a link for a sign-up page. You can sign up for the trial. And I'm going to show you how to import content into a blank project. If you don't want to register, that's fine. I'm going to do that anyway. And I'm going to share the ID with you. Actually I think the project ID is here in the readme of the repo. This is the only thing you'll need for the workshop in terms of the headless CMS.

2. Importing Content Package and Verifying Content

Short description:

To import the content package into your project, go to the content template manager and follow the steps to import the package. Make sure to generate the management key in the project settings and enable the management API. Drag and drop the package into the content template manager and check the checkbox to publish language variants after import. Once the import is finished, you will have all the content in the project. You can verify the content by accessing the deliver.content.ai website and entering the project ID. Additionally, you can use the project ID to access the content items in JSON format. If you need the template manager, you can find it by googling 'content template manager' or visiting kentico.github.io/content-template-manager.

Now, let me just switch to the right project. So, when you register for content and you create a new project, you're going to see this. So this is an empty screen. There is no content here. Nothing. It's completely a blank project. And this is the ID that I'm going to be using. Yeah, you see that it's 02A. It's the same one as is here. 02A, right. So, whatever I'll do in, in the project, you can consume it using the project ID.

Now, the first thing I want to do is I want to import the content package, right? So, there is a content package content workshop import package.zip. This is something that I want to import into my content project. Now, to, oops, create a new tab. Perfect. Now, for content, there is a thing called content template manager that allows you to kind of do a backup of your project, right? You can export import, you can even, there are templates that you can use. Now, when I prepared the project, I did an export. Now, I'm going to do an import, right? The target project ID is the one that I showed you. Now, for a second, I'm just going to go into project settings and generate the management key. So, if you want to import into your own project, you're going to need to do the same, to go into project settings, API keys, and enable the management API. So, let me activate this and copy the key. So, as the management API is available to all plans, even the free developer one, yeah? And I'm going to put it here. And the last thing I need to do is actually drag and drop the package. So let me just find the right folder. That will be......this one. So here, you see that this is the Content Workshop import package, so I'm just going to drag this here. And I want to publish language variants after import, so if you're importing into your project, then do the same, just drag and drop it here. Do a project ID, content management API key, check this checkbox and prepare for import. But there shouldn't be any errors, so, there our data inconsistencies are 0, so you get a nice status report. There are no issues, so I can proceed with import of data. So this is going to take a second, but once this is finished, we will have all the content in the project, right? So you see data was successfully imported, so I'm going to close this. Oops, did not want to do that. Let me see. So this is the project. Let's switch back to content. You see now I have three content items here and I should also have some assets here, right? So we have three images, three content items, so everything is as it should be. Now, if you're wondering how are we going to get the content and you now have the project ID, right? Again, the project ID is in the README of the repository that was shared, right? This part here. Now what you can do is you can verify that the content is making it through to the CDN, right? So if you go to deliver.content.ai and put there the ID of the project, so deliver.content.ai slash the project ID and slash items, you should get a nice JSON corresponding to the content items that we currently have in the project, right? Now it's probably not going to tell you much, but you see that there is a HawkIce Rock item. There's a market responding page. So pretty much all the items that we currently have in the system. So this is the first thing that you can verify if you can access this. Brittany, if you're looking for the template manager, I'll try to get there. But what you can do is just Google for content template manager. It's going to be the first link. Otherwise, it's on kentico.github.io slash content template manager. Now what they're going to see is this screen. Now I import it into my project. For that you're going to need a project ID and the management API key. If you don't have those, that's fine. You can just use mine. The project that I already imported. So that's perfectly fine. We're not going to do much in the CMS anyway. I've already prepared the content. So feel free to use the project ID that's in the GitHub report. It's perfectly fine. Perfect. And of course, if you want to import, then we can give you a few moments. That's fine. So what do you see in the CMS is there are three items here. But let's switch back to Content Model. Now, I want to show you the site structure. In the site, you currently have three content types. The root content type, I would call it, is called the Landing Page. And the Landing Page is something that is prepared for a content editor, a marketer, you could say. It has every page that is based on this content type. We'll have a title. It will have a content and URL slag, yeah? URL slag so that we know where to place that content. Title so that we name the page somehow. And rich text will be a place where marketers can create their components, where they can create the design of the page.

QnA

Watch more workshops on topic

React, TypeScript, and TDD
React Advanced Conference 2021React Advanced Conference 2021
174 min
React, TypeScript, and TDD
Top Content
Featured WorkshopFree
Paul Everitt
Paul Everitt
ReactJS is wildly popular and thus wildly supported. TypeScript is increasingly popular, and thus increasingly supported.

The two together? Not as much. Given that they both change quickly, it's hard to find accurate learning materials.

React+TypeScript, with JetBrains IDEs? That three-part combination is the topic of this series. We'll show a little about a lot. Meaning, the key steps to getting productive, in the IDE, for React projects using TypeScript. Along the way we'll show test-driven development and emphasize tips-and-tricks in the IDE.
Best Practices and Advanced TypeScript Tips for React Developers
React Advanced Conference 2022React Advanced Conference 2022
148 min
Best Practices and Advanced TypeScript Tips for React Developers
Top Content
Featured Workshop
Maurice de Beijer
Maurice de Beijer
Are you a React developer trying to get the most benefits from TypeScript? Then this is the workshop for you.In this interactive workshop, we will start at the basics and examine the pros and cons of different ways you can declare React components using TypeScript. After that we will move to more advanced concepts where we will go beyond the strict setting of TypeScript. You will learn when to use types like any, unknown and never. We will explore the use of type predicates, guards and exhaustive checking. You will learn about the built-in mapped types as well as how to create your own new type map utilities. And we will start programming in the TypeScript type system using conditional types and type inferring.
Deep TypeScript Tips & Tricks
Node Congress 2024Node Congress 2024
83 min
Deep TypeScript Tips & Tricks
Top Content
Workshop
Josh Goldberg
Josh Goldberg
TypeScript has a powerful type system with all sorts of fancy features for representing wild and wacky JavaScript states. But the syntax to do so isn't always straightforward, and the error messages aren't always precise in telling you what's wrong. Let's dive into how many of TypeScript's more powerful features really work, what kinds of real-world problems they solve, and how to wrestle the type system into submission so you can write truly excellent TypeScript code.
Practice TypeScript Techniques Building React Server Components App
TypeScript Congress 2023TypeScript Congress 2023
131 min
Practice TypeScript Techniques Building React Server Components App
Workshop
Maurice de Beijer
Maurice de Beijer
In this hands-on workshop, Maurice will personally guide you through a series of exercises designed to empower you with a deep understanding of React Server Components and the power of TypeScript. Discover how to optimize your applications, improve performance, and unlock new possibilities.
 
During the workshop, you will:
- Maximize code maintainability and scalability with advanced TypeScript practices
- Unleash the performance benefits of React Server Components, surpassing traditional approaches
- Turbocharge your TypeScript with the power of Mapped Types
- Make your TypeScript types more secure with Opaque Types
- Explore the power of Template Literal Types when using Mapped Types
 
Maurice will virtually be by your side, offering comprehensive guidance and answering your questions as you navigate each exercise. By the end of the workshop, you'll have mastered React Server Components, armed with a newfound arsenal of TypeScript knowledge to supercharge your React applications.
 
Don't miss this opportunity to elevate your React expertise to new heights. Join our workshop and unlock the potential of React Server Components with TypeScript. Your apps will thank you.
Advanced TypeScript types for fun and reliability
TypeScript Congress 2022TypeScript Congress 2022
116 min
Advanced TypeScript types for fun and reliability
Workshop
Maurice de Beijer
Maurice de Beijer
If you're looking to get the most out of TypeScript, this workshop is for you! In this interactive workshop, we will explore the use of advanced types to improve the safety and predictability of your TypeScript code. You will learn when to use types like unknown or never. We will explore the use of type predicates, guards and exhaustive checking to make your TypeScript code more reliable both at compile and run-time. You will learn about the built-in mapped types as well as how to create your own new type map utilities. And we will start programming in the TypeScript type system using conditional types and type inferring.
Are you familiar with the basics of TypeScript and want to dive deeper? Then please join me with your laptop in this advanced and interactive workshop to learn all these topics and more.
You can find the slides, with links, here: http://theproblemsolver.nl/docs/ts-advanced-workshop.pdf
And the repository we will be using is here: https://github.com/mauricedb/ts-advanced
Mastering Node.js Test Runner
TestJS Summit 2023TestJS Summit 2023
78 min
Mastering Node.js Test Runner
Workshop
Marco Ippolito
Marco Ippolito
Node.js test runner is modern, fast, and doesn't require additional libraries, but understanding and using it well can be tricky. You will learn how to use Node.js test runner to its full potential. We'll show you how it compares to other tools, how to set it up, and how to run your tests effectively. During the workshop, we'll do exercises to help you get comfortable with filtering, using native assertions, running tests in parallel, using CLI, and more. We'll also talk about working with TypeScript, making custom reports, and code coverage.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

TypeScript and React: Secrets of a Happy Marriage
React Advanced Conference 2022React Advanced Conference 2022
21 min
TypeScript and React: Secrets of a Happy Marriage
Top Content
TypeScript and React are inseparable. What's the secret to their successful union? Quite a lot of surprisingly strange code. Learn why useRef always feels weird, how to wrangle generics in custom hooks, and how union types can transform your components.
React's Most Useful Types
React Day Berlin 2023React Day Berlin 2023
21 min
React's Most Useful Types
Top Content
We don't think of React as shipping its own types. But React's types are a core part of the framework - overseen by the React team, and co-ordinated with React's major releases.In this live coding talk, we'll look at all the types you've been missing out on. How do you get the props type from a component? How do you know what ref a component takes? Should you use React.FC? And what's the deal with JSX.Element?You'll walk away with a bunch of exciting ideas to take to your React applications, and hopefully a new appreciation for the wonders of React and TypeScript working together.
Stop Writing Your Routes
Vue.js London 2023Vue.js London 2023
30 min
Stop Writing Your Routes
The more you keep working on an application, the more complicated its routing becomes, and the easier it is to make a mistake. ""Was the route named users or was it user?"", ""Did it have an id param or was it userId?"". If only TypeScript could tell you what are the possible names and params. If only you didn't have to write a single route anymore and let a plugin do it for you. In this talk we will go through what it took to bring automatically typed routes for Vue Router.
Making Magic: Building a TypeScript-First Framework
TypeScript Congress 2023TypeScript Congress 2023
31 min
Making Magic: Building a TypeScript-First Framework
I'll dive into the internals of Nuxt to describe how we've built a TypeScript-first framework that is deeply integrated with the user's IDE and type checking setup to offer end-to-end full-stack type safety, hints for layouts, middleware and more, typed runtime configuration options and even typed routing. Plus, I'll highlight what I'm most excited about doing in the days to come and how TypeScript makes that possible not just for us but for any library author.
Full-stack & typesafe React (+Native) apps with tRPC.io
React Advanced Conference 2021React Advanced Conference 2021
6 min
Full-stack & typesafe React (+Native) apps with tRPC.io
Top Content
Why are we devs so obsessed with decoupling things that are coupled nature? tRPC is a library that replaces the need for GraphQL or REST for internal APIs. When using it, you simply write backend functions whose input and output shapes are instantly inferred in your frontend without any code generation; making writing API schemas a thing of the past. It's lightweight, not tied to React, HTTP-cacheable, and can be incrementally adopted. In this talk, I'll give a glimpse of the DX you can get from tRPC and how (and why) to get started.
Faster TypeScript builds with --isolatedDeclarations
TypeScript Congress 2023TypeScript Congress 2023
24 min
Faster TypeScript builds with --isolatedDeclarations
Top Content
Type-checking a TypeScript codebase can be slow, especially for monorepos containing lots of projects that each need to use the type checker to generate type declaration files. In this talk, we introduce — for the very first time — a new TypeScript feature we are working on called “Isolated Declarations” that allows DTS files to be generated without using the type checker at all! This opens the door to faster declaration generation in TypeScript itself, as well as in external tools written in other languages such as ESBuild and swc. You'll see how to use this new option, and maybe (just maybe) you’ll be convinced about the benefits of explicit return types! Most importantly, we will show how Isolated Declarations enables parallel builds to spread work across your CPU cores to significantly improve the build speed of your TypeScript projects.