1
1
/* eslint-disable react-hooks/rules-of-hooks -- false positive, useMDXComponents are not react hooks */
2
2
3
- import { BASE_URL } from '@/app/_constants/project'
4
3
import { JsonLd , generateArticleSchema } from '@app/_components/json-ld'
5
4
import { generateBreadcrumbSchema } from '@app/_utils/generate-breadcrumb-schema'
6
5
import { Metadata } from 'next'
@@ -9,38 +8,49 @@ import { useMDXComponents } from '../../../mdx-components'
9
8
10
9
export const generateStaticParams = generateStaticParamsFor ( 'mdxPath' )
11
10
12
- export async function generateMetadata ( props : PageProps ) : Promise < Metadata | null > {
13
- try {
14
- const params = await props . params
15
- const { metadata } = await importPage ( params . mdxPath || [ ] , params . lang )
16
- const isHomepage = ! params . mdxPath || params . mdxPath . length === 0
11
+ export async function generateMetadata ( { params } ) : Promise < Metadata > {
12
+ const { frontMatter , metadata } = await importPage ( params . mdxPath || [ ] , params . lang )
13
+ const isHomepage = ! params . mdxPath || params . mdxPath . length === 0
14
+ const baseUrl = 'https://uxpatterns.dev'
15
+ const path = params . mdxPath ?. join ( '/' ) || ''
17
16
18
- // Include the language prefix in the canonical path
19
- const canonicalPath = `/${ params . lang } ${ params . mdxPath ? `/${ params . mdxPath . join ( '/' ) } ` : '' } `
17
+ // Shared metadata
18
+ const title = frontMatter ?. title || metadata ?. title || 'UX Patterns for Devs'
19
+ const description = frontMatter ?. description || metadata ?. description || ''
20
+ const summary = frontMatter ?. summary || ''
20
21
21
- const ogImage = {
22
- url : isHomepage
23
- ? '/og/opengraph-image.png'
24
- : `/api/og?title=${ encodeURIComponent ( metadata . title || '' ) } ` ,
25
- width : 1200 ,
26
- height : 630 ,
27
- type : 'image/png' ,
28
- }
22
+ // OG image handling
23
+ const isPatternPage = params . mdxPath ?. [ 0 ] === 'patterns'
24
+ const patternName = isPatternPage ? params . mdxPath ?. [ params . mdxPath . length - 1 ] : null
25
+ const ogImageUrl = isHomepage
26
+ ? '/og/opengraph-image.png'
27
+ : `/api/og?title=${ encodeURIComponent ( title ) } `
29
28
30
- return {
31
- ...metadata ,
32
- openGraph : {
33
- ...metadata . openGraph ,
34
- images : [ ogImage ] ,
35
- url : `${ BASE_URL } ${ canonicalPath } `
36
- } ,
37
- alternates : {
38
- canonical : `${ BASE_URL } ${ canonicalPath } `
39
- }
40
- }
41
- } catch ( e ) {
42
- console . error ( 'Error generating metadata:' , e )
43
- return null
29
+ return {
30
+ title,
31
+ description,
32
+ openGraph : {
33
+ title,
34
+ description,
35
+ type : 'article' ,
36
+ images : [
37
+ ...( isPatternPage && patternName ? [ {
38
+ url : `/covers/patterns/${ patternName } .png` ,
39
+ width : 800 ,
40
+ height : 400 ,
41
+ alt : `${ summary } - UX Pattern` ,
42
+ } ] : [ ] ) ,
43
+ {
44
+ url : ogImageUrl ,
45
+ width : 1200 ,
46
+ height : 630 ,
47
+ alt : description || title ,
48
+ } ,
49
+ ] ,
50
+ } ,
51
+ alternates : {
52
+ canonical : `${ baseUrl } /${ params . lang } ${ path ? `/${ path } ` : '' } ` ,
53
+ } ,
44
54
}
45
55
}
46
56
@@ -55,26 +65,25 @@ const Wrapper = useMDXComponents().wrapper
55
65
56
66
export default async function Page ( props : PageProps ) {
57
67
const params = await props . params
58
- const result = await importPage ( params . mdxPath , params . lang )
59
- const { default : MDXContent , toc, metadata } = result
68
+ const { default : MDXContent , toc, metadata } = await importPage ( params . mdxPath , params . lang )
60
69
61
- // Get the OG image URL from metadata
70
+ // Use the same logic for OG image as in generateMetadata
62
71
const isHomepage = ! params . mdxPath || params . mdxPath . length === 0
72
+ const title = metadata ?. title || 'UX Patterns for Devs'
73
+ const description = metadata ?. description || ''
63
74
const ogImageUrl = isHomepage
64
75
? '/og/opengraph-image.png'
65
- : `/api/og?title=${ encodeURIComponent ( metadata . title || '' ) } `
76
+ : `/api/og?title=${ encodeURIComponent ( title ) } `
66
77
67
78
const schemaData = generateArticleSchema (
68
- metadata . title || '' ,
69
- metadata . description || '' ,
79
+ title ,
80
+ description ,
70
81
`/${ params . lang } /${ params . mdxPath ?. join ( '/' ) || '' } ` ,
71
82
ogImageUrl
72
83
)
73
84
74
- // Generate breadcrumb items
75
- const breadcrumbs = [
76
- { title : 'Home' , url : `/${ params . lang } ` }
77
- ]
85
+ // Generate breadcrumb items for patterns only
86
+ const breadcrumbs : Array < { title : string ; url : string } > = [ ]
78
87
79
88
if ( params . mdxPath ) {
80
89
let currentPath = ''
@@ -93,7 +102,7 @@ export default async function Page(props: PageProps) {
93
102
return (
94
103
< >
95
104
< JsonLd data = { schemaData } />
96
- < JsonLd data = { breadcrumbSchema } />
105
+ { breadcrumbs . length > 0 && < JsonLd data = { breadcrumbSchema } /> }
97
106
< div className = "nextra-content" >
98
107
< Wrapper key = { pageKey } toc = { toc } metadata = { metadata } >
99
108
< MDXContent { ...props } params = { params } />
0 commit comments