From 4b7900a7b3997addd45b605b9cbb5d465ef9369b Mon Sep 17 00:00:00 2001 From: Miguel Oller Date: Wed, 28 Sep 2022 14:33:08 -0400 Subject: [PATCH] Update cms-makeswift example (#41005) 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) --- examples/cms-makeswift/next.config.js | 1 + examples/cms-makeswift/package.json | 2 +- examples/cms-makeswift/pages/[[...path]].ts | 7 --- examples/cms-makeswift/pages/[[...path]].tsx | 51 +++++++++++++++++++ .../pages/api/makeswift/[...makeswift].ts | 3 ++ examples/cms-makeswift/pages/makeswift.ts | 3 -- 6 files changed, 56 insertions(+), 11 deletions(-) delete mode 100644 examples/cms-makeswift/pages/[[...path]].ts create mode 100644 examples/cms-makeswift/pages/[[...path]].tsx create mode 100644 examples/cms-makeswift/pages/api/makeswift/[...makeswift].ts delete mode 100644 examples/cms-makeswift/pages/makeswift.ts diff --git a/examples/cms-makeswift/next.config.js b/examples/cms-makeswift/next.config.js index 31c157001e778..4894cbefb913e 100644 --- a/examples/cms-makeswift/next.config.js +++ b/examples/cms-makeswift/next.config.js @@ -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) diff --git a/examples/cms-makeswift/package.json b/examples/cms-makeswift/package.json index e7b953008c940..e02e952e2cb58 100644 --- a/examples/cms-makeswift/package.json +++ b/examples/cms-makeswift/package.json @@ -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" diff --git a/examples/cms-makeswift/pages/[[...path]].ts b/examples/cms-makeswift/pages/[[...path]].ts deleted file mode 100644 index f26a401b5fe91..0000000000000 --- a/examples/cms-makeswift/pages/[[...path]].ts +++ /dev/null @@ -1,7 +0,0 @@ -import '../lib/makeswift/register-components' - -export { - getStaticPaths, - getStaticProps, - Page as default, -} from '@makeswift/runtime/next' diff --git a/examples/cms-makeswift/pages/[[...path]].tsx b/examples/cms-makeswift/pages/[[...path]].tsx new file mode 100644 index 0000000000000..f8edbde7e886d --- /dev/null +++ b/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 +> { + 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 +): Promise> { + 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 +} diff --git a/examples/cms-makeswift/pages/api/makeswift/[...makeswift].ts b/examples/cms-makeswift/pages/api/makeswift/[...makeswift].ts new file mode 100644 index 0000000000000..edf4ad5ccae54 --- /dev/null +++ b/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!) diff --git a/examples/cms-makeswift/pages/makeswift.ts b/examples/cms-makeswift/pages/makeswift.ts deleted file mode 100644 index 365f94474357b..0000000000000 --- a/examples/cms-makeswift/pages/makeswift.ts +++ /dev/null @@ -1,3 +0,0 @@ -import '../lib/makeswift/register-components' - -export { getServerSideProps, Page as default } from '@makeswift/runtime/next'