File size: 8,337 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# A statically generated blog example using Next.js and Storyblok

This example showcases Next.js's [Static Generation](https://nextjs.org/docs/basic-features/pages) feature using [Storyblok](https://www.storyblok.com/) as the data source.

## Demo

[https://next-blog-storyblok.vercel.app/](https://next-blog-storyblok.vercel.app/)

## Deploy your own

Once you have access to [the environment variables you'll need](#step-6-set-up-environment-variables), deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=next-example).

[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https://github.com/vercel/next.js/tree/canary/examples/cms-storyblok&project-name=cms-storyblok&repository-name=cms-storyblok&env=STORYBLOK_PREVIEW_SECRET,STORYBLOK_API_KEY&envDescription=Required%20to%20connect%20the%20app%20with%20Storyblok&envLink=https://vercel.link/cms-storyblok-env)

### Related examples

- [AgilityCMS](/examples/cms-agilitycms)
- [Builder.io](/examples/cms-builder-io)
- [ButterCMS](/examples/cms-buttercms)
- [Contentful](/examples/cms-contentful)
- [Cosmic](/examples/cms-cosmic)
- [DatoCMS](/examples/cms-datocms)
- [DotCMS](/examples/cms-dotcms)
- [Drupal](/examples/cms-drupal)
- [Enterspeed](/examples/cms-enterspeed)
- [Ghost](/examples/cms-ghost)
- [GraphCMS](/examples/cms-graphcms)
- [Kontent.ai](/examples/cms-kontent-ai)
- [MakeSwift](/examples/cms-makeswift)
- [Payload](/examples/cms-payload)
- [Plasmic](/examples/cms-plasmic)
- [Prepr](/examples/cms-prepr)
- [Prismic](/examples/cms-prismic)
- [Sanity](/examples/cms-sanity)
- [Sitecore XM Cloud](/examples/cms-sitecore-xmcloud)
- [Sitefinity](/examples/cms-sitefinity)
- [Storyblok](/examples/cms-storyblok)
- [TakeShape](/examples/cms-takeshape)
- [Tina](/examples/cms-tina)
- [Umbraco](/examples/cms-umbraco)
- [Umbraco heartcore](/examples/cms-umbraco-heartcore)
- [Webiny](/examples/cms-webiny)
- [WordPress](/examples/cms-wordpress)
- [Blog Starter](/examples/blog-starter)

## How to use

Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init), [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/), or [pnpm](https://pnpm.io) to bootstrap the example:

```bash
npx create-next-app --example cms-storyblok cms-storyblok-app
```

```bash
yarn create next-app --example cms-storyblok cms-storyblok-app
```

```bash
pnpm create next-app --example cms-storyblok cms-storyblok-app
```

## Configuration

### Step 1. Create an account on Storyblok

[Create an account on Storyblok](https://app.storyblok.com/).

When signing up, select **Create a new space**. Its name can be anything.

### Step 2. Create the `Authors` folder

From the dashboard, create a new folder called `Authors`.

- For **Default content type**, select **Add new**.
- Name of content type should be `author`.
- Choose **Blank** as the content type blueprint.

### Step 3. Create an `author` entry

In the `Authors` folder, create a new entry.

- **Name** can be anything, such as `Test Author`.

After creating the entry, click **Define schema**.

- Add a key called `picture`. Then click `picture` and set the **Type** as **Asset** and select **Images**.

Then upload an image to `picture`. You can use an [image from Unsplash](https://unsplash.com/).

Finally, after uploading, click **Publish**.

### Step 4. Create the `Posts` folder

After publishing the author, go back to the dashboard by clicking **Content** on the sidebar.

This time, create a new folder called `Posts`.

- For **Default content type**, select **Add new**.
- Name of content type should be `post`.
- Choose **Post** as the content type blueprint.

### Step 5. Create a `post` entry

In the `Posts` folder, create a new entry.

- **Name** can be anything.

After creating the entry, click **Define schema**.

- Add a key called `title`. Then click `title` and set the **Type** as **Text**
- Add a key called `image`. Then click `title` and set the **Type** as **Text**
- Add a key called `intro`. Then click `title` and set the **Type** as **Text**
- Add a key called `long_text`. Then click `title` and set the **Type** as **Richtext**
- Add a key called `author`. Then click `title` and set the **Type** as **Single-Option** and set **Source** as **Stories** and **Restrict to content type** as **author**

Now, populate each field.

- **Title** can be any text.
- **Image** can be an [image from Unsplash](https://unsplash.com/).
- **Intro** can be any text.
- **Long Text** can be any text.
- **Author** should be the author created earlier.

Finally, click **Publish**.

You can create more posts under the `Posts` folder. Make sure to publish each one.

### Step 6. Set up environment variables

Go to the **Settings** menu for your space, and click **API-Keys**.

Then copy the **preview** token on the page.

Next, copy the `.env.local.example` file in this directory to `.env.local` (which will be ignored by Git):

```bash
cp .env.local.example .env.local
```

Then set each variable on `.env.local`:

- `STORYBLOK_API_KEY` should be the API key you just copied.
- `STORYBLOK_PREVIEW_SECRET` can be any random string (but avoid spaces), like `MY_SECRET` - this is used for [the Preview Mode](https://nextjs.org/docs/advanced-features/preview-mode).

### Step 9. Configure Image Optimization

To safely allow optimizing images, define a list of supported URL patterns in `next.config.js`. Use the following configuration to allow Storyblok assets and Unspash image URL's

```
module.exports = {
    images: {
      remotePatterns: [
        {
          protocol: 'https',
          hostname: 'a.storyblok.com',
          port: '',
          pathname: '**',
          search: '',
        },
        {
          protocol: 'https',
          hostname: 'images.unsplash.com',
          port: '',
          pathname: '**',
          search: '',
        },
       ],
    },
  }
```

Read more about image optimization in the [Documentation](https://nextjs.org/docs/pages/building-your-application/optimizing/images)

### Step 8. Run Next.js in development mode

```bash
npm install
npm run dev

# or

yarn install
yarn dev
```

Your blog should be up and running on [http://localhost:3000](http://localhost:3000)! If it doesn't work, you can post on [GitHub discussions](https://github.com/vercel/next.js/discussions).

### Step 9. Try preview mode

To try preview mode, create another post like before (you can try duplicating), but **do not publish it - just save it**:

Now, if you go to the post page on localhost, you won't see this post because it’s not published. However, if you use the **Preview Mode**, you'll be able to see the change ([Documentation](https://nextjs.org/docs/advanced-features/preview-mode)).

To enable the Preview Mode, go to this URL:

```
http://localhost:3000/api/preview?secret=<secret>&slug=<slug>
```

- `<secret>` should be the string you entered for `STORYBLOK_PREVIEW_SECRET`.
- `<slug>` should be the post's `slug` (which can be seen on the **Config** section).

You should now be able to see the draft post. To exit the preview mode, you can click **Click here to exit preview mode** at the top.

### Step 10. Deploy on Vercel

You can deploy this app to the cloud with [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)).

#### Deploy Your Local Project

To deploy your local project to Vercel, push it to GitHub/GitLab/Bitbucket and [import to Vercel](https://vercel.com/new?utm_source=github&utm_medium=readme&utm_campaign=next-example).

**Important**: When you import your project on Vercel, make sure to click on **Environment Variables** and set them to match your `.env.local` file.

#### Deploy from Our Template

Alternatively, you can deploy using our template by clicking on the Deploy button below.

[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https://github.com/vercel/next.js/tree/canary/examples/cms-storyblok&project-name=cms-storyblok&repository-name=cms-storyblok&env=STORYBLOK_PREVIEW_SECRET,STORYBLOK_API_KEY&envDescription=Required%20to%20connect%20the%20app%20with%20Storyblok&envLink=https://vercel.link/cms-storyblok-env)