-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1d93439
commit cbc931b
Showing
15 changed files
with
1,071 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,42 @@ | ||
import { Page as MakeswiftPage } from '@makeswift/runtime/next'; | ||
import { getSiteVersion } from '@makeswift/runtime/next/server'; | ||
import { notFound } from 'next/navigation'; | ||
|
||
export default function CatchAllPage() { | ||
notFound(); | ||
import { defaultLocale, locales } from '~/i18n/routing'; | ||
import { client } from '~/lib/makeswift/client'; | ||
|
||
export async function generateStaticParams() { | ||
const pages = await client.getPages().toArray(); | ||
|
||
return pages | ||
.filter((page) => page.path !== '/') | ||
.flatMap((page) => | ||
locales.map((locale) => ({ | ||
rest: page.path.split('/').filter((segment) => segment !== ''), | ||
locale: locale === defaultLocale ? undefined : locale, | ||
})), | ||
); | ||
} | ||
|
||
interface Props { | ||
params: Promise<{ | ||
locale: string; | ||
rest: string[]; | ||
}>; | ||
} | ||
|
||
export default async function CatchAllPage({ params }: Props) { | ||
const { rest, locale } = await params; | ||
const path = `/${rest.join('/')}`; | ||
|
||
const snapshot = await client.getPageSnapshot(path, { | ||
siteVersion: await getSiteVersion(), | ||
locale: locale === defaultLocale ? undefined : locale, | ||
}); | ||
|
||
if (snapshot == null) return notFound(); | ||
|
||
return <MakeswiftPage snapshot={snapshot} />; | ||
} | ||
|
||
export const runtime = 'nodejs'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { MakeswiftApiHandler } from '@makeswift/runtime/next/server'; | ||
import { strict } from 'assert'; | ||
|
||
import { runtime } from '~/lib/makeswift/runtime'; | ||
|
||
strict(process.env.MAKESWIFT_SITE_API_KEY, 'MAKESWIFT_SITE_API_KEY is required'); | ||
|
||
const handler = MakeswiftApiHandler(process.env.MAKESWIFT_SITE_API_KEY, { | ||
runtime, | ||
apiOrigin: process.env.MAKESWIFT_API_ORIGIN, | ||
appOrigin: process.env.MAKESWIFT_APP_ORIGIN, | ||
}); | ||
|
||
export { handler as GET, handler as POST }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { draftMode } from 'next/headers'; | ||
import { NextRequest } from 'next/server'; | ||
|
||
export const GET = async (request: NextRequest) => { | ||
if (request.headers.get('x-makeswift-api-key') === process.env.MAKESWIFT_SITE_API_KEY) { | ||
const draft = await draftMode(); | ||
|
||
draft.enable(); | ||
} | ||
|
||
return new Response(null); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Makeswift } from '@makeswift/runtime/next'; | ||
import { strict } from 'assert'; | ||
|
||
import { runtime } from '~/lib/makeswift/runtime'; | ||
|
||
strict(process.env.MAKESWIFT_SITE_API_KEY, 'MAKESWIFT_SITE_API_KEY is required'); | ||
|
||
export const client = new Makeswift(process.env.MAKESWIFT_SITE_API_KEY, { | ||
runtime, | ||
apiOrigin: process.env.MAKESWIFT_API_ORIGIN, | ||
}); |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
'use client'; | ||
|
||
import { ReactRuntimeProvider, RootStyleRegistry } from '@makeswift/runtime/next'; | ||
|
||
import { runtime } from '~/lib/makeswift/runtime'; | ||
import '~/lib/makeswift/components'; | ||
|
||
export function MakeswiftProvider({ | ||
children, | ||
previewMode, | ||
}: { | ||
children: React.ReactNode; | ||
previewMode: boolean; | ||
}) { | ||
return ( | ||
<ReactRuntimeProvider previewMode={previewMode} runtime={runtime}> | ||
<RootStyleRegistry>{children}</RootStyleRegistry> | ||
</ReactRuntimeProvider> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { ReactRuntime } from '@makeswift/runtime/react'; | ||
|
||
export const runtime = new ReactRuntime(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { NextRequest } from 'next/server'; | ||
import { parse as parseSetCookie } from 'set-cookie-parser'; | ||
|
||
import { MiddlewareFactory } from './compose-middlewares'; | ||
|
||
export const withMakeswift: MiddlewareFactory = (middleware) => { | ||
return async (request, event) => { | ||
const apiKey = request.nextUrl.searchParams.get('x-makeswift-draft-mode'); | ||
|
||
if (apiKey === process.env.MAKESWIFT_SITE_API_KEY) { | ||
const response = await fetch(new URL('/api/makeswift/draft-mode', request.nextUrl.origin), { | ||
headers: { | ||
'x-makeswift-api-key': apiKey, | ||
}, | ||
}); | ||
|
||
const cookies = parseSetCookie(response.headers.get('set-cookie') || ''); | ||
const prerenderBypassValue = cookies.find((c) => c.name === '__prerender_bypass')?.value; | ||
|
||
if (!prerenderBypassValue) { | ||
return middleware(request, event); | ||
} | ||
|
||
// https://github.com/vercel/next.js/issues/52967#issuecomment-1644675602 | ||
// if we don't pass request twice, headers are stripped | ||
const proxiedRequest = new NextRequest(request, request); | ||
|
||
proxiedRequest.cookies.set('__prerender_bypass', prerenderBypassValue); | ||
proxiedRequest.cookies.set( | ||
'x-makeswift-draft-data', | ||
JSON.stringify({ makeswift: true, siteVersion: 'Working' }), | ||
); | ||
|
||
return middleware(proxiedRequest, event); | ||
} | ||
|
||
return middleware(request, event); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.