|
| 1 | +<a href="https://www.gatsbyjs.com"> |
| 2 | + <img alt="Gatsby" src="https://www.gatsbyjs.com/Gatsby-Monogram.svg" width="60" /> |
| 3 | +</a> |
| 4 | + |
| 5 | +# Gatsby Starter Kontent.ai Homepage |
| 6 | + |
| 7 | +Create a homepage using Gatsby and Kontent.ai. This starter demonstrates how to use Kontent.ai to build a homepage and can be customized to match your own visual branding. |
| 8 | + |
| 9 | +[View the Demo](https://gatsbykontentaihomepage.gatsbyjs.io/) |
| 10 | + |
| 11 | +**Note:** |
| 12 | +This version of the Kontent.ai homepage starter is written in TypeScript. If you want to use Kontent.ai but JavaScript is more your style, there is also a JavaScript version maintained on [GitHub](https://github.com/gatsbyjs/gatsby-starter-kontent-ai-homepage). |
| 13 | + |
| 14 | +## Quick start |
| 15 | + |
| 16 | +To use this starter, you will need a new or existing [Kontent.ai project][] and then you will be asked for your [project id][]. |
| 17 | + |
| 18 | +[kontent.ai project]: https://kontent.ai/learn/tutorials/manage-kontent/projects/manage-projects/#a-create-projects |
| 19 | +[project id]: https://kontent.ai/learn/tutorials/develop-apps/get-content/get-content-items/#a-1-find-your-project-id |
| 20 | + |
| 21 | +1. **Create a Gatsby site** |
| 22 | + |
| 23 | + Use the Gatsby CLI to get started locally: |
| 24 | + |
| 25 | + ```sh repo |
| 26 | + npx gatsby new my-homepage https://github.com/gatsbyjs/gatsby-starter-kontent-ai-homepage-ts |
| 27 | + ``` |
| 28 | + |
| 29 | +1. **Create an empty [Kontent.ai project](https://kontent.ai/learn/tutorials/manage-kontent/projects/manage-projects/#a-create-projects)** |
| 30 | + |
| 31 | +1. **Run the Kontent.ai setup script.** |
| 32 | + |
| 33 | + > If you've decided to use a non-empty project, keep in mind that there might occur errors during import. Therefore it is better to use an empty project. The script provides you with an option to clear your project before importing. |
| 34 | +
|
| 35 | + From your site's root directory, run: |
| 36 | + |
| 37 | + ```sh |
| 38 | + cd my-homepage |
| 39 | + yarn setup |
| 40 | + ``` |
| 41 | + |
| 42 | + This will run a script to populate your Kontent.ai project with demo content. |
| 43 | + |
| 44 | +1. **Start developing** |
| 45 | + |
| 46 | + In your site directory, start the development server: |
| 47 | + |
| 48 | + ```sh |
| 49 | + yarn start |
| 50 | + ``` |
| 51 | + |
| 52 | + Your site should now be running at <http://localhost:8000> |
| 53 | + |
| 54 | +1. **Open the source code and start editing** |
| 55 | + |
| 56 | +## Deploy your site |
| 57 | + |
| 58 | +Once your content is available in Kontent.ai, deploy your site to [Gatsby Cloud](https://gatsbyjs.com/products/cloud): |
| 59 | + |
| 60 | +1. Push your local site to a new repo in either GitHub, GitLab, or Bitbucket |
| 61 | +1. Log into your [Gatsby Cloud Dashboard][] and click on **Add a site** |
| 62 | +1. Use the **Import from a Git repository** option to find your site |
| 63 | +1. Add the environment variables from your `.env.production` file to Gatsby Cloud during setup |
| 64 | +1. Click **Build site** and your site should start building |
| 65 | + |
| 66 | +For a more detailed walkthrough, see the tutorial on how to [build your site with Gatsby Cloud][tutorial]. |
| 67 | + |
| 68 | +[gatsby cloud dashboard]: https://gatsbyjs.com/dashboard |
| 69 | +[tutorial]: https://www.gatsbyjs.com/docs/tutorial/part-1/#build-your-site-with-gatsby-cloud |
| 70 | + |
| 71 | +### Deploy without using the CLI |
| 72 | + |
| 73 | +Alternatively, you can deploy this starter directly to Gatsby Cloud. |
| 74 | +Note that you will need to set up your content in Kontent.ai manually. |
| 75 | + |
| 76 | +[](test) |
| 77 | + |
| 78 | +## Setting up Gatsby Cloud Preview |
| 79 | + |
| 80 | +To use Gatsby Cloud Preview with this site, follow the instructions in the |
| 81 | +[Connecting to Kontent.ai](https://support.gatsbyjs.com/hc/en-us/articles/360052324654-Connecting-to-Kontent). |
| 82 | + |
| 83 | +## What's included? |
| 84 | + |
| 85 | +```sh |
| 86 | +├── README.md |
| 87 | +├── gatsby-config.js |
| 88 | +├── gatsby-node.js |
| 89 | +├── src |
| 90 | +│ ├── components |
| 91 | +│ ├── pages |
| 92 | +│ ├── colors.css.ts |
| 93 | +│ ├── styles.css.ts |
| 94 | +│ └── theme.css.ts |
| 95 | +└── .env.EXAMPLE |
| 96 | +``` |
| 97 | + |
| 98 | +1. **`gatsby-config.js`**: [Gatsby config][] file that includes plugins required for this starter. |
| 99 | +1. **`gatsby-node.js`**: [Gatsby Node][] config file that creates an abstract data model for the homepage content. |
| 100 | +1. **`src/`**: The source directory for the starter, including pages, components, and [Vanilla Extract][] files for styling. |
| 101 | + |
| 102 | +[gatsby config]: https://www.gatsbyjs.com/docs/reference/config-files/gatsby-config/ |
| 103 | +[gatsby node]: https://www.gatsbyjs.com/docs/reference/config-files/gatsby-node/ |
| 104 | +[vanilla extract]: https://vanilla-extract.style/ |
| 105 | + |
| 106 | +## How to |
| 107 | + |
| 108 | +### Update the color theme |
| 109 | + |
| 110 | +To update the colors used in this starter, edit the `src/colors.css.ts` file. |
| 111 | + |
| 112 | +```.ts |
| 113 | +// src/colors.css.ts |
| 114 | +export const colors = { |
| 115 | + background: "#41d9c5", |
| 116 | + text: "#231f20", |
| 117 | + primary: "#231f20", |
| 118 | + muted: "#2aceb9", |
| 119 | + active: "#001d3d", |
| 120 | + black: "#000", |
| 121 | +} |
| 122 | + |
| 123 | +``` |
| 124 | + |
| 125 | +If you'd like to add additional colors, add additional keys to this object. |
| 126 | +This file is imported into `src/theme.css.ts` and creates CSS custom properties, that can be imported and used in other `.css.ts` files. |
| 127 | + |
| 128 | +The UI components file `src/components/ui.tsx` imports styles from `src/components/ui.css.ts`. You can see how the theme and color values are being used in this file. |
| 129 | + |
| 130 | +### Add your logo |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | +Replace the `src/components/brand-logo.tsx` component with your own brand logo. |
| 135 | +If you have an SVG version, it can be rendered inline as a React component, following the example in this file. Note that SVG attributes will need to be camel cased for JSX. |
| 136 | + |
| 137 | +Using an inline SVG for the logo allows it to pick up the colors used in CSS, which is how the logo colors are inverted for the mobile menu. |
| 138 | + |
| 139 | +If you prefer to use an image, use the [`StaticImage`](https://www.gatsbyjs.com/docs/reference/built-in-components/gatsby-plugin-image/#staticimage) component from `gatsby-plugin-image` in place of the SVG in this file. |
| 140 | + |
| 141 | +### Customize headings, buttons, and other styles |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | +To further customize the look and feel of the homepage, edit the UI components in `src/components/ui.tsx` and styles in `src/components/ui.css.ts`. |
| 146 | + |
| 147 | +### Customize section components |
| 148 | + |
| 149 | +To customize any of the sections of the homepage, edit the relevant component in `src/components`. |
| 150 | +Most of the styles for these components are handled with shared UI components in `src/components/ui.tsx`. |
| 151 | + |
| 152 | +### Create custom section components |
| 153 | + |
| 154 | +To create a new type of section in your homepage, you'll want to create a new section component, using the existing components as an example. |
| 155 | +For this example, we'll create a new "Banner" component. |
| 156 | + |
| 157 | +1. First, update your content type in Kontent.ai project |
| 158 | + |
| 159 | + 1. In `Content model` section create a new content type and name it "Homepage Banner." |
| 160 | + |
| 161 | + <img src="./images/step-1.png" alt="Step 1" width="700" /> |
| 162 | + |
| 163 | + 1. Add two fields to your freshly created content type: `Heading` and `Text` – these can be _Text_ types. |
| 164 | + |
| 165 | + <img src="./images/step-2.png" alt="Step 2" width="700" /> |
| 166 | + |
| 167 | + 1. Find the content type for _Homepage_ in Kontent.ai and adjust the settings for the _Content_ element. Click on settings icon to show configuration for the given element. Under _Allowed content types_, ensure that the _Homepage Banner_ type is checked to make it available as a content type on the Homepage. |
| 168 | + |
| 169 | + <img src="./images/step-3.png" alt="Step 3" width="700" /> |
| 170 | + <img src="./images/step-4.png" alt="Step 4" width="700" /> |
| 171 | + |
| 172 | + 1. Navigate to the _Content & assets_ tab to edit the _Homepage_, start a new version of your item and add create a new item with new _Homepage Banner_ content type. |
| 173 | + |
| 174 | + <img src="./images/step-5.png" alt="Step 5" width="500" /> |
| 175 | + <img src="./images/step-6.png" alt="Step 6" width="500" /> |
| 176 | + <img src=".s/images/step-7.png" alt="Step 7" width="500" /> |
| 177 | + |
| 178 | +1. Update `gatsby-node.js` |
| 179 | + |
| 180 | + Edit your site's `gatsby-node.js` file, adding an interface for `HomepageBanner` and a type that matches your content type in Kontent.ai. |
| 181 | + This allows the homepage to query the abstract `HomepageBanner` type. |
| 182 | + |
| 183 | + ```js |
| 184 | + // in gatsby-node.js |
| 185 | + exports.createSchemaCustomization = async ({ actions }) => { |
| 186 | + // ... |
| 187 | + actions.createTypes(` |
| 188 | + interface HomepageBanner implements Node & HomepageBlock { |
| 189 | + id: ID! |
| 190 | + blocktype: String |
| 191 | + heading: String |
| 192 | + text: String |
| 193 | + } |
| 194 | + `) |
| 195 | + // ... |
| 196 | + actions.createTypes(` |
| 197 | + type kontent_item_homepage_banner implements Node & HomepageBanner & HomepageBlock @dontInfer { |
| 198 | + id: ID! |
| 199 | + blocktype: String @blocktype |
| 200 | + heading: String @proxy(from: "elements.heading.value") |
| 201 | + text: String @proxy(from: "elements.text.value") |
| 202 | + } |
| 203 | + `) |
| 204 | + // ... |
| 205 | + } |
| 206 | + ``` |
| 207 | + |
| 208 | +1. Next, create the Banner component: |
| 209 | + |
| 210 | + ```jsx fileExt |
| 211 | + // src/components/banner.tsx |
| 212 | + import * as React from "react" |
| 213 | + import { graphql } from "gatsby" |
| 214 | + import { Section, Container, Heading, Text } from "./ui" |
| 215 | + |
| 216 | + export default function Banner(props) { |
| 217 | + return ( |
| 218 | + <Section> |
| 219 | + <Container> |
| 220 | + <Heading>{props.heading}</Heading> |
| 221 | + <Text>{props.text}</Text> |
| 222 | + </Container> |
| 223 | + </Section> |
| 224 | + ) |
| 225 | + } |
| 226 | + |
| 227 | + export const query = graphql` |
| 228 | + fragment HomepageBannerContent on HomepageBanner { |
| 229 | + id |
| 230 | + heading |
| 231 | + text |
| 232 | + } |
| 233 | + ` |
| 234 | + ``` |
| 235 | + |
| 236 | +1. Export the component from `src/components/sections.tsx` |
| 237 | + |
| 238 | + ```js fileExt |
| 239 | + // src/components/sections.tsx |
| 240 | + export { default as HomepageHero } from "./hero" |
| 241 | + export { default as HomepageFeature } from "./feature" |
| 242 | + export { default as HomepageFeatureList } from "./feature-list" |
| 243 | + export { default as HomepageLogoList } from "./logo-list" |
| 244 | + export { default as HomepageBenefitList } from "./benefit-list" |
| 245 | + export { default as HomepageTestimonialList } from "./testimonial-list" |
| 246 | + export { default as HomepageStatList } from "./stat-list" |
| 247 | + export { default as HomepageCta } from "./cta" |
| 248 | + export { default as HomepageProductList } from "./product-list" |
| 249 | + |
| 250 | + // add export for new component |
| 251 | + export { default as HomepageBanner } from "./banner" |
| 252 | + ``` |
| 253 | + |
| 254 | +1. Add the GraphQL query fragment to the query in `src/pages/index.tsx` |
| 255 | + |
| 256 | + ```js fileExt |
| 257 | + // in src/pages/index.tsx |
| 258 | + export const query = graphql` |
| 259 | + { |
| 260 | + homepage { |
| 261 | + id |
| 262 | + title |
| 263 | + description |
| 264 | + image { |
| 265 | + id |
| 266 | + url |
| 267 | + } |
| 268 | + blocks: content { |
| 269 | + id |
| 270 | + blocktype |
| 271 | + ...HomepageHeroContent |
| 272 | + ...HomepageFeatureContent |
| 273 | + ...HomepageFeatureListContent |
| 274 | + ...HomepageCtaContent |
| 275 | + ...HomepageLogoListContent |
| 276 | + ...HomepageTestimonialListContent |
| 277 | + ...HomepageBenefitListContent |
| 278 | + ...HomepageStatListContent |
| 279 | + ...HomepageProductListContent |
| 280 | + # New component fragment |
| 281 | + ...HomepageBannerContent |
| 282 | + } |
| 283 | + } |
| 284 | + } |
| 285 | + ` |
| 286 | + ``` |
| 287 | + |
| 288 | +## Troubleshooting |
| 289 | + |
| 290 | +### Errors after making changes to the schema |
| 291 | + |
| 292 | +If you've made changes to the `gatsby-node.js` file or changes to the Kontent.ai data model, clear the Gatsby cache before running the develop server: |
| 293 | + |
| 294 | +```sh |
| 295 | +yarn clean && yarn start |
| 296 | +``` |
| 297 | + |
| 298 | +--- |
| 299 | + |
| 300 | +## 🎓 Learning Gatsby |
| 301 | + |
| 302 | +Looking for more guidance? Full documentation for Gatsby lives [on the website](https://www.gatsbyjs.com/). Here are some places to start: |
| 303 | + |
| 304 | +- **For most developers, we recommend starting with our [in-depth tutorial for creating a site with Gatsby](https://www.gatsbyjs.com/tutorial/).** It starts with zero assumptions about your level of ability and walks through every step of the process. |
| 305 | +- **To dive straight into code samples, head [to our documentation](https://www.gatsbyjs.com/docs/).** |
| 306 | + |
| 307 | +## 💫 Deploy |
| 308 | + |
| 309 | +[Build, Deploy, and Host On The Only Cloud Built For Gatsby](https://www.gatsbyjs.com/cloud/) |
| 310 | + |
| 311 | +Gatsby Cloud is an end-to-end cloud platform specifically built for the Gatsby framework that combines a modern developer experience with an optimized, global edge network. |
0 commit comments