Unverified Commit d5837e03 authored by Ben Read's avatar Ben Read Committed by GitHub

chore(examples): add webiny cms example (#41159)

## Documentation / Examples

- [X] Make sure the linting passes by running `pnpm lint`
- [X] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
Co-authored-by: 's avatarBalázs Orbán <info@balazsorban.com>
parent 45bed967
PREVIEW_API_SECRET=
WEBINY_API_SECRET=
NEXT_PUBLIC_WEBINY_API_URL=
NEXT_PUBLIC_WEBINY_PREVIEW_API_URL=
\ No newline at end of file
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
# dependencies
/node_modules
/.pnp
.pnp.js
# testing
/coverage
# next.js
/.next/
/out/
# production
/build
# misc
.DS_Store
*.pem
# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# local env files
.env*.local
# vercel
.vercel
# typescript
*.tsbuildinfo
next-env.d.ts
# A statically generated blog example using Next.js and Webiny
This example showcases Next.js's [Static Generation](https://nextjs.org/docs/basic-features/pages) feature using [Webiny](https://webiny.com/) as the data source.
## Demo
[https://webiny-headlesscms-nextjs-example.vercel.app/](https://webiny-headlesscms-nextjs-example.vercel.app/)
## Deploy your own
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/git/external?repository-url=https://github.com/vercel/next.js/tree/canary/examples/cms-webiny&project-name=cms-webiny&repository-name=cms-webiny&env=PREVIEW_API_SECRET,WEBINY_API_SECRET,NEXT_PUBLIC_WEBINY_API_URL,NEXT_PUBLIC_WEBINY_PREVIEW_API_URL&envDescription=Required%20to%20connect%20the%20app%20with%20Webiny&envLink=https://vercel.link/cms-webiny-env)
### Related examples
- [WordPress](/examples/cms-wordpress)
- [DatoCMS](/examples/cms-datocms)
- [Sanity](/examples/cms-sanity)
- [TakeShape](/examples/cms-takeshape)
- [Prismic](/examples/cms-prismic)
- [Contentful](/examples/cms-contentful)
- [Strapi](/examples/cms-strapi)
- [Agility CMS](/examples/cms-agilitycms)
- [Cosmic](/examples/cms-cosmic)
- [ButterCMS](/examples/cms-buttercms)
- [Storyblok](/examples/cms-storyblok)
- [GraphCMS](/examples/cms-graphcms)
- [Kontent](/examples/cms-kontent)
- [Umbraco Heartcore](/examples/cms-umbraco-heartcore)
- [Builder.io](/examples/cms-builder-io)
## 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-webiny cms-webiny-app
```
```bash
yarn create next-app --example cms-webiny cms-webiny-app
```
```bash
pnpm create next-app --example cms-webiny cms-webiny-app
```
### Step 1. Set up a Webiny project
Follow the [Webiny docs](https://www.webiny.com/docs/tutorials/install-webiny) to install a Webiny project on your cloud hosting provider. Because Webiny is a distributed system we don't run it locally. This also means you don't need to worry about setting up Docker, or installing databases and drivers on your local machine for Postgres, MongoDB or similar. The cloud takes care of that for you.
If you get stuck or have any questions, please [join the community](http://webiny-community.slack.com 'Webiny slack channel') and reach out for some help.
Once you have an app up and running click into the "HeadlessCMS" app in the sidebar, click on _models_ and add the following models and fields:
#### Authors
- A `text` field with the value "name"
- A `text` field with the value "slug" (optionally add a validator using this regex which will make sure you have valid urls: `^(?!.*--)[a-z0-9\-]+$`)
- a `files` field with the value "picture"
#### Posts
- A `text` field with the value "title"
- A `text` field with the value "slug" (optionally use the regex above as a validator)
- A `files` field with the value "featured image"
- A `rich text` field with the value "body"
- A `reference` field with the value "Author"
Next, choose **API Keys** in the sidebar. Add an API key with any name and description. Select "Headless CMS" and choose a Custom access level for all content model groups with the values `read` and `preview`. Save the API token and the token itself will be revealed.
You will be able to use the same API token for both published and draft posts.
### Step 2. Set up environment variables
Copy the `.env.local.example` file to `.env.local`, then set the variables as follows:
- `PREVIEW_API_SECRET` can be any random string (but avoid spaces), like `MY_SECRET` - this is used for [Preview Mode](https://nextjs.org/docs/advanced-features/preview-mode).
- WEBINY_API_SECRET this will be your security token generated in Webiny
- You can find the values for `NEXT_PUBLIC_WEBINY_API_URL` and `NEXT_PUBLIC_WEBINY_PREVIEW_API_URL` two ways: From your local Webiny project root, run `yarn webiny info`, alternatively go to **API Playground** in the sidebar. At the top of the GraphQL explorer are four tabs, one for each of our APIs, and you'll see both the Read API and the Preview API on those tabs. The URL for your environment is just below the tab. ([More info here if you get stuck](https://www.webiny.com/docs/headless-cms/basics/graphql-api))
### Step 3. Run Next.js in development mode
Inside the Next.js app directory, run:
```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)!
The best place to debug is inside the `fetchAPI` function in `lib/api.js`. If you need help, you can post on [GitHub discussions](https://github.com/vercel/next.js/discussions).
### Step 4. Try preview mode
If you go to the `/posts/draft` 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=draft
```
- `<secret>` should be the string you entered for `PREVIEW_API_SECRET`.
- `<slug>` should be the post's `slug` attribute.
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.
To add more preview pages, create a post and set the **status** as `draft`.
### Step 5. 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/git/external?repository-url=https://github.com/vercel/next.js/tree/canary/examples/cms-webiny&project-name=cms-webiny&repository-name=cms-webiny&env=PREVIEW_API_SECRET,WEBINY_API_SECRET,NEXT_PUBLIC_WEBINY_API_URL,NEXT_PUBLIC_WEBINY_PREVIEW_API_URL&envDescription=Required%20to%20connect%20the%20app%20with%20Webiny&envLink=https://vercel.link/cms-webiny-env)
import Container from './container'
import cn from 'classnames'
import { EXAMPLE_PATH } from '../lib/constants'
export default function Alert({ preview }) {
return (
<div
className={cn('border-b', {
'bg-accent-7 border-accent-7 text-white': preview,
'bg-accent-1 border-accent-2': !preview,
})}
>
<Container>
<div className="py-2 text-sm text-center">
{preview ? (
<>
This page is a preview.{' '}
<a
href="/api/exit-preview"
className="underline transition-colors duration-200 hover:text-cyan"
>
Click here
</a>{' '}
to exit preview mode.
</>
) : (
<>
The source code for this blog is{' '}
<a
href={`https://github.com/vercel/next.js/tree/canary/examples/${EXAMPLE_PATH}`}
className="underline transition-colors duration-200 hover:text-success"
>
available on GitHub
</a>
.
</>
)}
</div>
</Container>
</div>
)
}
import Image from 'next/image'
export default function Avatar({ name, picture }) {
return (
<div className="flex items-center">
<Image
src={picture}
width={48}
height={48}
className="w-12 h-12 mr-4 rounded-full"
alt={name}
/>
<div className="text-xl font-bold">{name}</div>
</div>
)
}
export default function Container({ children }) {
return <div className="container px-5 mx-auto">{children}</div>
}
import cn from 'classnames'
import Link from 'next/link'
import Image from 'next/image'
export type TCoverImage = {
title: string
src: string
slug?: string
height: number
width: number
}
const CoverImage: React.FC<TCoverImage> = ({
title,
src,
slug,
height,
width,
}) => {
const image = (
<Image
src={src}
alt={`Cover Image for ${title}`}
className={cn('shadow-sm', {
'hover:shadow-md transition-shadow duration-200': slug,
})}
layout="responsive"
width={width}
height={height}
/>
)
return (
<div className="sm:mx-0">
{slug ? (
<Link href={`/posts/${slug}`}>
<a aria-label={title}>{image}</a>
</Link>
) : (
image
)}
</div>
)
}
export default CoverImage
import { parseISO, format } from 'date-fns'
export default function DateFormatter({ dateString }) {
if (!dateString) {
return null
}
const date = parseISO(dateString)
const formattedDate = format(date, 'LLLL d, yyyy')
return <time dateTime={dateString}>{formattedDate}</time>
}
import Container from './container'
import { EXAMPLE_PATH } from '../lib/constants'
export default function Footer() {
return (
<footer className="border-t bg-accent-1 border-accent-2">
<Container>
<div className="flex flex-col items-center py-28 lg:flex-row">
<h3 className="mb-10 text-4xl font-bold leading-tight tracking-tighter text-center lg:text-5xl lg:text-left lg:mb-0 lg:pr-4 lg:w-1/2">
Statically Generated with Next.js.
</h3>
<div className="flex flex-col items-center justify-center lg:flex-row lg:pl-4 lg:w-1/2">
<a
href="https://nextjs.org/docs/basic-features/pages"
className="px-12 py-3 mx-3 mb-6 font-bold text-white transition-colors duration-200 bg-black border border-black hover:bg-white hover:text-black lg:px-8 lg:mb-0"
>
Read Documentation
</a>
<a
href={`https://github.com/vercel/next.js/tree/canary/examples/${EXAMPLE_PATH}`}
className="mx-3 font-bold hover:underline"
>
View on GitHub
</a>
</div>
</div>
</Container>
</footer>
)
}
import Link from 'next/link'
export default function Header() {
return (
<h2 className="mt-8 mb-20 text-2xl font-bold leading-tight tracking-tight md:text-4xl md:tracking-tighter">
<Link href="/">
<a className="hover:underline">Blog</a>
</Link>
.
</h2>
)
}
import Avatar from '../components/avatar'
import DateFormatter from '../components/date-formatter'
import CoverImage from '../components/cover-image'
import Link from 'next/link'
export default function HeroPost({
title,
coverImage,
createdOn,
excerpt,
author,
slug,
}) {
return (
<section>
<div className="mb-8 md:mb-16">
<CoverImage
title={title}
src={coverImage}
slug={slug}
height={620}
width={1240}
/>
</div>
<div className="mb-20 md:grid md:grid-cols-2 md:gap-x-16 lg:gap-x-8 md:mb-28">
<div>
<h3 className="mb-4 text-4xl leading-tight lg:text-6xl">
<Link href={`/posts/${slug}`}>
<a className="hover:underline">{title}</a>
</Link>
</h3>
<div className="mb-4 text-lg md:mb-0">
<DateFormatter dateString={createdOn} />
</div>
</div>
<div>
<p className="mb-4 text-lg leading-relaxed">{excerpt}</p>
<Avatar name={author.name} picture={author.picture} />
</div>
</div>
</section>
)
}
import { CMS_NAME, CMS_URL } from '../lib/constants'
export default function Intro() {
return (
<section className="flex flex-col items-center mt-16 mb-16 md:flex-row md:justify-between md:mb-12">
<h1 className="text-6xl font-bold leading-tight tracking-tighter md:text-8xl md:pr-8">
Blog.
</h1>
<h4 className="mt-5 text-lg text-center md:text-left md:pl-8">
A statically generated blog example using{' '}
<a
href="https://nextjs.org/"
className="underline transition-colors duration-200 hover:text-success"
>
Next.js
</a>{' '}
and{' '}
<a
href={CMS_URL}
className="underline transition-colors duration-200 hover:text-success"
>
{CMS_NAME}
</a>
.
</h4>
</section>
)
}
import Alert from '../components/alert'
import Footer from '../components/footer'
import Meta from '../components/meta'
export default function Layout({ preview, children }) {
return (
<>
<Meta />
<div className="min-h-screen">
<Alert preview={preview} />
<main>{children}</main>
</div>
<Footer />
</>
)
}
.markdown {
@apply text-lg leading-relaxed;
}
.markdown p,
.markdown ul,
.markdown ol,
.markdown blockquote {
@apply my-6;
}
.markdown h2 {
@apply mt-12 mb-4 text-3xl leading-snug;
}
.markdown h3 {
@apply mt-8 mb-4 text-2xl leading-snug;
}
import Head from 'next/head'
import { CMS_NAME, HOME_OG_IMAGE_URL } from '../lib/constants'
export default function Meta() {
return (
<Head>
<link
rel="apple-touch-icon"
sizes="180x180"
href="/favicons/apple-touch-icon.png"
/>
<link
rel="icon"
type="image/png"
sizes="32x32"
href="/favicons/favicon-32x32.png"
/>
<link
rel="icon"
type="image/png"
sizes="16x16"
href="/favicons/favicon-16x16.png"
/>
<link rel="manifest" href="/favicons/site.webmanifest" />
<link
rel="mask-icon"
href="/favicons/safari-pinned-tab.svg"
color="#000000"
/>
<link rel="shortcut icon" href="/favicons/favicon.ico" />
<meta name="msapplication-TileColor" content="#000000" />
<meta name="msapplication-config" content="/favicons/browserconfig.xml" />
<meta name="theme-color" content="#000" />
<link rel="alternate" type="application/rss+xml" href="/feed.xml" />
<meta
name="description"
content={`A statically generated blog example using Next.js and ${CMS_NAME}.`}
/>
<meta property="og:image" content={HOME_OG_IMAGE_URL} />
</Head>
)
}
import PostPreview from '../components/post-preview'
export default function MoreStories({ posts }) {
return (
<section>
<h2 className="mb-8 text-6xl font-bold leading-tight tracking-tighter md:text-7xl">
More Stories
</h2>
<div className="grid grid-cols-1 mb-32 md:grid-cols-2 md:gap-x-16 lg:gap-x-32 gap-y-20 md:gap-y-32">
{posts.map((post) => (
<PostPreview
key={post.slug}
title={post.title}
coverImage={post.featuredImage}
date={post.createdOn}
author={post.author}
slug={post.slug}
excerpt={post.excerpt}
/>
))}
</div>
</section>
)
}
import markdownStyles from './markdown-styles.module.css'
import { RichTextRenderer } from '../lib/rich-text-renderer'
export default function PostBody({ content }) {
return (
<div className="max-w-2xl mx-auto">
<div className={markdownStyles['markdown']}>
<RichTextRenderer data={content} />
</div>
</div>
)
}
import Avatar from '../components/avatar'
import DateFormatter from '../components/date-formatter'
import CoverImage from '../components/cover-image'
import PostTitle from '../components/post-title'
export default function PostHeader({ title, coverImage, date, author }) {
return (
<>
<PostTitle>{title}</PostTitle>
<div className="hidden md:block md:mb-12">
<Avatar name={author.name} picture={author.picture} />
</div>
<div className="mb-8 md:mb-16 sm:mx-0">
<CoverImage title={title} src={coverImage} height={620} width={1240} />
</div>
<div className="max-w-2xl mx-auto">
<div className="block mb-6 md:hidden">
<Avatar name={author.name} picture={author.picture} />
</div>
<div className="mb-6 text-lg">
<DateFormatter dateString={date} />
</div>
</div>
</>
)
}
import Avatar from '../components/avatar'
import DateFormatter from '../components/date-formatter'
import CoverImage from './cover-image'
import Link from 'next/link'
export default function PostPreview({
title,
coverImage,
date,
excerpt,
author,
slug,
}) {
return (
<div>
<div className="mb-5">
<CoverImage
slug={slug}
title={title}
src={coverImage}
height={278}
width={556}
/>
</div>
<h3 className="mb-3 text-3xl leading-snug">
<Link href={`/posts/${slug}`}>
<a className="hover:underline">{title}</a>
</Link>
</h3>
<div className="mb-4 text-lg">
<DateFormatter dateString={date} />
</div>
<p className="mb-4 text-lg leading-relaxed">{excerpt}</p>
<Avatar name={author.name} picture={author.picture} />
</div>
)
}
export default function PostTitle({ children }) {
return (
<h1 className="mb-12 text-6xl font-bold leading-tight tracking-tighter text-center md:text-7xl lg:text-8xl md:leading-none md:text-left">
{children}
</h1>
)
}
export default function SectionSeparator() {
return <hr className="mb-24 border-accent-2 mt-28" />
}
async function fetchAPI(
query: string,
{ variables } = {} as any,
preview: boolean
) {
const url = preview
? `${process.env.NEXT_PUBLIC_WEBINY_PREVIEW_API_URL}`
: `${process.env.NEXT_PUBLIC_WEBINY_API_UR}`
const res = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.WEBINY_API_SECRET}`,
},
body: JSON.stringify({
query,
variables,
}),
})
const json = await res.json()
if (json.errors) {
console.error(json.errors)
throw new Error('Failed to fetch API')
}
return json.data
}
export async function getAllPostsWithSlug() {
const data = await fetchAPI(
`
query PostSlugs {
listPosts {
data {
slug
}
}
}
`,
{},
false
)
return data?.listPosts.data
}
export async function getAllPostsForHome(preview) {
const data = await fetchAPI(
`
query Posts {
listPosts {
data {
id
title
slug
description
createdOn
featuredImage
author {
name
picture
}
}
}
}
`,
{},
preview
)
return data.listPosts.data
}
export async function getPostBySlug(slug, preview) {
const data = await fetchAPI(
`
query PostBySlug( $PostsGetWhereInput: PostsGetWhereInput!) {
post: getPosts( where: $PostsGetWhereInput ) {
data {
id
title
slug
description
createdOn
featuredImage
body
author {
name
picture
}
}
}
morePosts: listPosts(limit: 2, sort: createdOn_ASC) {
data {
id
title
slug
description
createdOn
featuredImage
author {
name
picture
}
}
}
}
`,
{
variables: {
PostsGetWhereInput: {
slug: slug,
},
},
},
preview
)
return data
}
export const EXAMPLE_PATH = 'cms-webiny'
export const CMS_NAME = 'Webiny'
export const CMS_URL = 'https://www.webiny.com/'
export const HOME_OG_IMAGE_URL =
'https://og-image.vercel.app/Next.js%20Blog%20Starter%20Example.png?theme=light&md=1&fontSize=100px&images=https%3A%2F%2Fassets.vercel.com%2Fimage%2Fupload%2Ffront%2Fassets%2Fdesign%2Fnextjs-black-logo.svg'
import * as React from 'react'
import classNames from 'classnames'
const renderParagraph = (block) => {
const props = { style: {}, className: '' }
if (block.data.textAlign) {
props.style['textAlign'] = block.data.textAlign
}
if (block.data.className) {
props.className = block.data.className
}
return (
<p
{...props}
className={classNames('rte-block-paragraph', props.className)}
dangerouslySetInnerHTML={{ __html: block.data.text }}
/>
)
}
const renderDelimiter = () => {
return <div className="rte-block-delimiter" />
}
const renderHeader = (block) => {
const props = { style: {}, className: '' }
if (block.data.textAlign) {
props.style['textAlign'] = block.data.textAlign
}
if (block.data.className) {
props.className = block.data.className
}
switch (block.data.level) {
case 1:
return (
<h1
{...props}
className={classNames(
props.className,
'rte-block-heading rte-block-heading--h1'
)}
dangerouslySetInnerHTML={{ __html: block.data.text }}
/>
)
case 2:
return (
<h2
{...props}
className={classNames(
props.className,
'rte-block-heading rte-block-heading--h2'
)}
dangerouslySetInnerHTML={{ __html: block.data.text }}
/>
)
case 3:
return (
<h3
{...props}
className={classNames(
props.className,
'rte-block-heading rte-block-heading--h3'
)}
dangerouslySetInnerHTML={{ __html: block.data.text }}
/>
)
case 4:
return (
<h4
{...props}
className={classNames(
props.className,
'rte-block-heading rte-block-heading--h4'
)}
dangerouslySetInnerHTML={{ __html: block.data.text }}
/>
)
case 5:
return (
<h5
{...props}
className={classNames(
props.className,
'rte-block-heading rte-block-heading--h5'
)}
dangerouslySetInnerHTML={{ __html: block.data.text }}
/>
)
case 6:
return (
<h6
{...props}
className={classNames(
props.className,
'rte-block-heading rte-block-heading--h6'
)}
dangerouslySetInnerHTML={{ __html: block.data.text }}
/>
)
default:
return null
}
}
function renderImage(block) {
return (
<img
className={'rte-block-image'}
alt={block.data.caption}
src={block.data.file}
/>
)
}
function renderList(block) {
switch (block.data.style) {
case 'unordered':
return (
<ul className={'rte-block-list'}>
{block.data.items.map((text, i) => (
<li key={i}>{text}</li>
))}
</ul>
)
case 'ordered':
return (
<ol className={'rte-block-list'}>
{block.data.items.map((text, i) => (
<li key={i}>{text}</li>
))}
</ol>
)
default:
return null
}
}
function renderQuote(block) {
return (
<blockquote className={'rte-block-blockquote'}>
<p>{block.data.text}</p>
</blockquote>
)
}
const defaultRenderers = {
delimiter: renderDelimiter,
header: renderHeader,
image: renderImage,
list: renderList,
paragraph: renderParagraph,
quote: renderQuote,
}
export const RichTextRenderer = (props) => {
// Combine default renderers with custom renderers
const renderers = Object.assign({}, defaultRenderers, props.renderers)
return (
<React.Fragment>
{props.data.map((block, index) => {
const renderer = renderers[block.type]
if (!renderer) {
return null
}
const node = renderer(block)
if (React.isValidElement(node)) {
return React.cloneElement(node, { key: index })
}
return null
})}
</React.Fragment>
)
}
module.exports = {
images: {
domains: ['d16nhgqznszc7v.cloudfront.net'],
},
}
{
"private": true,
"scripts": {
"dev": "next",
"build": "next build",
"start": "next start"
},
"dependencies": {
"classnames": "2.3.1",
"date-fns": "2.28.0",
"gray-matter": "4.0.3",
"next": "latest",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"remark": "14.0.2",
"remark-html": "15.0.1"
},
"devDependencies": {
"autoprefixer": "^10.4.2",
"postcss": "^8.4.7",
"tailwindcss": "^3.0.23"
}
}
import '../styles/globals.css'
export default function MyApp({ Component, pageProps }) {
return <Component {...pageProps} />
}
export default async function exit(_, res) {
// Exit the current user from "Preview Mode". This function accepts no args.
res.clearPreviewData()
// Redirect the user back to the index page.
res.writeHead(307, { Location: '/' })
res.end()
}
import { getPostBySlug } from '../../lib/api'
export default async function preview(req, res) {
// Check the secret and next parameters
// This secret should only be known to this API route and the CMS
if (req.query.secret !== process.env.PREVIEW_API_SECRET || !req.query.slug) {
return res.status(401).json({ message: 'Invalid token' })
}
// Fetch the headless CMS to check if the provided `slug` exists
const { post } = await getPostBySlug(req.query.slug, true)
// If the slug doesn't exist prevent preview mode from being enabled
if (!post) {
return res.status(401).json({ message: 'Invalid slug' })
}
// Enable Preview Mode by setting the cookies
res.setPreviewData({})
// Redirect to the path from the fetched post
// We don't redirect to req.query.slug as that might lead to open redirect vulnerabilities
res.writeHead(307, { Location: `/posts/${post.data.slug}` })
res.end()
}
import Container from '../components/container'
import MoreStories from '../components/more-stories'
import HeroPost from '../components/hero-post'
import Intro from '../components/intro'
import Layout from '../components/layout'
import { getAllPostsForHome } from '../lib/api'
import Head from 'next/head'
import { CMS_NAME } from '../lib/constants'
export default function Index({ allPosts, preview }) {
const heroPost = allPosts[0]
const morePosts = allPosts.slice(1)
return (
<>
<Layout preview={preview}>
<Head>
<title>Next.js Blog Example with {CMS_NAME}</title>
</Head>
<Container>
<Intro />
{heroPost && (
<HeroPost
title={heroPost.title}
coverImage={heroPost.featuredImage}
createdOn={heroPost.date}
author={heroPost.author}
slug={heroPost.slug}
excerpt={heroPost.excerpt}
/>
)}
{morePosts.length > 0 && <MoreStories posts={morePosts} />}
</Container>
</Layout>
</>
)
}
export async function getStaticProps({ preview }) {
const allPosts = await getAllPostsForHome(preview)
return {
props: { allPosts, preview },
}
}
import { useRouter } from 'next/router'
import ErrorPage from 'next/error'
import Container from '../../components/container'
import PostBody from '../../components/post-body'
import Header from '../../components/header'
import PostHeader from '../../components/post-header'
import MoreStories from '../../components/more-stories'
import SectionSeparator from '../../components/section-separator'
import Layout from '../../components/layout'
import { getPostBySlug, getAllPostsWithSlug } from '../../lib/api'
import PostTitle from '../../components/post-title'
import Head from 'next/head'
import { CMS_NAME } from '../../lib/constants'
export default function Post({ post, morePosts, preview }) {
const router = useRouter()
if (!router.isFallback && !post?.slug) {
return <ErrorPage statusCode={404} />
}
return (
<Layout preview={preview}>
<Container>
<Header />
{router.isFallback ? (
<PostTitle>Loading…</PostTitle>
) : (
<>
<article className="mb-32">
<Head>
<title>
{post.title} | Next.js Blog Example with {CMS_NAME}
</title>
<meta property="og:image" content={post.featuredImage} />
</Head>
<PostHeader
title={post.title}
coverImage={post.featuredImage}
date={post.createdOn}
author={post.author}
/>
<PostBody content={post.body} />
</article>
<SectionSeparator />
{morePosts.data.length > 0 && (
<MoreStories posts={morePosts.data} />
)}
</>
)}
</Container>
</Layout>
)
}
export async function getStaticProps(context) {
const data = await getPostBySlug(context.params.slug, context.preview)
return {
props: {
preview: context.preview ?? false,
post: {
...data.post.data,
},
morePosts: data.morePosts,
},
}
}
export async function getStaticPaths() {
const allPosts = await getAllPostsWithSlug()
return {
paths: allPosts.map((post) => {
return {
params: {
slug: `/posts/${post.slug}`,
},
}
}),
fallback: true,
}
}
// If you want to use other PostCSS plugins, see the following:
// https://tailwindcss.com/docs/using-with-preprocessors
module.exports = {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
}
<?xml version="1.0" encoding="utf-8"?>
<browserconfig>
<msapplication>
<tile>
<square150x150logo src="/favicons/mstile-150x150.png"/>
<TileColor>#000000</TileColor>
</tile>
</msapplication>
</browserconfig>
<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN"
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg version="1.0" xmlns="http://www.w3.org/2000/svg"
width="1024.000000pt" height="1024.000000pt" viewBox="0 0 1024.000000 1024.000000"
preserveAspectRatio="xMidYMid meet">
<metadata>
Created by potrace 1.11, written by Peter Selinger 2001-2013
</metadata>
<g transform="translate(0.000000,1024.000000) scale(0.100000,-0.100000)"
fill="#000000" stroke="none">
<path d="M4785 10234 c-22 -2 -92 -9 -155 -14 -1453 -131 -2814 -915 -3676
-2120 -480 -670 -787 -1430 -903 -2235 -41 -281 -46 -364 -46 -745 0 -381 5
-464 46 -745 278 -1921 1645 -3535 3499 -4133 332 -107 682 -180 1080 -224
155 -17 825 -17 980 0 687 76 1269 246 1843 539 88 45 105 57 93 67 -8 6 -383
509 -833 1117 l-818 1105 -1025 1517 c-564 834 -1028 1516 -1032 1516 -4 1 -8
-673 -10 -1496 -3 -1441 -4 -1499 -22 -1533 -26 -49 -46 -69 -88 -91 -32 -16
-60 -19 -211 -19 l-173 0 -46 29 c-30 19 -52 44 -67 73 l-21 45 2 2005 3 2006
31 39 c16 21 50 48 74 61 41 20 57 22 230 22 204 0 238 -8 291 -66 15 -16 570
-852 1234 -1859 664 -1007 1572 -2382 2018 -3057 l810 -1227 41 27 c363 236
747 572 1051 922 647 743 1064 1649 1204 2615 41 281 46 364 46 745 0 381 -5
464 -46 745 -278 1921 -1645 3535 -3499 4133 -327 106 -675 179 -1065 223 -96
10 -757 21 -840 13z m2094 -3094 c48 -24 87 -70 101 -118 8 -26 10 -582 8
-1835 l-3 -1798 -317 486 -318 486 0 1307 c0 845 4 1320 10 1343 16 56 51 100
99 126 41 21 56 23 213 23 148 0 174 -2 207 -20z"/>
<path d="M7843 789 c-35 -22 -46 -37 -15 -20 22 13 58 40 52 41 -3 0 -20 -10
-37 -21z"/>
<path d="M7774 744 c-18 -14 -18 -15 4 -4 12 6 22 13 22 15 0 8 -5 6 -26 -11z"/>
<path d="M7724 714 c-18 -14 -18 -15 4 -4 12 6 22 13 22 15 0 8 -5 6 -26 -11z"/>
<path d="M7674 684 c-18 -14 -18 -15 4 -4 12 6 22 13 22 15 0 8 -5 6 -26 -11z"/>
<path d="M7598 644 c-38 -20 -36 -28 2 -9 17 9 30 18 30 20 0 7 -1 6 -32 -11z"/>
</g>
</svg>
{
"name": "Next.js",
"short_name": "Next.js",
"icons": [
{
"src": "/favicons/android-chrome-192x192.png",
"sizes": "192x192",
"type": "image/png"
},
{
"src": "/favicons/android-chrome-512x512.png",
"sizes": "512x512",
"type": "image/png"
}
],
"theme_color": "#000000",
"background_color": "#000000",
"display": "standalone"
}
@tailwind base;
@tailwind components;
@tailwind utilities;
/** @type {import('tailwindcss').Config} */
module.exports = {
content: [
'./components/**/*.{js,ts,jsx,tsx}',
'./pages/**/*.{js,ts,jsx,tsx}',
],
theme: {
extend: {
colors: {
'accent-1': '#FAFAFA',
'accent-2': '#EAEAEA',
'accent-7': '#333',
success: '#0070f3',
cyan: '#79FFE1',
},
spacing: {
28: '7rem',
},
letterSpacing: {
tighter: '-.04em',
},
lineHeight: {
tight: 1.2,
},
fontSize: {
'5xl': '2.5rem',
'6xl': '2.75rem',
'7xl': '4.5rem',
'8xl': '6.25rem',
},
boxShadow: {
sm: '0 5px 10px rgba(0, 0, 0, 0.12)',
md: '0 8px 30px rgba(0, 0, 0, 0.12)',
},
},
},
}
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"incremental": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve"
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment