-
Notifications
You must be signed in to change notification settings - Fork 1
/
page.tsx
66 lines (57 loc) · 1.9 KB
/
page.tsx
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
import { loadDocumentPropsCached, NodeRenderer } from '@networkteam/zebra/server';
import { DataLoaderOptions } from '@networkteam/zebra/types';
import { Metadata } from 'next';
import { notFound, permanentRedirect, redirect } from 'next/navigation';
const dataLoaderOptionsFor = (routePath: string): DataLoaderOptions => ({
cache: 'force-cache',
next: {
tags: ['document', `document:${routePath}`],
},
});
export async function generateMetadata({
params,
}: {
params: {
slug: string[];
};
}): Promise<Metadata> {
const routePath = params.slug && Array.isArray(params.slug) ? params.slug.join('/') : '/';
const neosData = await loadDocumentPropsCached(routePath, dataLoaderOptionsFor(routePath));
if (!neosData || !('node' in neosData)) {
return {};
}
const { node, site, meta } = neosData;
const title = meta?.isRootPage ? site.properties.title : `${node.properties.title} – ${site.properties.title}`;
return {
title,
};
}
const Page = async ({ params: { slug } }: { params: { slug: string[] } }) => {
const routePath = slug && Array.isArray(slug) ? slug.join('/') : '/';
const dataLoaderOptions = dataLoaderOptionsFor(routePath);
const neosData = await loadDocumentPropsCached(routePath, dataLoaderOptions);
if (!neosData) {
return notFound();
}
if ('redirect' in neosData) {
if (neosData.redirect.statusCode === 308 || neosData.redirect.statusCode === 301) {
permanentRedirect(neosData.redirect.targetPath);
}
redirect(neosData.redirect.targetPath);
}
if (neosData?.node.nodeType === 'Neos.Neos:Shortcut') {
return redirect(neosData.node.properties.targetUri || '/');
}
return (
<NodeRenderer
ctx={{
routePath,
currentNodeIdentifier: neosData.node.identifier,
documentNodeIdentifier: neosData.node.identifier,
dataLoaderOptions,
}}
node={neosData.node}
/>
);
};
export default Page;