Skip to content

Commit 3560297

Browse files
committed
Starter ready for internationalization;
More robust page fetch
1 parent 3e9abed commit 3560297

File tree

4 files changed

+59
-18
lines changed

4 files changed

+59
-18
lines changed

components/errorNoPage.tsx

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const ErrorNoPage = () => {
2+
return (
3+
<div className="max-w-5xl mx-auto px-6 py-20 text-gray-600">
4+
<h1 className="text-2xl text-red-600 font-semibold mb-8">
5+
Page not found
6+
</h1>
7+
<p className="mb-6">
8+
React Bricks cannot find a page for the specified slug.
9+
</p>
10+
</div>
11+
)
12+
}
13+
14+
export default ErrorNoPage

next.config.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
i18n: {
3+
locales: ['en'], // Add your languages here
4+
defaultLocale: 'en',
5+
localeDetection: false,
6+
},
7+
}

pages/[slug].tsx

+33-13
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,31 @@ import { GetStaticProps, GetStaticPaths } from 'next'
1212

1313
import config from '../react-bricks/config'
1414
import Layout from '../components/layout'
15+
import ErrorNoPage from '../components/errorNoPage'
1516

1617
interface PageProps {
1718
page: types.Page
19+
error: string
1820
}
1921

20-
const Page: React.FC<PageProps> = ({ page }) => {
22+
const Page: React.FC<PageProps> = ({ page, error }) => {
2123
// Clean the received content
2224
// Removes unknown or not allowed bricks
2325
const { pageTypes, bricks } = useContext(ReactBricksContext)
24-
const pageOk = cleanPage(page, pageTypes, bricks)
26+
const pageOk = page ? cleanPage(page, pageTypes, bricks) : null
2527

2628
return (
2729
<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 />}
3340
</Layout>
3441
)
3542
}
@@ -39,20 +46,33 @@ export const getStaticProps: GetStaticProps = async (context) => {
3946
return { props: { error: 'NOKEYS' } }
4047
}
4148
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+
}
4455
}
4556

46-
export const getStaticPaths: GetStaticPaths = async () => {
57+
export const getStaticPaths: GetStaticPaths = async (context) => {
4758
if (!config.apiKey) {
4859
return { paths: [], fallback: false }
4960
}
5061

5162
const allPages = await fetchPages(config.apiKey)
5263

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()
5676

5777
return { paths, fallback: false }
5878
}

pages/index.tsx

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@ import {
44
PageViewer,
55
fetchPage,
66
cleanPage,
7-
types
7+
types,
88
} from 'react-bricks'
99
import Head from 'next/head'
10-
import { GetStaticProps } from 'next'
10+
import { GetStaticProps, GetStaticPaths } from 'next'
1111

1212
import config from '../react-bricks/config'
1313
import Layout from '../components/layout'
1414
import ErrorNoKeys from '../components/errorNoKeys'
1515
import ErrorNoHomePage from '../components/errorNoHomePage'
1616

1717
interface HomeProps {
18-
page: types.Page,
18+
page: types.Page
1919
error: string
2020
}
2121

@@ -43,12 +43,12 @@ const Home: React.FC<HomeProps> = ({ page, error }) => {
4343
)
4444
}
4545

46-
export const getStaticProps: GetStaticProps = async () => {
46+
export const getStaticProps: GetStaticProps = async (context) => {
4747
if (!config.apiKey) {
4848
return { props: { error: 'NOKEYS' } }
4949
}
5050
try {
51-
const page = await fetchPage('home', config.apiKey)
51+
const page = await fetchPage('home', config.apiKey, context.locale)
5252
return { props: { page } }
5353
} catch {
5454
return { props: { error: 'NOPAGE' } }

0 commit comments

Comments
 (0)