-
Notifications
You must be signed in to change notification settings - Fork 156
Expand file tree
/
Copy path[[...identifier]].js
More file actions
85 lines (72 loc) · 1.82 KB
/
[[...identifier]].js
File metadata and controls
85 lines (72 loc) · 1.82 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { getAuthString } from '@/utils/getAuthString';
import availableTemplates from '@/wp-templates';
import availableQueries from '@/wp-templates/templateQueries';
import {
createDefaultClient,
setGraphQLClient,
uriToTemplate,
} from '@faustjs/nextjs/pages';
import { fetchTemplateQueries } from '@faustjs/data-fetching';
export default function Page(props) {
const { templateData } = props;
const PageTemplate = availableTemplates[templateData?.template?.id];
return <PageTemplate {...props} />;
}
export async function getStaticProps({
params,
draftMode: isDraftModeEnabled,
}) {
// Send the authentication string only if draft mode is enabled
const headers = isDraftModeEnabled
? {
Authorization: getAuthString(),
}
: null;
const client = createDefaultClient(
process.env.NEXT_PUBLIC_WORDPRESS_URL,
headers,
);
setGraphQLClient(client);
const uri = params?.identifier ? `/${params.identifier.join('/')}/` : '/';
const variables = isDraftModeEnabled
? {
id: params.identifier?.[0],
asPreview: true,
}
: { uri };
try {
const templateData = await uriToTemplate({
...variables,
availableTemplates: Object.keys(availableTemplates),
wordpressUrl: process.env.NEXT_PUBLIC_WORDPRESS_URL,
});
if (
!templateData?.template?.id ||
templateData?.template?.id === '404 Not Found'
) {
return { notFound: true };
}
const queriesData = await fetchTemplateQueries({
availableQueries,
templateData,
client,
locale: templateData?.seedNode?.locale,
});
return {
props: {
uri,
templateData: JSON.parse(JSON.stringify(templateData)),
queriesData,
},
};
} catch (error) {
console.error('Error resolving template:', error);
return { notFound: true };
}
}
export async function getStaticPaths() {
return {
paths: [],
fallback: 'blocking',
};
}