|
1 | | -# Contentful Blog Starter Template |
| 1 | +# Contentful Statsig Integration Blog Page Example |
2 | 2 |
|
3 | | -A Blog Starter Template powered by Next.js & Contentful, pre-designed with optimized & adjustable pages, components, and data management. |
| 3 | +This Repository serves as an example of how you can integrate your application to use Statsig's Contentful Integration. Before proceeding, follow the guide in [these docs](https://docs.statsig.com/guides/contentful/) to setup your Contentful space to be configured with Statsig. |
4 | 4 |
|
5 | | - |
| 5 | +## Integration Instructions |
6 | 6 |
|
7 | | -$~$ |
| 7 | +This example shows how you can run experiments on `page - Blog post`s, serving different `component - Rich image`s to users through a Statsig experiment automatically generated via Contentful. |
8 | 8 |
|
9 | | -## What is Contentful? |
| 9 | +Make sure to checkout [Contentful's Next.js Blog Starter Template](https://github.com/contentful/template-blog-webapp-nextjs/tree/bc28265) to get your test Blog Page app started first. |
10 | 10 |
|
11 | | -[Contentful](https://www.contentful.com/) provides content infrastructure for digital teams to power websites, apps, and devices. Unlike a CMS, Contentful was built to integrate with the modern software stack. It offers a central hub for structured content, powerful management, and delivery APIs, and a customizable web app that enables developers and content creators to ship their products faster. |
| 11 | +### Setup a Statsig SDK |
12 | 12 |
|
13 | | -$~$ |
| 13 | +You will need a Statsig SDK to get your Statsig experiment assignment for a given user. To add a Statsig js-client sdk to your Blog Page app, run the command: |
14 | 14 |
|
15 | | -## DISCLAIMER ⚠️ |
| 15 | +```yarn add @statsig/js-client``` |
16 | 16 |
|
17 | | -The Starter Templates experience is currently only available to new users. |
| 17 | +Now, add a client SDK key as a `STATSIG_SDK_KEY` in your `.env` file (see [`.env.example`](https://github.com/statsig-io/contentful-blog-webapp-nextjs-example/blob/main/.env.example)). You can setup an example statsig client following [this changeset](https://github.com/statsig-io/contentful-blog-webapp-nextjs-example/pull/5/commits/f18bae9b60cc944deeb12f5c8862072024cd9b84) as a guide. |
18 | 18 |
|
19 | | -To benefit from this experience, please follow this link to create a new |
20 | | -account and select the template to install: [https://www.contentful.com/starter-templates/nextjs-blog/sign-up/?action=create_starter_template](https://www.contentful.com/starter-templates/nextjs-blog/sign-up/?action=create_starter_template&utm_source=github.com&utm_medium=referral&utm_campaign=template-blog-webapp-nextjs). |
| 19 | +### Add Experiment Fields to GraphQL |
21 | 20 |
|
22 | | -Alternatively, to immediately start the auto installation of this template after creating a new account, |
23 | | -please follow this link: |
24 | | -[https://www.contentful.com/starter-templates/nextjs-blog/sign-up/?action=create_starter_template&template_name=blog](https://www.contentful.com/starter-templates/nextjs-blog/sign-up/?action=create_starter_template&template_name=blog&utm_source=github.com&utm_medium=referral&utm_campaign=template-blog-webapp-nextjs). |
| 21 | +Now you can setup your experiments to be pulled through GraphQL. Follow [this changeset](https://github.com/statsig-io/contentful-blog-webapp-nextjs-example/pull/5/commits/545697b54af182fe55d93994369a2b205fe3387f) as a guide. |
25 | 22 |
|
26 | | -$~$ |
| 23 | +### Add Experiment to Content Types |
27 | 24 |
|
28 | | -## Begin your journey with Contentful and the Blog Starter Template |
29 | | - |
30 | | -Follow this [guide](https://github.com/contentful/template-blog-webapp-nextjs/blob/main/docs/tutorials/contentful-and-the-starter-template.md/?utm_source=github.com-guide&utm_medium=referral&utm_campaign=template-blog-webapp-nextjs) to understand the relationship between |
31 | | -Contentful and the Starter Template source code through guided steps: |
32 | | - |
33 | | -- Entry editing, and updates preview in the Starter Template application (online/locally) |
34 | | -- Content type editing in the Contentful web app, as well as in the Starter Template's code |
35 | | - |
36 | | -$~$ |
37 | | - |
38 | | -## Features |
39 | | - |
40 | | -- Composable content through powerful & flexible content modeling. |
41 | | -- Localization ready. |
42 | | -- SEO ready. |
43 | | -- Incremental Static Regeneration with Next.js[^1]. |
44 | | -- Generation of GraphQL[^2] typed code (schema, and types), in sync with the content types through graphql-codegen[^3]. |
45 | | -- Enhanced Developer Experience with TypeScript[^4]. |
46 | | - |
47 | | -$~$ |
48 | | - |
49 | | -## Getting started |
50 | | - |
51 | | -To get started, read the following guidelines. |
52 | | - |
53 | | -- [Environment variables](./README.md#environment-variables) |
54 | | -- [Dependencies](./README.md#dependencies) |
55 | | -- [Development](./README.md#development) |
56 | | -- [Contentful API & GraphQL](./README.md#contentful-api--graphql) |
57 | | -- [Deployment](./README.md#deployment) |
58 | | - |
59 | | -$~$ |
60 | | - |
61 | | -### Environment variables |
62 | | - |
63 | | -In order to authenticate the requests to the Contentful APIs, the following values are necessary: |
64 | | - |
65 | | -- Your space ID: [https://www.contentful.com/help/find-space-id/](https://www.contentful.com/help/find-space-id/) |
66 | | -- Contentful Delivery API token: [https://www.contentful.com/developers/docs/references/content-delivery-api/](https://www.contentful.com/developers/docs/references/content-delivery-api/) |
67 | | -- Contentful Preview API token: [https://www.contentful.com/developers/docs/references/content-preview-api/](https://www.contentful.com/developers/docs/references/content-preview-api/) |
68 | | - |
69 | | -Rename the `.env.example` file to `.env` and add the necessary values. |
70 | | - |
71 | | -$~$ |
72 | | - |
73 | | -### Dependencies |
74 | | - |
75 | | -To install the necessary dependencies, run: |
76 | | - |
77 | | -```bash |
78 | | -yarn |
79 | | -``` |
80 | | - |
81 | | -### Run the Starter Template in development mode |
82 | | - |
83 | | -```bash |
84 | | -yarn dev |
85 | | -``` |
86 | | - |
87 | | -The Starter Template should be up and running on `http://localhost:3000`. |
88 | | - |
89 | | -All necessary dependencies are installed under `node_modules` and any necessary tools can be accessed via npm scripts. |
90 | | - |
91 | | -$~$ |
92 | | - |
93 | | -## Development |
94 | | - |
95 | | -### Node |
96 | | - |
97 | | -It is recommended to use the Node version listed in the `.nvmrc` file, we recommend using [nvm](https://github.com/nvm-sh/nvm) to easily switch between Node versions. |
98 | | - |
99 | | -$~$ |
100 | | - |
101 | | -### Husky & git hooks |
102 | | - |
103 | | -This repository makes use of [Husky](https://github.com/typicode/husky) to enforce commit hooks. |
104 | | - |
105 | | -The config for both the `pre-commit` and `pre-push` hooks can be found in the `.husky` folder, located in the root of the project. |
106 | | - |
107 | | ---- |
108 | | - |
109 | | -#### Pre-commit |
110 | | - |
111 | | -Before allowing a commit, we require a successful result from the TypeScript compiler (`tsc`) and our `lint-staged` script will be run. |
112 | | - |
113 | | -This ensures all ESLint and Prettier rules are enforced on the files that are staged to be committed. |
114 | | - |
115 | | -The `tsc` command is run separately from the `lint-staged` step because we require the Typescript compiler to sample _all_ files. |
116 | | - |
117 | | -This is important to ensure that no deviating types were introduced by the [codegen](./README.md#graphql--code-generation) for example. |
118 | | - |
119 | | ---- |
120 | | - |
121 | | -#### Pre-push |
122 | | - |
123 | | -The same two tasks are run for pre-push and for pre-commit. |
124 | | - |
125 | | ---- |
126 | | - |
127 | | -#### Overriding the Husky git hooks |
128 | | - |
129 | | -In case of wanting to bypass the `pre-commit` or `pre-push` hooks, pass a `--noVerify` flag to your Git commands. |
130 | | - |
131 | | -⚠️ Make sure you only use this if you know why you're using it. ⚠️ |
132 | | - |
133 | | -$~$ |
134 | | - |
135 | | -### Contentful API & GraphQL |
136 | | - |
137 | | -This project makes use of Contentful's [GraphQL API](https://www.contentful.com/developers/docs/references/graphql/). |
138 | | - |
139 | | -API calls made to the Contentful GraphQL endpoint are made through `graphql-request`. |
140 | | - |
141 | | -The types are generated from the `.graphql` files located in the `/lib/graphql/` directory: |
142 | | - |
143 | | -1. `lib/graphql/[fileName].graphql` is detected by the [codegen](./README.md#graphql--code-generation) |
144 | | -2. `lib/__generated/sdk.ts` is generated |
145 | | -3. Within the generated file, their types and a new `getSdk` function are generated |
146 | | -4. The `getSdk` function can now be imported and used within the `getStaticProps` in the pages files |
147 | | - |
148 | | -$~$ |
149 | | - |
150 | | -### GraphQL & code generation |
151 | | - |
152 | | -We use `graphql-codegen` to generate a type-safe API client, utilizing [GraphQLClient](https://the-guild.dev/graphql/codegen/plugins/typescript/typescript-graphql-request) as the "client". |
153 | | - |
154 | | ---- |
155 | | - |
156 | | -#### Commands |
157 | | - |
158 | | -In order to (re-)generate the GraphQL schema, types and sdk, please use either of the following commands: |
159 | | - |
160 | | -- `yarn graphql-codegen:generate` generates a schema, types and code to fetch data from the Contentful APIs |
161 | | -- `yarn graphql-codegen:watch` similar to the `generate` command, but it runs as a watch task which will rerun the steps when changes are made in the `.graphql` files |
162 | | - |
163 | | -The first steps of the codegen generate files that contain the GraphQL schema and matching TypeScript types. All these files are located in the `src/lib/graphql` folder. |
164 | | -They're generated to the `src/lib/__generated` folder and ought to be committed once altered/added to the repository. |
165 | | - |
166 | | -The TS types for these files are generated in the same location, in a `__generated` folder and like the other files ought to be committed. |
167 | | - |
168 | | ---- |
169 | | - |
170 | | -#### Configuration |
171 | | - |
172 | | -The configuration for the codegen can be found in `codegen.ts`, located in the root of the project. |
173 | | - |
174 | | -$~$ |
175 | | - |
176 | | ---- |
177 | | - |
178 | | -$~$ |
179 | | - |
180 | | -## Deployment |
181 | | - |
182 | | -The Starter Template can be deployed to your hosting provider of choice. |
183 | | - |
184 | | -We offer integrations with Vercel and Netlify to speed up the process by clicking one of the deploy buttons below. The GitHub repository and |
185 | | -the necessary environment variables keys are pre-configured in the hosting provider space. |
186 | | - |
187 | | -| Vercel | Netlify | |
188 | | -| -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
189 | | -| [](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fcontentful%2Ftemplate-blog-webapp-nextjs&env=CONTENTFUL_SPACE_ID,CONTENTFUL_ACCESS_TOKEN,CONTENTFUL_PREVIEW_ACCESS_TOKEN&envDescription=API%20Keys%20needed%20for%20the%20application&envLink=https%3A%2F%2Fgithub.com%2Fcontentful%2Ftemplate-blog-webapp-nextjs%23environment-variables) | [](https://app.netlify.com/start/deploy?repository=https%3A%2F%2Fgithub.com%2Fcontentful%2Ftemplate-blog-webapp-nextjs#CONTENTFUL_SPACE_ID=&CONTENTFUL_ACCESS_TOKEN=&CONTENTFUL_PREVIEW_ACCESS_TOKEN=) | |
190 | | -| [Environment variables docs](https://vercel.com/docs/concepts/projects/environment-variables) | [Environment variables docs](https://docs.netlify.com/environment-variables/overview/) | |
191 | | - |
192 | | -Make sure to add the necessary [environment variables values](./README.md#environment-variables) to the hosting provider |
193 | | -environment variables. |
194 | | - |
195 | | ---- |
196 | | - |
197 | | -## Content preview |
198 | | - |
199 | | -Once you have the Starter Template deployed on your hosting provider, you can update the Content preview URL in your space settings. |
200 | | - |
201 | | -You can follow our guide to learn how to do |
202 | | -so: [https://www.contentful.com/help/setup-content-preview](https://www.contentful.com/help/setup-content-preview/?utm_source=github.com-preview-guide&utm_medium=referral&utm_campaign=template-marketing-webapp-nextjs). |
203 | | - |
204 | | -For the live preview the basic field tagging for the inspector mode and live updates are already implemented. |
205 | | -For custom components, you can find the instructions at our [guide](https://www.contentful.com/developers/docs/tutorials/general/live-preview/). |
206 | | - |
207 | | -### Adjustments in code |
208 | | - |
209 | | -1. Set a unique value for `process.env.CONTENTFUL_PREVIEW_SECRET` in your environment variables. This value should be kept secret and only known to the API route and the CMS. |
210 | | -2. Configure the entry preview URLs in Contentful to match the draft API route's URL structure. This can be done in the Contentful web interface under "Settings" for each content type. For more information see: https://www.contentful.com/help/setup-content-preview/#preview-content-in-your-online-environment |
211 | | -3. The draft mode API route is already written in the app and can be found in `src/app/api/enable-draft/route.ts`. This route checks for a valid secret and slug before redirecting to the corresponding page\*. |
212 | | - |
213 | | -_\*The `slug` field is optional; When not passed we redirect the page to the root of the domain._ |
214 | | - |
215 | | -### Adjustments in Contentful |
216 | | - |
217 | | -1. Next, you will need to configure your Contentful space to use the correct preview URLs. To do this, go to the "Settings" section of your space, and click on the "Content Preview" tab. From here, you can configure the preview URLs for each of your content models. |
218 | | -2. Edit all content models that need a preview url. We usually expect that to only be the models prefixed with `📄 page -`. |
219 | | -3. Add a new URL with the following format: `https://<your-site>/api/enable-draft?path=%2F{locale}%2F{entry.fields.slug}&x-contentful-preview-secret=<token>`. Make sure to replace `<your-site>` with the URL of your Next.js site, and `<token>` with the value of `process.env.CONTENTFUL_PREVIEW_SECRET`. |
220 | | -4. Now, when you view an unpublished entry in Contentful, you should see a "Preview" button that will take you to the preview URL for that entry. Clicking this button should show you a preview of the entry on your Next.js site, using the draft API route that we set up earlier. |
221 | | - |
222 | | - |
223 | | -$~$ |
224 | | - |
225 | | ---- |
226 | | - |
227 | | -$~$ |
228 | | - |
229 | | -## Support |
230 | | - |
231 | | -If you have a problem with this Starter Template, post a message in our [Contentful Community Slack](https://www.contentful.com/slack/). |
232 | | - |
233 | | -Can't find your answer there? You can file a feedback issue through [this template](https://github.com/contentful/template-blog-webapp-nextjs/tree/main/.github/ISSUE_TEMPLATE/feedback.md). |
234 | | - |
235 | | -If you have other problems with Contentful not related to the Starter Template, you can contact the [Customer Support](https://support.contentful.com/). |
236 | | - |
237 | | -$~$ |
238 | | - |
239 | | -## Contributing |
240 | | - |
241 | | -See [CONTRIBUTING.md](./CONTRIBUTING.md). |
242 | | - |
243 | | -$~$ |
244 | | - |
245 | | -## License |
246 | | - |
247 | | -MIT License, see [LICENSE](./LICENSE). |
248 | | - |
249 | | -$~$ |
250 | | - |
251 | | -<!-- FOOTNOTES --> |
252 | | - |
253 | | -[^1]: [Next.js docs](https://nextjs.org/docs/basic-features/data-fetching/get-static-props) |
254 | | -[^2]: [GraphQL docs](https://graphql.org/learn/) |
255 | | -[^3]: [graphql-codegen](https://www.the-guild.dev/graphql/codegen) |
256 | | -[^4]: [TypeScript](https://www.typescriptlang.org/) |
257 | | -[^note]: [React docs](https://reactjs.org/docs/getting-started.html) |
| 25 | +Now that the graphql fragments are setup, we can add our experiment to each place the content types are used. In [this changeset](https://github.com/statsig-io/contentful-blog-webapp-nextjs-example/pull/5/commits/8be42a98c02a2a20586f91bd4433d0aa923aef84), we will replace the Featured Image of our blogs with the content pulled from the associated Statsig experiemnt for all blogs on the landing page. This is highly customizable, and can be adapted to fit your actual app's exact needs. [this changeset](https://github.com/statsig-io/contentful-blog-webapp-nextjs-example/pull/5/commits/8be42a98c02a2a20586f91bd4433d0aa923aef84) also outlines the mapping between the experiment in Contentful, and the associated parameter of the experiment in Statsig. |
0 commit comments