Skip to content

Commit

Permalink
Update cms-makeswift example (vercel#41005)
Browse files Browse the repository at this point in the history
Makeswift now uses Next.js' Preview Mode so there's no need for a
preview route. It also supports automatic on-demand revalidation with
the introduction of the Makeswift API handler.

## 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)
  • Loading branch information
migueloller authored and BowlingX committed Oct 5, 2022
1 parent 6d18776 commit 4b7900a
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 11 deletions.
1 change: 1 addition & 0 deletions examples/cms-makeswift/next.config.js
Expand Up @@ -3,6 +3,7 @@ const withMakeswift = require('@makeswift/runtime/next/plugin')()
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
swcMinify: true,
}

module.exports = withMakeswift(nextConfig)
2 changes: 1 addition & 1 deletion examples/cms-makeswift/package.json
Expand Up @@ -6,7 +6,7 @@
"start": "next start"
},
"dependencies": {
"@makeswift/runtime": "0.1.10",
"@makeswift/runtime": "0.2.2",
"next": "latest",
"react": "18.2.0",
"react-dom": "18.2.0"
Expand Down
7 changes: 0 additions & 7 deletions examples/cms-makeswift/pages/[[...path]].ts

This file was deleted.

51 changes: 51 additions & 0 deletions examples/cms-makeswift/pages/[[...path]].tsx
@@ -0,0 +1,51 @@
import '../lib/makeswift/register-components'

import { Makeswift } from '@makeswift/runtime/next'
import {
GetStaticPathsResult,
GetStaticPropsContext,
GetStaticPropsResult,
} from 'next'

import {
Page as MakeswiftPage,
PageProps as MakeswiftPageProps,
} from '@makeswift/runtime/next'

type ParsedUrlQuery = { path?: string[] }

export async function getStaticPaths(): Promise<
GetStaticPathsResult<ParsedUrlQuery>
> {
const makeswift = new Makeswift(process.env.MAKESWIFT_SITE_API_KEY!)
const pages = await makeswift.getPages()

return {
paths: pages.map((page) => ({
params: {
path: page.path.split('/').filter((segment) => segment !== ''),
},
})),
fallback: 'blocking',
}
}

type Props = MakeswiftPageProps

export async function getStaticProps(
ctx: GetStaticPropsContext<ParsedUrlQuery>
): Promise<GetStaticPropsResult<Props>> {
const makeswift = new Makeswift(process.env.MAKESWIFT_SITE_API_KEY!)
const path = '/' + (ctx.params?.path ?? []).join('/')
const snapshot = await makeswift.getPageSnapshot(path, {
preview: ctx.preview,
})

if (snapshot == null) return { notFound: true }

return { props: { snapshot } }
}

export default function Page({ snapshot }: Props) {
return <MakeswiftPage snapshot={snapshot} />
}
3 changes: 3 additions & 0 deletions examples/cms-makeswift/pages/api/makeswift/[...makeswift].ts
@@ -0,0 +1,3 @@
import { MakeswiftApiHandler } from '@makeswift/runtime/next'

export default MakeswiftApiHandler(process.env.MAKESWIFT_SITE_API_KEY!)
3 changes: 0 additions & 3 deletions examples/cms-makeswift/pages/makeswift.ts

This file was deleted.

0 comments on commit 4b7900a

Please sign in to comment.