create-sitecore-jss 22.10.0-canary.1 → 22.10.0-canary.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/templates/nextjs/src/lib/component-props/index.ts +40 -0
- package/dist/templates/nextjs/src/lib/page-props-factory/plugins/component-props.ts +4 -37
- package/dist/templates/nextjs-sxa/src/pages/404.tsx +30 -17
- package/dist/templates/nextjs-sxa/src/pages/500.tsx +30 -17
- package/package.json +2 -2
|
@@ -2,7 +2,14 @@ import {
|
|
|
2
2
|
ComponentParams,
|
|
3
3
|
ComponentRendering,
|
|
4
4
|
SitecoreContextValue,
|
|
5
|
+
LayoutServiceData,
|
|
6
|
+
ComponentPropsService,
|
|
7
|
+
ComponentPropsCollection,
|
|
8
|
+
ComponentPropsError,
|
|
5
9
|
} from '@sitecore-jss/sitecore-jss-nextjs';
|
|
10
|
+
import { GetServerSidePropsContext, GetStaticPropsContext } from 'next';
|
|
11
|
+
import { isServerSidePropsContext } from 'lib/page-props-factory';
|
|
12
|
+
import { moduleFactory } from 'temp/componentBuilder';
|
|
6
13
|
|
|
7
14
|
/**
|
|
8
15
|
* Shared component props
|
|
@@ -21,3 +28,36 @@ export type ComponentProps = {
|
|
|
21
28
|
export type ComponentWithContextProps = ComponentProps & {
|
|
22
29
|
sitecoreContext: SitecoreContextValue;
|
|
23
30
|
};
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Fetch component props for a given layout data and context
|
|
34
|
+
* @param {LayoutServiceData} layoutData - The layout data to fetch component props for
|
|
35
|
+
* @param {GetServerSidePropsContext | GetStaticPropsContext} context - The context to fetch component props for
|
|
36
|
+
* @returns {ComponentPropsCollection} The component props
|
|
37
|
+
* @throws {Error} If there are errors during component props fetching
|
|
38
|
+
*/
|
|
39
|
+
export async function fetchComponentProps(
|
|
40
|
+
layoutData: LayoutServiceData,
|
|
41
|
+
context: GetServerSidePropsContext | GetStaticPropsContext
|
|
42
|
+
): Promise<ComponentPropsCollection> {
|
|
43
|
+
const service = new ComponentPropsService();
|
|
44
|
+
|
|
45
|
+
const componentProps = isServerSidePropsContext(context)
|
|
46
|
+
? await service.fetchServerSideComponentProps({ layoutData, context, moduleFactory })
|
|
47
|
+
: await service.fetchStaticComponentProps({ layoutData, context, moduleFactory });
|
|
48
|
+
|
|
49
|
+
const errors = Object.keys(componentProps)
|
|
50
|
+
.map(id => {
|
|
51
|
+
const c = componentProps[id] as ComponentPropsError;
|
|
52
|
+
return c?.error
|
|
53
|
+
? `\nUnable to get component props for ${c.componentName} (${id}): ${c.error}`
|
|
54
|
+
: '';
|
|
55
|
+
})
|
|
56
|
+
.join('');
|
|
57
|
+
|
|
58
|
+
if (errors.length) {
|
|
59
|
+
throw new Error(errors);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return componentProps;
|
|
63
|
+
}
|
|
@@ -1,50 +1,17 @@
|
|
|
1
|
-
import { ComponentPropsService, ComponentPropsError } from '@sitecore-jss/sitecore-jss-nextjs';
|
|
2
|
-
import { SitecorePageProps } from 'lib/page-props';
|
|
3
1
|
import { GetServerSidePropsContext, GetStaticPropsContext } from 'next';
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
2
|
+
import { SitecorePageProps } from 'lib/page-props';
|
|
3
|
+
import { fetchComponentProps } from 'lib/component-props';
|
|
4
|
+
import { Plugin } from '..';
|
|
6
5
|
|
|
7
6
|
class ComponentPropsPlugin implements Plugin {
|
|
8
|
-
private componentPropsService: ComponentPropsService;
|
|
9
|
-
|
|
10
7
|
// Make sure to run this plugin last to ensure that the updated layout data is used
|
|
11
8
|
order = 10;
|
|
12
9
|
|
|
13
|
-
constructor() {
|
|
14
|
-
this.componentPropsService = new ComponentPropsService();
|
|
15
|
-
}
|
|
16
|
-
|
|
17
10
|
async exec(props: SitecorePageProps, context: GetServerSidePropsContext | GetStaticPropsContext) {
|
|
18
11
|
if (!props.layoutData.sitecore.route) return props;
|
|
19
12
|
|
|
20
13
|
// Retrieve component props using side-effects defined on components level
|
|
21
|
-
|
|
22
|
-
props.componentProps = await this.componentPropsService.fetchServerSideComponentProps({
|
|
23
|
-
layoutData: props.layoutData,
|
|
24
|
-
context,
|
|
25
|
-
moduleFactory,
|
|
26
|
-
});
|
|
27
|
-
} else {
|
|
28
|
-
props.componentProps = await this.componentPropsService.fetchStaticComponentProps({
|
|
29
|
-
layoutData: props.layoutData,
|
|
30
|
-
context,
|
|
31
|
-
moduleFactory,
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const errors = Object.keys(props.componentProps)
|
|
36
|
-
.map(id => {
|
|
37
|
-
const component = props.componentProps[id] as ComponentPropsError;
|
|
38
|
-
|
|
39
|
-
return component.error
|
|
40
|
-
? `\nUnable to get component props for ${component.componentName} (${id}): ${component.error}`
|
|
41
|
-
: '';
|
|
42
|
-
})
|
|
43
|
-
.join('');
|
|
44
|
-
|
|
45
|
-
if (errors.length) {
|
|
46
|
-
throw new Error(errors);
|
|
47
|
-
}
|
|
14
|
+
props.componentProps = await fetchComponentProps(props.layoutData, context);
|
|
48
15
|
|
|
49
16
|
return props;
|
|
50
17
|
}
|
|
@@ -1,17 +1,19 @@
|
|
|
1
1
|
import { JSX } from 'react';
|
|
2
|
-
import
|
|
2
|
+
import { GetStaticProps } from 'next';
|
|
3
3
|
import {
|
|
4
4
|
GraphQLErrorPagesService,
|
|
5
5
|
SitecoreContext,
|
|
6
|
+
ComponentPropsContext,
|
|
6
7
|
ErrorPages,
|
|
7
8
|
} from '@sitecore-jss/sitecore-jss-nextjs';
|
|
8
9
|
import { SitecorePageProps } from 'lib/page-props';
|
|
9
10
|
import NotFound from 'src/NotFound';
|
|
10
11
|
import { componentBuilder } from 'temp/componentBuilder';
|
|
11
12
|
import Layout from 'src/Layout';
|
|
12
|
-
import { GetStaticProps } from 'next';
|
|
13
13
|
import { siteResolver } from 'lib/site-resolver';
|
|
14
|
+
import { fetchComponentProps } from 'lib/component-props';
|
|
14
15
|
import clientFactory from 'lib/graphql-client-factory';
|
|
16
|
+
import config from 'temp/config';
|
|
15
17
|
|
|
16
18
|
const Custom404 = (props: SitecorePageProps): JSX.Element => {
|
|
17
19
|
if (!(props && props.layoutData)) {
|
|
@@ -19,20 +21,22 @@ const Custom404 = (props: SitecorePageProps): JSX.Element => {
|
|
|
19
21
|
}
|
|
20
22
|
|
|
21
23
|
return (
|
|
22
|
-
<
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
24
|
+
<ComponentPropsContext value={props.componentProps}>
|
|
25
|
+
<SitecoreContext
|
|
26
|
+
componentFactory={componentBuilder.getComponentFactory()}
|
|
27
|
+
layoutData={props.layoutData}
|
|
28
|
+
<% if (templates.includes('nextjs-xmcloud')) { %>
|
|
29
|
+
api={{
|
|
30
|
+
edge: {
|
|
31
|
+
contextId: config.sitecoreEdgeContextId,
|
|
32
|
+
edgeUrl: config.sitecoreEdgeUrl,
|
|
33
|
+
},
|
|
34
|
+
}}
|
|
35
|
+
<% } %>
|
|
36
|
+
>
|
|
37
|
+
<Layout layoutData={props.layoutData} headLinks={props.headLinks} />
|
|
38
|
+
</SitecoreContext>
|
|
39
|
+
</ComponentPropsContext>
|
|
36
40
|
);
|
|
37
41
|
};
|
|
38
42
|
|
|
@@ -58,10 +62,19 @@ export const getStaticProps: GetStaticProps = async (context) => {
|
|
|
58
62
|
}
|
|
59
63
|
}
|
|
60
64
|
|
|
65
|
+
const layoutData = resultErrorPages?.notFoundPage?.rendered || null;
|
|
66
|
+
|
|
67
|
+
let componentProps = {};
|
|
68
|
+
|
|
69
|
+
if (layoutData?.sitecore?.route) {
|
|
70
|
+
componentProps = await fetchComponentProps(layoutData, context);
|
|
71
|
+
}
|
|
72
|
+
|
|
61
73
|
return {
|
|
62
74
|
props: {
|
|
63
75
|
headLinks: [],
|
|
64
|
-
layoutData
|
|
76
|
+
layoutData,
|
|
77
|
+
componentProps,
|
|
65
78
|
},
|
|
66
79
|
};
|
|
67
80
|
};
|
|
@@ -1,17 +1,19 @@
|
|
|
1
|
+
import { GetStaticProps } from 'next';
|
|
1
2
|
import { JSX } from 'react';
|
|
2
3
|
import Head from 'next/head';
|
|
3
4
|
import {
|
|
4
5
|
GraphQLErrorPagesService,
|
|
5
6
|
SitecoreContext,
|
|
7
|
+
ComponentPropsContext,
|
|
6
8
|
ErrorPages,
|
|
7
9
|
} from '@sitecore-jss/sitecore-jss-nextjs';
|
|
8
10
|
import { SitecorePageProps } from 'lib/page-props';
|
|
9
11
|
import Layout from 'src/Layout';
|
|
10
12
|
import { componentBuilder } from 'temp/componentBuilder';
|
|
11
|
-
import { GetStaticProps } from 'next';
|
|
12
|
-
import config from 'temp/config';
|
|
13
13
|
import { siteResolver } from 'lib/site-resolver';
|
|
14
14
|
import clientFactory from 'lib/graphql-client-factory';
|
|
15
|
+
import { fetchComponentProps } from 'lib/component-props';
|
|
16
|
+
import config from 'temp/config';
|
|
15
17
|
|
|
16
18
|
/**
|
|
17
19
|
* Rendered in case if we have 500 error
|
|
@@ -35,20 +37,22 @@ const Custom500 = (props: SitecorePageProps): JSX.Element => {
|
|
|
35
37
|
}
|
|
36
38
|
|
|
37
39
|
return (
|
|
38
|
-
<
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
40
|
+
<ComponentPropsContext value={props.componentProps}>
|
|
41
|
+
<SitecoreContext
|
|
42
|
+
componentFactory={componentBuilder.getComponentFactory()}
|
|
43
|
+
layoutData={props.layoutData}
|
|
44
|
+
<% if (templates.includes('nextjs-xmcloud')) { %>
|
|
45
|
+
api={{
|
|
46
|
+
edge: {
|
|
47
|
+
contextId: config.sitecoreEdgeContextId,
|
|
48
|
+
edgeUrl: config.sitecoreEdgeUrl,
|
|
49
|
+
},
|
|
50
|
+
}}
|
|
51
|
+
<% } %>
|
|
52
|
+
>
|
|
53
|
+
<Layout layoutData={props.layoutData} headLinks={props.headLinks} />
|
|
54
|
+
</SitecoreContext>
|
|
55
|
+
</ComponentPropsContext>
|
|
52
56
|
);
|
|
53
57
|
};
|
|
54
58
|
|
|
@@ -74,10 +78,19 @@ export const getStaticProps: GetStaticProps = async (context) => {
|
|
|
74
78
|
}
|
|
75
79
|
}
|
|
76
80
|
|
|
81
|
+
const layoutData = resultErrorPages?.notFoundPage?.rendered || null;
|
|
82
|
+
|
|
83
|
+
let componentProps = {};
|
|
84
|
+
|
|
85
|
+
if (layoutData?.sitecore?.route) {
|
|
86
|
+
componentProps = await fetchComponentProps(layoutData, context);
|
|
87
|
+
}
|
|
88
|
+
|
|
77
89
|
return {
|
|
78
90
|
props: {
|
|
79
91
|
headLinks: [],
|
|
80
|
-
layoutData
|
|
92
|
+
layoutData,
|
|
93
|
+
componentProps,
|
|
81
94
|
},
|
|
82
95
|
};
|
|
83
96
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "create-sitecore-jss",
|
|
3
|
-
"version": "22.10.0-canary.
|
|
3
|
+
"version": "22.10.0-canary.3",
|
|
4
4
|
"description": "Sitecore JSS initializer",
|
|
5
5
|
"bin": "./dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -63,5 +63,5 @@
|
|
|
63
63
|
"ts-node": "^10.9.2",
|
|
64
64
|
"typescript": "~5.9.2"
|
|
65
65
|
},
|
|
66
|
-
"gitHead": "
|
|
66
|
+
"gitHead": "6b65bba899f6e3d2b242bc0f7575ca8302f7706e"
|
|
67
67
|
}
|