forked from sitecorelabs/xmcloud-foundation-head-staging
-
Notifications
You must be signed in to change notification settings - Fork 84
/
normal-mode.ts
84 lines (66 loc) · 3.17 KB
/
normal-mode.ts
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
import { GetServerSidePropsContext, GetStaticPropsContext } from 'next';
import { DictionaryService, LayoutService } from '@sitecore-jss/sitecore-jss-nextjs';
import { dictionaryServiceFactory } from 'lib/dictionary-service-factory';
import { layoutServiceFactory } from 'lib/layout-service-factory';
import { SitecorePageProps } from 'lib/page-props';
import { pathExtractor } from 'lib/extract-path';
import { Plugin, isServerSidePropsContext } from '..';
class NormalModePlugin implements Plugin {
private dictionaryServices: Map<string, DictionaryService>;
private layoutServices: Map<string, LayoutService>;
order = 1;
constructor() {
this.dictionaryServices = new Map<string, DictionaryService>();
this.layoutServices = new Map<string, LayoutService>();
}
async exec(props: SitecorePageProps, context: GetServerSidePropsContext | GetStaticPropsContext) {
if (context.preview) return props;
// Get normalized Sitecore item path
const path = pathExtractor.extract(context.params);
// Use context locale if Next.js i18n is configured, otherwise use default site language
props.locale = context.locale ?? props.site.language;
// Fetch layout data, passing on req/res for SSR
const layoutService = this.getLayoutService(props.site.name);
props.layoutData = await layoutService.fetchLayoutData(
path,
props.locale,
// eslint-disable-next-line prettier/prettier
isServerSidePropsContext(context) ? (context as GetServerSidePropsContext).req : undefined,
isServerSidePropsContext(context) ? (context as GetServerSidePropsContext).res : undefined
);
if (!props.layoutData.sitecore.route) {
// A missing route value signifies an invalid path, so set notFound.
// Our page routes will return this in getStatic/ServerSideProps,
// which will trigger our custom 404 page with proper 404 status code.
// You could perform additional logging here to track these if desired.
props.notFound = true;
}
// Fetch dictionary data if layout data was present
if (!props.notFound) {
const dictionaryService = this.getDictionaryService(props.site.name);
props.dictionary = await dictionaryService.fetchDictionaryData(props.locale);
}
// Initialize links to be inserted on the page
props.headLinks = [];
return props;
}
private getDictionaryService(siteName: string): DictionaryService {
if (this.dictionaryServices.has(siteName)) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return this.dictionaryServices.get(siteName)!;
}
const dictionaryService = dictionaryServiceFactory.create(siteName);
this.dictionaryServices.set(siteName, dictionaryService);
return dictionaryService;
}
private getLayoutService(siteName: string): LayoutService {
if (this.layoutServices.has(siteName)) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return this.layoutServices.get(siteName)!;
}
const layoutService = layoutServiceFactory.create(siteName);
this.layoutServices.set(siteName, layoutService);
return layoutService;
}
}
export const normalModePlugin = new NormalModePlugin();