@butterflypress/nextjs 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chunk-IFQHJL33.js +97 -0
- package/dist/chunk-IFQHJL33.js.map +1 -0
- package/dist/chunk-ROFORVTO.js +116 -0
- package/dist/chunk-ROFORVTO.js.map +1 -0
- package/dist/client.cjs +144 -0
- package/dist/client.cjs.map +1 -0
- package/dist/client.d.cts +42 -0
- package/dist/client.d.ts +42 -0
- package/dist/client.js +116 -0
- package/dist/client.js.map +1 -0
- package/dist/index.cjs +556 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +39 -0
- package/dist/index.d.ts +39 -0
- package/dist/index.js +243 -0
- package/dist/index.js.map +1 -0
- package/dist/metadata.cjs +123 -0
- package/dist/metadata.cjs.map +1 -0
- package/dist/metadata.d.cts +43 -0
- package/dist/metadata.d.ts +43 -0
- package/dist/metadata.js +3 -0
- package/dist/metadata.js.map +1 -0
- package/dist/server.cjs +369 -0
- package/dist/server.cjs.map +1 -0
- package/dist/server.d.cts +87 -0
- package/dist/server.d.ts +87 -0
- package/dist/server.js +310 -0
- package/dist/server.js.map +1 -0
- package/dist/ssr.cjs +100 -0
- package/dist/ssr.cjs.map +1 -0
- package/dist/ssr.d.cts +39 -0
- package/dist/ssr.d.ts +39 -0
- package/dist/ssr.js +3 -0
- package/dist/ssr.js.map +1 -0
- package/package.json +81 -3
- package/src/client.ts +4 -0
- package/src/components/BlockRenderer.tsx +18 -0
- package/src/components/DynamicForm.tsx +54 -0
- package/src/components/PreviewBanner.tsx +99 -0
- package/src/components/SectionWrapper.tsx +1 -0
- package/src/components/StreamFieldRenderer.tsx +8 -0
- package/src/index.ts +54 -0
- package/src/metadata/index.ts +177 -0
- package/src/primitives/BpAction.tsx +78 -0
- package/src/primitives/BpImage.tsx +200 -0
- package/src/primitives/BpLink.tsx +7 -0
- package/src/server.ts +470 -0
- package/src/ssr/index.ts +167 -0
- package/styles.css +61 -0
- package/README.md +0 -3
package/src/server.ts
ADDED
|
@@ -0,0 +1,470 @@
|
|
|
1
|
+
import crypto from "node:crypto"
|
|
2
|
+
import React from "react"
|
|
3
|
+
import type { Metadata } from "next"
|
|
4
|
+
import { cookies, draftMode } from "next/headers"
|
|
5
|
+
import { NextResponse, type NextRequest } from "next/server"
|
|
6
|
+
import {
|
|
7
|
+
ButterflyPressApiError,
|
|
8
|
+
createButterflyPressClient,
|
|
9
|
+
createPathLocalizer,
|
|
10
|
+
isRootPageSlug,
|
|
11
|
+
localizeMenuItems,
|
|
12
|
+
normalizeLocale,
|
|
13
|
+
resolveInternalUrl,
|
|
14
|
+
type ButterflyPressClient,
|
|
15
|
+
type ButterflyPressClientOptions,
|
|
16
|
+
type MenuPayload,
|
|
17
|
+
type PagePayload,
|
|
18
|
+
type SiteConfigPayload
|
|
19
|
+
} from "@butterflypress/core"
|
|
20
|
+
import { buildNextMetadata, type BuildNextMetadataOptions } from "./metadata/index"
|
|
21
|
+
|
|
22
|
+
export const BUTTERFLYPRESS_PREVIEW_COOKIE = "bp_preview_context"
|
|
23
|
+
|
|
24
|
+
const CONTENT_TYPE_PATTERN = /^[a-z0-9_]+\.[a-z0-9_]+$/i
|
|
25
|
+
const TOKEN_PATTERN = /^[a-zA-Z0-9._:-]+$/
|
|
26
|
+
|
|
27
|
+
export interface DraftPreviewCookieContext {
|
|
28
|
+
contentType: string
|
|
29
|
+
token: string
|
|
30
|
+
pageId?: number
|
|
31
|
+
slug?: string
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface ButterflyPressServerClientOptions extends ButterflyPressClientOptions {
|
|
35
|
+
revalidate?: number | false
|
|
36
|
+
tags?: string[]
|
|
37
|
+
cache?: RequestCache
|
|
38
|
+
preview?: boolean
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function createServerButterflyPressClient(options: ButterflyPressServerClientOptions): ButterflyPressClient {
|
|
42
|
+
const { revalidate = 60, tags, cache, preview = false, fetchImpl, ...clientOptions } = options
|
|
43
|
+
const baseFetch = fetchImpl || globalThis.fetch
|
|
44
|
+
|
|
45
|
+
const wrappedFetch: typeof fetch = (input, init) => {
|
|
46
|
+
const nextInit: RequestInit & { next?: { revalidate?: number | false; tags?: string[] } } = {
|
|
47
|
+
...init
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (preview) {
|
|
51
|
+
nextInit.cache = "no-store"
|
|
52
|
+
} else {
|
|
53
|
+
if (cache) {
|
|
54
|
+
nextInit.cache = cache
|
|
55
|
+
}
|
|
56
|
+
nextInit.next = {
|
|
57
|
+
...(nextInit.next || {}),
|
|
58
|
+
revalidate,
|
|
59
|
+
...(tags ? { tags } : {})
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return baseFetch(input, nextInit)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return createButterflyPressClient({
|
|
67
|
+
...clientOptions,
|
|
68
|
+
fetchImpl: wrappedFetch
|
|
69
|
+
})
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export async function getDraftPreviewContext(): Promise<{
|
|
73
|
+
isEnabled: boolean
|
|
74
|
+
context: DraftPreviewCookieContext | null
|
|
75
|
+
}> {
|
|
76
|
+
let isEnabled = false
|
|
77
|
+
try {
|
|
78
|
+
const dm = await Promise.resolve(draftMode())
|
|
79
|
+
isEnabled = Boolean(dm?.isEnabled)
|
|
80
|
+
} catch {
|
|
81
|
+
return { isEnabled: false, context: null }
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (!isEnabled) {
|
|
85
|
+
return { isEnabled: false, context: null }
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
try {
|
|
89
|
+
const cookieStore = await Promise.resolve(cookies())
|
|
90
|
+
const raw = cookieStore.get(BUTTERFLYPRESS_PREVIEW_COOKIE)?.value
|
|
91
|
+
if (!raw) {
|
|
92
|
+
return { isEnabled: true, context: null }
|
|
93
|
+
}
|
|
94
|
+
const parsed = JSON.parse(raw) as Partial<DraftPreviewCookieContext>
|
|
95
|
+
if (
|
|
96
|
+
parsed &&
|
|
97
|
+
typeof parsed.contentType === "string" &&
|
|
98
|
+
typeof parsed.token === "string" &&
|
|
99
|
+
CONTENT_TYPE_PATTERN.test(parsed.contentType) &&
|
|
100
|
+
TOKEN_PATTERN.test(parsed.token)
|
|
101
|
+
) {
|
|
102
|
+
return {
|
|
103
|
+
isEnabled: true,
|
|
104
|
+
context: {
|
|
105
|
+
contentType: parsed.contentType,
|
|
106
|
+
token: parsed.token,
|
|
107
|
+
...(typeof parsed.pageId === "number" ? { pageId: parsed.pageId } : {}),
|
|
108
|
+
...(typeof parsed.slug === "string" ? { slug: parsed.slug } : {})
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
} catch {
|
|
113
|
+
// Ignore invalid preview cookie
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return { isEnabled: true, context: null }
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export interface ResolveNextPageDataOptions extends ButterflyPressServerClientOptions {
|
|
120
|
+
slug?: string | string[]
|
|
121
|
+
locale?: string
|
|
122
|
+
contentType?: string
|
|
123
|
+
token?: string
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export interface ResolvedNextPageData {
|
|
127
|
+
site: SiteConfigPayload | null
|
|
128
|
+
siteConfig: SiteConfigPayload | null
|
|
129
|
+
page: PagePayload | null
|
|
130
|
+
navbarMenu: MenuPayload | null
|
|
131
|
+
footerMenu: MenuPayload | null
|
|
132
|
+
locale: string
|
|
133
|
+
localizePath: (path: string) => string
|
|
134
|
+
isDraftMode: boolean
|
|
135
|
+
previewContext: DraftPreviewCookieContext | null
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function hydrateLocalizedMenu(
|
|
139
|
+
menu: MenuPayload | null | undefined,
|
|
140
|
+
localizePath: (path: string) => string
|
|
141
|
+
): MenuPayload | null {
|
|
142
|
+
if (!menu) return null
|
|
143
|
+
return {
|
|
144
|
+
...menu,
|
|
145
|
+
items: localizeMenuItems(menu.items || [], localizePath)
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
async function resolveNextPageDataImpl(options: ResolveNextPageDataOptions): Promise<ResolvedNextPageData> {
|
|
150
|
+
const draftState = await getDraftPreviewContext()
|
|
151
|
+
const previewContext =
|
|
152
|
+
options.contentType && options.token
|
|
153
|
+
? { contentType: options.contentType, token: options.token }
|
|
154
|
+
: draftState.context
|
|
155
|
+
const isDraftMode = Boolean(options.preview || draftState.isEnabled)
|
|
156
|
+
|
|
157
|
+
const client = createServerButterflyPressClient({
|
|
158
|
+
...options,
|
|
159
|
+
preview: isDraftMode
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
const rawSlug = options.slug
|
|
163
|
+
const slugParts = Array.isArray(rawSlug) ? rawSlug : rawSlug ? [rawSlug] : []
|
|
164
|
+
const htmlPath = slugParts.join("/")
|
|
165
|
+
const targetSlug = slugParts[slugParts.length - 1] || "home"
|
|
166
|
+
|
|
167
|
+
const activeLocale = normalizeLocale(options.locale, client.defaultLocale)
|
|
168
|
+
const localizePath = createPathLocalizer(activeLocale, client.defaultLocale)
|
|
169
|
+
|
|
170
|
+
let pagePromise: Promise<PagePayload | null>
|
|
171
|
+
if (isDraftMode && previewContext) {
|
|
172
|
+
pagePromise = client.getPagePreview(previewContext.contentType, previewContext.token)
|
|
173
|
+
} else if (htmlPath) {
|
|
174
|
+
pagePromise = client.findPageByHtmlPath(htmlPath, activeLocale).then((found) => {
|
|
175
|
+
if (found) return found
|
|
176
|
+
return client.getPageBySlug(targetSlug, activeLocale)
|
|
177
|
+
})
|
|
178
|
+
} else {
|
|
179
|
+
pagePromise = client.getPageBySlug(targetSlug, activeLocale)
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const [site, page, directNavbarMenu, directFooterMenu] = await Promise.all([
|
|
183
|
+
client.getSiteConfig(undefined, activeLocale),
|
|
184
|
+
pagePromise,
|
|
185
|
+
client.getMenu("navbar", activeLocale),
|
|
186
|
+
client.getMenu("footer", activeLocale)
|
|
187
|
+
])
|
|
188
|
+
|
|
189
|
+
const rawNavbar = site?.navigationsettings?.navbarMenu || site?.navigationsettings?.navbar_menu || directNavbarMenu
|
|
190
|
+
const rawFooter =
|
|
191
|
+
site?.navigationsettings?.footerMenu ||
|
|
192
|
+
site?.navigationsettings?.footer_menu ||
|
|
193
|
+
site?.navigationsettings?.footerPrimaryMenu ||
|
|
194
|
+
site?.navigationsettings?.footer_primary_menu ||
|
|
195
|
+
directFooterMenu
|
|
196
|
+
|
|
197
|
+
return {
|
|
198
|
+
site,
|
|
199
|
+
siteConfig: site,
|
|
200
|
+
page,
|
|
201
|
+
navbarMenu: hydrateLocalizedMenu(rawNavbar, localizePath),
|
|
202
|
+
footerMenu: hydrateLocalizedMenu(rawFooter, localizePath),
|
|
203
|
+
locale: activeLocale,
|
|
204
|
+
localizePath,
|
|
205
|
+
isDraftMode,
|
|
206
|
+
previewContext
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const cacheFn =
|
|
211
|
+
typeof (React as any).cache === "function"
|
|
212
|
+
? (React as any).cache
|
|
213
|
+
: <T extends (...args: any[]) => any>(fn: T): T => fn
|
|
214
|
+
|
|
215
|
+
export const resolveNextPageData = cacheFn(resolveNextPageDataImpl)
|
|
216
|
+
|
|
217
|
+
export interface GetButterflyPressStaticParamsOptions extends ButterflyPressServerClientOptions {
|
|
218
|
+
limit?: number
|
|
219
|
+
locale?: string
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
export async function getButterflyPressStaticParams(
|
|
223
|
+
options: GetButterflyPressStaticParamsOptions
|
|
224
|
+
): Promise<Array<{ slug: string[] }>> {
|
|
225
|
+
const client = createServerButterflyPressClient(options)
|
|
226
|
+
const { items } = await client.getPages({
|
|
227
|
+
limit: options.limit ?? 100,
|
|
228
|
+
locale: options.locale
|
|
229
|
+
})
|
|
230
|
+
|
|
231
|
+
return items
|
|
232
|
+
.map((page) => {
|
|
233
|
+
const htmlUrl = page.meta?.htmlUrl || page.meta?.html_url
|
|
234
|
+
if (htmlUrl) {
|
|
235
|
+
const local = resolveInternalUrl(htmlUrl, undefined, { isPageLink: true })
|
|
236
|
+
const parts = local.split("/").filter(Boolean)
|
|
237
|
+
return { slug: parts }
|
|
238
|
+
}
|
|
239
|
+
const slug = page.meta?.slug
|
|
240
|
+
if (!slug || isRootPageSlug(slug, page.meta?.type)) {
|
|
241
|
+
return { slug: [] }
|
|
242
|
+
}
|
|
243
|
+
return { slug: [slug] }
|
|
244
|
+
})
|
|
245
|
+
.filter((entry, index, arr) => {
|
|
246
|
+
const key = entry.slug.join("/")
|
|
247
|
+
return arr.findIndex((other) => other.slug.join("/") === key) === index
|
|
248
|
+
})
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
export function generateButterflyPressMetadata(baseOptions: ResolveNextPageDataOptions & BuildNextMetadataOptions) {
|
|
252
|
+
return async function generateMetadata({
|
|
253
|
+
params
|
|
254
|
+
}: {
|
|
255
|
+
params: Promise<{ slug?: string | string[]; locale?: string }> | { slug?: string | string[]; locale?: string }
|
|
256
|
+
}): Promise<Metadata> {
|
|
257
|
+
const resolvedParams = await Promise.resolve(params)
|
|
258
|
+
const data = await resolveNextPageData({
|
|
259
|
+
...baseOptions,
|
|
260
|
+
slug: resolvedParams?.slug ?? baseOptions.slug,
|
|
261
|
+
locale: resolvedParams?.locale ?? baseOptions.locale
|
|
262
|
+
})
|
|
263
|
+
|
|
264
|
+
return buildNextMetadata(data.page, data.siteConfig, {
|
|
265
|
+
...baseOptions,
|
|
266
|
+
locale: data.locale,
|
|
267
|
+
isDraftMode: data.isDraftMode
|
|
268
|
+
})
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
export function sanitizeRedirectPath(rawPath?: string | null): string {
|
|
273
|
+
if (!rawPath || typeof rawPath !== "string") {
|
|
274
|
+
return "/"
|
|
275
|
+
}
|
|
276
|
+
const trimmed = rawPath.trim()
|
|
277
|
+
if (
|
|
278
|
+
!trimmed ||
|
|
279
|
+
!trimmed.startsWith("/") ||
|
|
280
|
+
trimmed.startsWith("//") ||
|
|
281
|
+
trimmed.includes("\\") ||
|
|
282
|
+
/[\x00-\x1f\x7f]/.test(trimmed)
|
|
283
|
+
) {
|
|
284
|
+
return "/"
|
|
285
|
+
}
|
|
286
|
+
try {
|
|
287
|
+
const parsed = new URL(trimmed, "http://localhost")
|
|
288
|
+
if (parsed.origin !== "http://localhost") {
|
|
289
|
+
return "/"
|
|
290
|
+
}
|
|
291
|
+
return `${parsed.pathname}${parsed.search}${parsed.hash}`
|
|
292
|
+
} catch {
|
|
293
|
+
return "/"
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
function timingSafeSecretEqual(expected: string, actual: string): boolean {
|
|
298
|
+
const expectedBuf = Buffer.from(expected, "utf8")
|
|
299
|
+
const actualBuf = Buffer.from(actual, "utf8")
|
|
300
|
+
if (expectedBuf.length !== actualBuf.length) {
|
|
301
|
+
return false
|
|
302
|
+
}
|
|
303
|
+
return crypto.timingSafeEqual(expectedBuf, actualBuf)
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
export interface PreviewHandlerOptions extends ButterflyPressServerClientOptions {
|
|
307
|
+
secret?: string
|
|
308
|
+
cookieName?: string
|
|
309
|
+
maxAgeSeconds?: number
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
export type VerifiedPreviewResult =
|
|
313
|
+
| {
|
|
314
|
+
ok: true
|
|
315
|
+
contentType: string
|
|
316
|
+
token: string
|
|
317
|
+
page: PagePayload
|
|
318
|
+
redirectPath: string
|
|
319
|
+
}
|
|
320
|
+
| {
|
|
321
|
+
ok: false
|
|
322
|
+
status: number
|
|
323
|
+
error: string
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
function extractParamsFromRequestOrRecord(
|
|
327
|
+
requestOrParams: Request | URL | Record<string, string | undefined>
|
|
328
|
+
): Record<string, string | undefined> {
|
|
329
|
+
if (requestOrParams instanceof URL) {
|
|
330
|
+
const out: Record<string, string | undefined> = {}
|
|
331
|
+
requestOrParams.searchParams.forEach((v, k) => {
|
|
332
|
+
out[k] = v
|
|
333
|
+
})
|
|
334
|
+
return out
|
|
335
|
+
}
|
|
336
|
+
if (typeof Request !== "undefined" && requestOrParams instanceof Request) {
|
|
337
|
+
const url = new URL(requestOrParams.url)
|
|
338
|
+
const out: Record<string, string | undefined> = {}
|
|
339
|
+
url.searchParams.forEach((v, k) => {
|
|
340
|
+
out[k] = v
|
|
341
|
+
})
|
|
342
|
+
return out
|
|
343
|
+
}
|
|
344
|
+
return requestOrParams as Record<string, string | undefined>
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
export async function verifyPreviewRequest(
|
|
348
|
+
requestOrParams: Request | URL | Record<string, string | undefined>,
|
|
349
|
+
options: PreviewHandlerOptions
|
|
350
|
+
): Promise<VerifiedPreviewResult> {
|
|
351
|
+
const params = extractParamsFromRequestOrRecord(requestOrParams)
|
|
352
|
+
const expectedSecret = options.secret
|
|
353
|
+
if (expectedSecret) {
|
|
354
|
+
const providedSecret = params.secret || ""
|
|
355
|
+
if (!providedSecret || !timingSafeSecretEqual(expectedSecret, providedSecret)) {
|
|
356
|
+
return { ok: false, status: 401, error: "Invalid preview secret" }
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
const contentType = (params.content_type || params.contentType || "").trim()
|
|
361
|
+
const token = (params.token || "").trim()
|
|
362
|
+
|
|
363
|
+
if (!contentType || !CONTENT_TYPE_PATTERN.test(contentType)) {
|
|
364
|
+
return { ok: false, status: 400, error: "Invalid or missing content_type" }
|
|
365
|
+
}
|
|
366
|
+
if (!token || !TOKEN_PATTERN.test(token)) {
|
|
367
|
+
return { ok: false, status: 400, error: "Invalid or missing token" }
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
const client = createServerButterflyPressClient({ ...options, preview: true })
|
|
371
|
+
try {
|
|
372
|
+
const page = await client.getPagePreview(contentType, token)
|
|
373
|
+
const requestedRedirect = params.redirect || params.path || params.slug
|
|
374
|
+
const derivedPath = page.meta?.htmlUrl
|
|
375
|
+
? resolveInternalUrl(page.meta.htmlUrl, undefined, { isPageLink: true })
|
|
376
|
+
: page.meta?.slug
|
|
377
|
+
? `/${page.meta.slug}`
|
|
378
|
+
: "/"
|
|
379
|
+
const redirectPath = sanitizeRedirectPath(requestedRedirect || derivedPath)
|
|
380
|
+
return {
|
|
381
|
+
ok: true,
|
|
382
|
+
contentType,
|
|
383
|
+
token,
|
|
384
|
+
page,
|
|
385
|
+
redirectPath
|
|
386
|
+
}
|
|
387
|
+
} catch (err) {
|
|
388
|
+
const status = err instanceof ButterflyPressApiError ? err.status : 404
|
|
389
|
+
return {
|
|
390
|
+
ok: false,
|
|
391
|
+
status: status >= 400 && status < 600 ? status : 404,
|
|
392
|
+
error: err instanceof Error ? err.message : "Preview not found"
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
export function createEnableDraftModeHandler(options: PreviewHandlerOptions) {
|
|
398
|
+
const cookieName = options.cookieName || BUTTERFLYPRESS_PREVIEW_COOKIE
|
|
399
|
+
const maxAge = options.maxAgeSeconds ?? 3600
|
|
400
|
+
|
|
401
|
+
return async function GET(request: NextRequest | Request): Promise<NextResponse> {
|
|
402
|
+
const verification = await verifyPreviewRequest(request, options)
|
|
403
|
+
if (!verification.ok) {
|
|
404
|
+
return NextResponse.json({ error: verification.error }, { status: verification.status })
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
const dm = await Promise.resolve(draftMode())
|
|
408
|
+
dm.enable()
|
|
409
|
+
|
|
410
|
+
const cookiePayload: DraftPreviewCookieContext = {
|
|
411
|
+
contentType: verification.contentType,
|
|
412
|
+
token: verification.token,
|
|
413
|
+
pageId: verification.page.id,
|
|
414
|
+
slug: verification.page.meta?.slug
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
const targetUrl = new URL(verification.redirectPath, request.url)
|
|
418
|
+
const response = NextResponse.redirect(targetUrl)
|
|
419
|
+
response.cookies.set(cookieName, JSON.stringify(cookiePayload), {
|
|
420
|
+
httpOnly: true,
|
|
421
|
+
sameSite: "lax",
|
|
422
|
+
path: "/",
|
|
423
|
+
maxAge
|
|
424
|
+
})
|
|
425
|
+
|
|
426
|
+
return response
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
export function createDisableDraftModeHandler(options: Pick<PreviewHandlerOptions, "cookieName"> = {}) {
|
|
431
|
+
const cookieName = options.cookieName || BUTTERFLYPRESS_PREVIEW_COOKIE
|
|
432
|
+
|
|
433
|
+
return async function handleDisable(request: NextRequest | Request): Promise<NextResponse> {
|
|
434
|
+
const dm = await Promise.resolve(draftMode())
|
|
435
|
+
dm.disable()
|
|
436
|
+
|
|
437
|
+
const url = new URL(request.url)
|
|
438
|
+
const accept = request.headers.get("accept") || ""
|
|
439
|
+
const wantsJson = url.searchParams.get("format") === "json" || accept.includes("application/json")
|
|
440
|
+
const method = (request.method || "GET").toUpperCase()
|
|
441
|
+
const redirectStatus = method === "GET" || method === "HEAD" ? 307 : 303
|
|
442
|
+
|
|
443
|
+
const response = wantsJson
|
|
444
|
+
? NextResponse.json({ draftMode: false }, { status: 200 })
|
|
445
|
+
: NextResponse.redirect(new URL(sanitizeRedirectPath(url.searchParams.get("redirect")), request.url), {
|
|
446
|
+
status: redirectStatus
|
|
447
|
+
})
|
|
448
|
+
|
|
449
|
+
response.cookies.set(cookieName, "", {
|
|
450
|
+
httpOnly: true,
|
|
451
|
+
sameSite: "lax",
|
|
452
|
+
path: "/",
|
|
453
|
+
maxAge: 0
|
|
454
|
+
})
|
|
455
|
+
|
|
456
|
+
return response
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
export function createDraftModeHandlers(options: PreviewHandlerOptions) {
|
|
461
|
+
const enable = createEnableDraftModeHandler(options)
|
|
462
|
+
const disable = createDisableDraftModeHandler(options)
|
|
463
|
+
return {
|
|
464
|
+
enable,
|
|
465
|
+
disable,
|
|
466
|
+
GET: enable,
|
|
467
|
+
POST: disable,
|
|
468
|
+
DELETE: disable
|
|
469
|
+
}
|
|
470
|
+
}
|
package/src/ssr/index.ts
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createButterflyPressClient,
|
|
3
|
+
createPathLocalizer,
|
|
4
|
+
localizeMenuItems,
|
|
5
|
+
normalizeLocale,
|
|
6
|
+
type ButterflyPressClientOptions,
|
|
7
|
+
type MenuPayload,
|
|
8
|
+
type PagePayload,
|
|
9
|
+
type SiteConfigPayload
|
|
10
|
+
} from "@butterflypress/core"
|
|
11
|
+
|
|
12
|
+
export interface NextCatchAllServerSideContext {
|
|
13
|
+
params?: {
|
|
14
|
+
slug?: string | string[]
|
|
15
|
+
locale?: string
|
|
16
|
+
}
|
|
17
|
+
query?: Record<string, string | string[] | undefined>
|
|
18
|
+
locale?: string
|
|
19
|
+
draftMode?: boolean
|
|
20
|
+
preview?: boolean
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface NextCatchAllPageProps {
|
|
24
|
+
site: SiteConfigPayload | null
|
|
25
|
+
page: PagePayload
|
|
26
|
+
navbarMenu: MenuPayload | null
|
|
27
|
+
footerMenu: MenuPayload | null
|
|
28
|
+
locale: string
|
|
29
|
+
isDraftMode?: boolean
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface NextPreviewPageProps extends NextCatchAllPageProps {
|
|
33
|
+
isDraftMode: true
|
|
34
|
+
previewContext: {
|
|
35
|
+
contentType: string
|
|
36
|
+
token: string
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function hydrateLocalizedMenu(
|
|
41
|
+
menu: MenuPayload | null | undefined,
|
|
42
|
+
localizePath: (path: string) => string
|
|
43
|
+
): MenuPayload | null {
|
|
44
|
+
if (!menu) {
|
|
45
|
+
return null
|
|
46
|
+
}
|
|
47
|
+
return {
|
|
48
|
+
...menu,
|
|
49
|
+
items: localizeMenuItems(menu.items || [], localizePath)
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function pickQueryString(val: string | string[] | undefined): string {
|
|
54
|
+
if (Array.isArray(val)) return val[0] || ""
|
|
55
|
+
return typeof val === "string" ? val : ""
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function createButterflyPressCatchAllSSP(options: ButterflyPressClientOptions) {
|
|
59
|
+
const client = createButterflyPressClient(options)
|
|
60
|
+
|
|
61
|
+
return async function getServerSideProps(
|
|
62
|
+
context: NextCatchAllServerSideContext
|
|
63
|
+
): Promise<{ props: NextCatchAllPageProps } | { notFound: true }> {
|
|
64
|
+
const rawSlug = context.params?.slug
|
|
65
|
+
const slugParts = Array.isArray(rawSlug) ? rawSlug : rawSlug ? [rawSlug] : []
|
|
66
|
+
const targetSlug = slugParts[slugParts.length - 1] || "home"
|
|
67
|
+
|
|
68
|
+
const activeLocale = normalizeLocale(context.locale || context.params?.locale, client.defaultLocale)
|
|
69
|
+
const localizePath = createPathLocalizer(activeLocale, client.defaultLocale)
|
|
70
|
+
|
|
71
|
+
const isDraft = Boolean(context.draftMode || context.preview)
|
|
72
|
+
const queryContentType = pickQueryString(context.query?.content_type ?? context.query?.contentType)
|
|
73
|
+
const queryToken = pickQueryString(context.query?.token)
|
|
74
|
+
|
|
75
|
+
const pagePromise =
|
|
76
|
+
isDraft && queryContentType && queryToken
|
|
77
|
+
? client.getPagePreview(queryContentType, queryToken)
|
|
78
|
+
: client.getPageBySlug(targetSlug, activeLocale)
|
|
79
|
+
|
|
80
|
+
const [site, page, directNavbarMenu, directFooterMenu] = await Promise.all([
|
|
81
|
+
client.getSiteConfig(undefined, activeLocale),
|
|
82
|
+
pagePromise,
|
|
83
|
+
client.getMenu("navbar", activeLocale),
|
|
84
|
+
client.getMenu("footer", activeLocale)
|
|
85
|
+
])
|
|
86
|
+
|
|
87
|
+
if (!page) {
|
|
88
|
+
return { notFound: true }
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const rawNavbar = site?.navigationsettings?.navbarMenu || site?.navigationsettings?.navbar_menu || directNavbarMenu
|
|
92
|
+
const rawFooter =
|
|
93
|
+
site?.navigationsettings?.footerMenu ||
|
|
94
|
+
site?.navigationsettings?.footer_menu ||
|
|
95
|
+
site?.navigationsettings?.footerPrimaryMenu ||
|
|
96
|
+
site?.navigationsettings?.footer_primary_menu ||
|
|
97
|
+
directFooterMenu
|
|
98
|
+
|
|
99
|
+
return {
|
|
100
|
+
props: {
|
|
101
|
+
site,
|
|
102
|
+
page,
|
|
103
|
+
navbarMenu: hydrateLocalizedMenu(rawNavbar, localizePath),
|
|
104
|
+
footerMenu: hydrateLocalizedMenu(rawFooter, localizePath),
|
|
105
|
+
locale: activeLocale,
|
|
106
|
+
...(isDraft ? { isDraftMode: true } : {})
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export function createButterflyPressPreviewSSP(options: ButterflyPressClientOptions) {
|
|
113
|
+
const client = createButterflyPressClient(options)
|
|
114
|
+
|
|
115
|
+
return async function getServerSideProps(
|
|
116
|
+
context: NextCatchAllServerSideContext
|
|
117
|
+
): Promise<{ props: NextPreviewPageProps } | { notFound: true }> {
|
|
118
|
+
const contentType = pickQueryString(context.query?.content_type ?? context.query?.contentType).trim()
|
|
119
|
+
const token = pickQueryString(context.query?.token).trim()
|
|
120
|
+
|
|
121
|
+
if (!contentType || !token) {
|
|
122
|
+
return { notFound: true }
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const activeLocale = normalizeLocale(context.locale || context.params?.locale, client.defaultLocale)
|
|
126
|
+
const localizePath = createPathLocalizer(activeLocale, client.defaultLocale)
|
|
127
|
+
|
|
128
|
+
const [site, page, directNavbarMenu, directFooterMenu] = await Promise.all([
|
|
129
|
+
client.getSiteConfig(undefined, activeLocale),
|
|
130
|
+
client.getPagePreview(contentType, token).catch((err) => {
|
|
131
|
+
if (err && typeof err === "object" && "status" in err && (err as { status?: number }).status === 404) {
|
|
132
|
+
return null
|
|
133
|
+
}
|
|
134
|
+
throw err
|
|
135
|
+
}),
|
|
136
|
+
client.getMenu("navbar", activeLocale),
|
|
137
|
+
client.getMenu("footer", activeLocale)
|
|
138
|
+
])
|
|
139
|
+
|
|
140
|
+
if (!page) {
|
|
141
|
+
return { notFound: true }
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const rawNavbar = site?.navigationsettings?.navbarMenu || site?.navigationsettings?.navbar_menu || directNavbarMenu
|
|
145
|
+
const rawFooter =
|
|
146
|
+
site?.navigationsettings?.footerMenu ||
|
|
147
|
+
site?.navigationsettings?.footer_menu ||
|
|
148
|
+
site?.navigationsettings?.footerPrimaryMenu ||
|
|
149
|
+
site?.navigationsettings?.footer_primary_menu ||
|
|
150
|
+
directFooterMenu
|
|
151
|
+
|
|
152
|
+
return {
|
|
153
|
+
props: {
|
|
154
|
+
site,
|
|
155
|
+
page,
|
|
156
|
+
navbarMenu: hydrateLocalizedMenu(rawNavbar, localizePath),
|
|
157
|
+
footerMenu: hydrateLocalizedMenu(rawFooter, localizePath),
|
|
158
|
+
locale: activeLocale,
|
|
159
|
+
isDraftMode: true,
|
|
160
|
+
previewContext: {
|
|
161
|
+
contentType,
|
|
162
|
+
token
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
package/styles.css
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
@import "tailwindcss";
|
|
2
|
+
@source "./dist/**/*.{js,mjs,cjs}";
|
|
3
|
+
@source "./src/**/*.{ts,tsx}";
|
|
4
|
+
|
|
5
|
+
@theme inline {
|
|
6
|
+
--color-background: var(--background);
|
|
7
|
+
--color-foreground: var(--foreground);
|
|
8
|
+
--color-card: var(--card);
|
|
9
|
+
--color-card-foreground: var(--card-foreground);
|
|
10
|
+
--color-popover: var(--popover);
|
|
11
|
+
--color-popover-foreground: var(--popover-foreground);
|
|
12
|
+
--color-primary: var(--primary);
|
|
13
|
+
--color-primary-foreground: var(--primary-foreground);
|
|
14
|
+
--color-secondary: var(--secondary);
|
|
15
|
+
--color-secondary-foreground: var(--secondary-foreground);
|
|
16
|
+
--color-muted: var(--muted);
|
|
17
|
+
--color-muted-foreground: var(--muted-foreground);
|
|
18
|
+
--color-accent: var(--accent);
|
|
19
|
+
--color-accent-foreground: var(--accent-foreground);
|
|
20
|
+
--color-destructive: var(--destructive);
|
|
21
|
+
--color-destructive-foreground: var(--destructive-foreground);
|
|
22
|
+
--color-border: var(--border);
|
|
23
|
+
--color-input: var(--input);
|
|
24
|
+
--color-ring: var(--ring);
|
|
25
|
+
|
|
26
|
+
--radius-sm: calc(var(--radius) - 4px);
|
|
27
|
+
--radius-md: calc(var(--radius) - 2px);
|
|
28
|
+
--radius-lg: var(--radius);
|
|
29
|
+
--radius-xl: calc(var(--radius) + 4px);
|
|
30
|
+
|
|
31
|
+
--font-heading: var(--font-heading, "Inter", system-ui, sans-serif);
|
|
32
|
+
--font-body: var(--font-body, "Inter", system-ui, sans-serif);
|
|
33
|
+
--font-sans: var(--font-sans, "Inter", system-ui, sans-serif);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
[data-bp-section][data-bp-surface="default"] {
|
|
37
|
+
background-color: var(--background);
|
|
38
|
+
color: var(--foreground);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
[data-bp-section][data-bp-surface="muted"] {
|
|
42
|
+
background-color: var(--muted);
|
|
43
|
+
color: var(--muted-foreground);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
[data-bp-section][data-bp-surface="brand"] {
|
|
47
|
+
background-color: var(--primary);
|
|
48
|
+
color: var(--primary-foreground);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
[data-bp-section][data-bp-surface="contrast"] {
|
|
52
|
+
background-color: var(--foreground);
|
|
53
|
+
color: var(--background);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
[data-bp-card] {
|
|
57
|
+
background-color: var(--card);
|
|
58
|
+
color: var(--card-foreground);
|
|
59
|
+
border-color: var(--border);
|
|
60
|
+
border-radius: var(--radius);
|
|
61
|
+
}
|
package/README.md
DELETED