-
Notifications
You must be signed in to change notification settings - Fork 160
/
[name].js
71 lines (58 loc) · 1.41 KB
/
[name].js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
import { i18n } from '../../next-i18next.config.js';
import { findPost, getPublishedPosts } from '../../api/index.js';
import AmpLayout from '../../components/AmpLayout.jsx';
import AmpPostPageInfo from '../../components/AmpPostPageInfo.jsx';
const AmpPost = ({ post }) => {
if (!post) {
return null;
}
return (
<AmpLayout title={post.header}>
<AmpPostPageInfo post={post} />
</AmpLayout>
);
};
export const config = {
amp: true,
};
export const getStaticProps = async ({ locale, params }) => {
const post = await findPost(params.name, locale);
if (!post) {
return {
notFound: true,
};
}
if (post.redirect_to) {
return {
redirect: {
permanent: true,
destination: post.redirect_to,
},
};
}
return {
props: {
post,
...(await serverSideTranslations(locale, ['common', 'post'])),
},
};
};
export const getStaticPaths = async () => {
const promises = i18n.locales.map(async (locale) => {
const posts = await getPublishedPosts(locale);
return posts
.filter(({ redirect_to }) => !redirect_to)
.map(({ name }) => ({
locale,
params: { name },
}));
});
const allPaths = await Promise.all(promises);
const paths = allPaths.flat();
return {
paths,
fallback: true,
};
};
export default AmpPost;