@@ -12,24 +12,31 @@ import { GetStaticProps, GetStaticPaths } from 'next'
12
12
13
13
import config from '../react-bricks/config'
14
14
import Layout from '../components/layout'
15
+ import ErrorNoPage from '../components/errorNoPage'
15
16
16
17
interface PageProps {
17
18
page : types . Page
19
+ error : string
18
20
}
19
21
20
- const Page : React . FC < PageProps > = ( { page } ) => {
22
+ const Page : React . FC < PageProps > = ( { page, error } ) => {
21
23
// Clean the received content
22
24
// Removes unknown or not allowed bricks
23
25
const { pageTypes, bricks } = useContext ( ReactBricksContext )
24
- const pageOk = cleanPage ( page , pageTypes , bricks )
26
+ const pageOk = page ? cleanPage ( page , pageTypes , bricks ) : null
25
27
26
28
return (
27
29
< Layout >
28
- < Head >
29
- < title > { page . meta . title } </ title >
30
- < meta name = "description" content = { page . meta . description } />
31
- </ Head >
32
- < PageViewer page = { pageOk } />
30
+ { pageOk && (
31
+ < >
32
+ < Head >
33
+ < title > { page . meta . title } </ title >
34
+ < meta name = "description" content = { page . meta . description } />
35
+ </ Head >
36
+ < PageViewer page = { pageOk } />
37
+ </ >
38
+ ) }
39
+ { error === 'NOPAGE' && < ErrorNoPage /> }
33
40
</ Layout >
34
41
)
35
42
}
@@ -39,20 +46,33 @@ export const getStaticProps: GetStaticProps = async (context) => {
39
46
return { props : { error : 'NOKEYS' } }
40
47
}
41
48
const { slug } = context . params
42
- const page = await fetchPage ( slug . toString ( ) , config . apiKey )
43
- return { props : { page } }
49
+ try {
50
+ const page = await fetchPage ( slug . toString ( ) , config . apiKey , context . locale )
51
+ return { props : { page } }
52
+ } catch {
53
+ return { props : { error : 'NOPAGE' } }
54
+ }
44
55
}
45
56
46
- export const getStaticPaths : GetStaticPaths = async ( ) => {
57
+ export const getStaticPaths : GetStaticPaths = async ( context ) => {
47
58
if ( ! config . apiKey ) {
48
59
return { paths : [ ] , fallback : false }
49
60
}
50
61
51
62
const allPages = await fetchPages ( config . apiKey )
52
63
53
- const paths = allPages . map ( ( page ) => ( {
54
- params : { slug : page . slug } ,
55
- } ) )
64
+ const paths = allPages
65
+ . map ( ( page ) =>
66
+ page . translations
67
+ . filter (
68
+ ( translation ) => context . locales . indexOf ( translation . language ) > - 1
69
+ )
70
+ . map ( ( translation ) => ( {
71
+ params : { slug : translation . slug } ,
72
+ locale : translation . language ,
73
+ } ) )
74
+ )
75
+ . flat ( )
56
76
57
77
return { paths, fallback : false }
58
78
}
0 commit comments