@prezly/theme-kit-nextjs 1.14.1 → 1.15.1
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/README.md +55 -0
- package/build/adapter-nextjs/getNewsroomStaticProps.d.ts +15 -0
- package/build/adapter-nextjs/getNewsroomStaticProps.js +69 -0
- package/build/adapter-nextjs/getNewsroomStaticProps.js.map +1 -0
- package/build/adapter-nextjs/index.d.ts +2 -0
- package/build/adapter-nextjs/index.js +2 -0
- package/build/adapter-nextjs/index.js.map +1 -1
- package/build/adapter-nextjs/page-props/category.d.ts +3 -1
- package/build/adapter-nextjs/page-props/category.js +104 -1
- package/build/adapter-nextjs/page-props/category.js.map +1 -1
- package/build/adapter-nextjs/page-props/gallery.d.ts +2 -1
- package/build/adapter-nextjs/page-props/gallery.js +48 -1
- package/build/adapter-nextjs/page-props/gallery.js.map +1 -1
- package/build/adapter-nextjs/page-props/galleryAlbum.d.ts +3 -1
- package/build/adapter-nextjs/page-props/galleryAlbum.js +61 -1
- package/build/adapter-nextjs/page-props/galleryAlbum.js.map +1 -1
- package/build/adapter-nextjs/page-props/home.d.ts +2 -1
- package/build/adapter-nextjs/page-props/home.js +48 -1
- package/build/adapter-nextjs/page-props/home.js.map +1 -1
- package/build/adapter-nextjs/page-props/index.d.ts +6 -6
- package/build/adapter-nextjs/page-props/index.js +10 -1
- package/build/adapter-nextjs/page-props/index.js.map +1 -1
- package/build/adapter-nextjs/page-props/lib/types.d.ts +2 -2
- package/build/adapter-nextjs/page-props/search.d.ts +1 -0
- package/build/adapter-nextjs/page-props/search.js +30 -1
- package/build/adapter-nextjs/page-props/search.js.map +1 -1
- package/build/adapter-nextjs/page-props/story.d.ts +10 -1
- package/build/adapter-nextjs/page-props/story.js +69 -1
- package/build/adapter-nextjs/page-props/story.js.map +1 -1
- package/build/adapter-nextjs/processStaticRequest.d.ts +11 -0
- package/build/adapter-nextjs/processStaticRequest.js +37 -0
- package/build/adapter-nextjs/processStaticRequest.js.map +1 -0
- package/build/data-fetching/api/PrezlyApi.d.ts +3 -2
- package/build/data-fetching/api/PrezlyApi.js +18 -3
- package/build/data-fetching/api/PrezlyApi.js.map +1 -1
- package/build/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/adapter-nextjs/getNewsroomStaticProps.ts +29 -0
- package/src/adapter-nextjs/index.ts +2 -0
- package/src/adapter-nextjs/page-props/category.ts +88 -2
- package/src/adapter-nextjs/page-props/gallery.ts +51 -1
- package/src/adapter-nextjs/page-props/galleryAlbum.ts +50 -1
- package/src/adapter-nextjs/page-props/home.ts +49 -1
- package/src/adapter-nextjs/page-props/index.ts +24 -6
- package/src/adapter-nextjs/page-props/lib/types.ts +5 -5
- package/src/adapter-nextjs/page-props/search.ts +27 -1
- package/src/adapter-nextjs/page-props/story.ts +52 -1
- package/src/adapter-nextjs/processStaticRequest.ts +32 -0
- package/src/data-fetching/api/PrezlyApi.ts +12 -4
|
@@ -1,10 +1,20 @@
|
|
|
1
1
|
import type { ExtraStoryFields, Story } from '@prezly/sdk';
|
|
2
|
-
import type {
|
|
2
|
+
import type {
|
|
3
|
+
GetServerSidePropsContext,
|
|
4
|
+
GetServerSidePropsResult,
|
|
5
|
+
GetStaticPathsResult,
|
|
6
|
+
GetStaticPropsContext,
|
|
7
|
+
GetStaticPropsResult,
|
|
8
|
+
} from 'next';
|
|
3
9
|
|
|
10
|
+
import { getPrezlyApi } from '../../data-fetching';
|
|
4
11
|
import type { PaginationProps } from '../../infinite-loading/types';
|
|
5
|
-
import {
|
|
12
|
+
import { LocaleObject } from '../../intl';
|
|
13
|
+
import { DEFAULT_PAGE_SIZE, getLocalizedCategoryData } from '../../utils';
|
|
6
14
|
import { getNewsroomServerSideProps } from '../getNewsroomServerSideProps';
|
|
15
|
+
import { getNewsroomStaticProps } from '../getNewsroomStaticProps';
|
|
7
16
|
import { processRequest } from '../processRequest';
|
|
17
|
+
import { processStaticRequest } from '../processStaticRequest';
|
|
8
18
|
|
|
9
19
|
import type { PropsFunction } from './lib/types';
|
|
10
20
|
|
|
@@ -73,3 +83,79 @@ export function getCategoryPageServerSideProps<
|
|
|
73
83
|
);
|
|
74
84
|
};
|
|
75
85
|
}
|
|
86
|
+
|
|
87
|
+
export function getCategoryPageStaticProps<
|
|
88
|
+
CustomProps extends Record<string, any>,
|
|
89
|
+
StoryType extends Story = Story,
|
|
90
|
+
>(customProps: CustomProps | PropsFunction<CustomProps>, options?: Options) {
|
|
91
|
+
const { pageSize = DEFAULT_PAGE_SIZE, extraStoryFields } = options || {};
|
|
92
|
+
|
|
93
|
+
return async function getStaticProps(
|
|
94
|
+
context: GetStaticPropsContext,
|
|
95
|
+
): Promise<GetStaticPropsResult<CategoryPageProps<StoryType> & CustomProps>> {
|
|
96
|
+
const { api, staticProps } = await getNewsroomStaticProps(context);
|
|
97
|
+
|
|
98
|
+
const { slug } = context.params as { slug: string };
|
|
99
|
+
const category = await api.getCategoryBySlug(slug);
|
|
100
|
+
|
|
101
|
+
if (!category) {
|
|
102
|
+
return {
|
|
103
|
+
notFound: true,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const { localeCode } = staticProps.newsroomContextProps;
|
|
108
|
+
|
|
109
|
+
const { stories, storiesTotal } = await api.getStoriesFromCategory(category, {
|
|
110
|
+
pageSize,
|
|
111
|
+
include: extraStoryFields,
|
|
112
|
+
localeCode,
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
return processStaticRequest(context, {
|
|
116
|
+
...staticProps,
|
|
117
|
+
newsroomContextProps: {
|
|
118
|
+
...staticProps.newsroomContextProps,
|
|
119
|
+
currentCategory: category,
|
|
120
|
+
},
|
|
121
|
+
// TODO: This is temporary until return types from API are figured out
|
|
122
|
+
stories: stories as StoryType[],
|
|
123
|
+
pagination: {
|
|
124
|
+
itemsTotal: storiesTotal,
|
|
125
|
+
currentPage: 1,
|
|
126
|
+
pageSize,
|
|
127
|
+
},
|
|
128
|
+
...(typeof customProps === 'function'
|
|
129
|
+
? await (customProps as PropsFunction<CustomProps, GetStaticPropsContext>)(
|
|
130
|
+
context,
|
|
131
|
+
staticProps,
|
|
132
|
+
)
|
|
133
|
+
: customProps),
|
|
134
|
+
});
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export async function getCategoryPageStaticPaths(): Promise<GetStaticPathsResult> {
|
|
139
|
+
const api = getPrezlyApi();
|
|
140
|
+
const [categories, defaultLanguage] = await Promise.all([
|
|
141
|
+
api.getCategories(),
|
|
142
|
+
api.getNewsroomDefaultLanguage(),
|
|
143
|
+
]);
|
|
144
|
+
const locale = LocaleObject.fromAnyCode(defaultLanguage.code);
|
|
145
|
+
|
|
146
|
+
const paths = categories
|
|
147
|
+
.map((category) => {
|
|
148
|
+
const { slug } = getLocalizedCategoryData(category, locale);
|
|
149
|
+
if (!slug) {
|
|
150
|
+
return undefined;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return { params: { slug } };
|
|
154
|
+
})
|
|
155
|
+
.filter<{ params: { slug: string } }>(Boolean as any);
|
|
156
|
+
|
|
157
|
+
return {
|
|
158
|
+
paths,
|
|
159
|
+
fallback: 'blocking',
|
|
160
|
+
};
|
|
161
|
+
}
|
|
@@ -1,10 +1,17 @@
|
|
|
1
1
|
import type { NewsroomGallery } from '@prezly/sdk';
|
|
2
|
-
import type {
|
|
2
|
+
import type {
|
|
3
|
+
GetServerSidePropsContext,
|
|
4
|
+
GetServerSidePropsResult,
|
|
5
|
+
GetStaticPropsContext,
|
|
6
|
+
GetStaticPropsResult,
|
|
7
|
+
} from 'next';
|
|
3
8
|
|
|
4
9
|
import type { PaginationProps } from '../../infinite-loading/types';
|
|
5
10
|
import { DEFAULT_GALLERY_PAGE_SIZE } from '../../utils';
|
|
6
11
|
import { getNewsroomServerSideProps } from '../getNewsroomServerSideProps';
|
|
12
|
+
import { getNewsroomStaticProps } from '../getNewsroomStaticProps';
|
|
7
13
|
import { processRequest } from '../processRequest';
|
|
14
|
+
import { processStaticRequest } from '../processStaticRequest';
|
|
8
15
|
|
|
9
16
|
import type { PropsFunction } from './lib/types';
|
|
10
17
|
|
|
@@ -62,3 +69,46 @@ export function getGalleryPageServerSideProps<CustomProps extends Record<string,
|
|
|
62
69
|
);
|
|
63
70
|
};
|
|
64
71
|
}
|
|
72
|
+
|
|
73
|
+
export function getGalleryPageStaticProps<CustomProps extends Record<string, any>>(
|
|
74
|
+
customProps: CustomProps | PropsFunction<CustomProps>,
|
|
75
|
+
options?: Options,
|
|
76
|
+
) {
|
|
77
|
+
const { pageSize = DEFAULT_GALLERY_PAGE_SIZE } = options || {};
|
|
78
|
+
|
|
79
|
+
return async function getStaticProps(
|
|
80
|
+
context: GetStaticPropsContext,
|
|
81
|
+
): Promise<GetStaticPropsResult<GalleryPageProps & CustomProps>> {
|
|
82
|
+
const { api, staticProps } = await getNewsroomStaticProps(context);
|
|
83
|
+
|
|
84
|
+
const { galleries, pagination } = await api.getGalleries({ pageSize });
|
|
85
|
+
|
|
86
|
+
// If there's only one gallery, redirect to it immediately
|
|
87
|
+
if (galleries.length === 1) {
|
|
88
|
+
const { uuid } = galleries[0];
|
|
89
|
+
|
|
90
|
+
return {
|
|
91
|
+
redirect: {
|
|
92
|
+
destination: `/media/album/${uuid}`,
|
|
93
|
+
permanent: false,
|
|
94
|
+
},
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return processStaticRequest(context, {
|
|
99
|
+
...staticProps,
|
|
100
|
+
galleries,
|
|
101
|
+
pagination: {
|
|
102
|
+
itemsTotal: pagination.matched_records_number,
|
|
103
|
+
currentPage: 1,
|
|
104
|
+
pageSize,
|
|
105
|
+
},
|
|
106
|
+
...(typeof customProps === 'function'
|
|
107
|
+
? await (customProps as PropsFunction<CustomProps, GetStaticPropsContext>)(
|
|
108
|
+
context,
|
|
109
|
+
staticProps,
|
|
110
|
+
)
|
|
111
|
+
: customProps),
|
|
112
|
+
});
|
|
113
|
+
};
|
|
114
|
+
}
|
|
@@ -1,8 +1,17 @@
|
|
|
1
1
|
import type { NewsroomGallery } from '@prezly/sdk';
|
|
2
|
-
import type {
|
|
2
|
+
import type {
|
|
3
|
+
GetServerSidePropsContext,
|
|
4
|
+
GetServerSidePropsResult,
|
|
5
|
+
GetStaticPathsResult,
|
|
6
|
+
GetStaticPropsContext,
|
|
7
|
+
GetStaticPropsResult,
|
|
8
|
+
} from 'next';
|
|
3
9
|
|
|
10
|
+
import { getPrezlyApi } from '../../data-fetching';
|
|
4
11
|
import { getNewsroomServerSideProps } from '../getNewsroomServerSideProps';
|
|
12
|
+
import { getNewsroomStaticProps } from '../getNewsroomStaticProps';
|
|
5
13
|
import { processRequest } from '../processRequest';
|
|
14
|
+
import { processStaticRequest } from '../processStaticRequest';
|
|
6
15
|
|
|
7
16
|
import type { PropsFunction } from './lib/types';
|
|
8
17
|
|
|
@@ -38,3 +47,43 @@ export function getGalleryAlbumPageServerSideProps<CustomProps extends Record<st
|
|
|
38
47
|
);
|
|
39
48
|
};
|
|
40
49
|
}
|
|
50
|
+
|
|
51
|
+
export function getGalleryAlbumPageStaticProps<CustomProps extends Record<string, any>>(
|
|
52
|
+
customProps: CustomProps | PropsFunction<CustomProps>,
|
|
53
|
+
) {
|
|
54
|
+
return async function getStaticProps(
|
|
55
|
+
context: GetStaticPropsContext,
|
|
56
|
+
): Promise<GetStaticPropsResult<GalleryAlbumPageProps & CustomProps>> {
|
|
57
|
+
const { api, staticProps } = await getNewsroomStaticProps(context);
|
|
58
|
+
|
|
59
|
+
const { uuid } = context.params as { uuid: string };
|
|
60
|
+
const gallery = await api.getGallery(uuid);
|
|
61
|
+
|
|
62
|
+
if (!gallery || !gallery.images_number) {
|
|
63
|
+
return { notFound: true };
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return processStaticRequest(context, {
|
|
67
|
+
...staticProps,
|
|
68
|
+
gallery,
|
|
69
|
+
...(typeof customProps === 'function'
|
|
70
|
+
? await (customProps as PropsFunction<CustomProps, GetStaticPropsContext>)(
|
|
71
|
+
context,
|
|
72
|
+
staticProps,
|
|
73
|
+
)
|
|
74
|
+
: customProps),
|
|
75
|
+
});
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export async function getGalleryAlbumPageStaticPaths(): Promise<GetStaticPathsResult> {
|
|
80
|
+
const api = getPrezlyApi();
|
|
81
|
+
const { galleries } = await api.getGalleries({});
|
|
82
|
+
|
|
83
|
+
const paths = galleries.map(({ uuid }) => ({ params: { uuid } }));
|
|
84
|
+
|
|
85
|
+
return {
|
|
86
|
+
paths,
|
|
87
|
+
fallback: 'blocking',
|
|
88
|
+
};
|
|
89
|
+
}
|
|
@@ -1,10 +1,17 @@
|
|
|
1
1
|
import type { ExtraStoryFields, Story } from '@prezly/sdk';
|
|
2
|
-
import type {
|
|
2
|
+
import type {
|
|
3
|
+
GetServerSidePropsContext,
|
|
4
|
+
GetServerSidePropsResult,
|
|
5
|
+
GetStaticPropsContext,
|
|
6
|
+
GetStaticPropsResult,
|
|
7
|
+
} from 'next';
|
|
3
8
|
|
|
4
9
|
import type { PaginationProps } from '../../infinite-loading/types';
|
|
5
10
|
import { DEFAULT_PAGE_SIZE } from '../../utils';
|
|
6
11
|
import { getNewsroomServerSideProps } from '../getNewsroomServerSideProps';
|
|
12
|
+
import { getNewsroomStaticProps } from '../getNewsroomStaticProps';
|
|
7
13
|
import { processRequest } from '../processRequest';
|
|
14
|
+
import { processStaticRequest } from '../processStaticRequest';
|
|
8
15
|
|
|
9
16
|
import type { PropsFunction } from './lib/types';
|
|
10
17
|
|
|
@@ -63,3 +70,44 @@ export function getHomepageServerSideProps<
|
|
|
63
70
|
);
|
|
64
71
|
};
|
|
65
72
|
}
|
|
73
|
+
|
|
74
|
+
export function getHomepageStaticProps<
|
|
75
|
+
CustomProps extends Record<string, any>,
|
|
76
|
+
StoryType extends Story = Story,
|
|
77
|
+
>(customProps: CustomProps | PropsFunction<CustomProps>, options?: Options) {
|
|
78
|
+
const { pageSize = DEFAULT_PAGE_SIZE, extraStoryFields } = options || {};
|
|
79
|
+
|
|
80
|
+
return async function getStaticProps(
|
|
81
|
+
context: GetStaticPropsContext,
|
|
82
|
+
): Promise<GetStaticPropsResult<HomePageProps<StoryType> & CustomProps>> {
|
|
83
|
+
const { api, staticProps } = await getNewsroomStaticProps(context, {
|
|
84
|
+
loadHomepageContacts: true,
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
const { localeCode } = staticProps.newsroomContextProps;
|
|
88
|
+
|
|
89
|
+
const storiesPaginated = await api.getStories({
|
|
90
|
+
pageSize,
|
|
91
|
+
include: extraStoryFields,
|
|
92
|
+
localeCode,
|
|
93
|
+
});
|
|
94
|
+
const { stories, storiesTotal } = storiesPaginated;
|
|
95
|
+
|
|
96
|
+
return processStaticRequest(context, {
|
|
97
|
+
...staticProps,
|
|
98
|
+
// TODO: This is temporary until return types from API are figured out
|
|
99
|
+
stories: stories as StoryType[],
|
|
100
|
+
pagination: {
|
|
101
|
+
itemsTotal: storiesTotal,
|
|
102
|
+
currentPage: 1,
|
|
103
|
+
pageSize,
|
|
104
|
+
},
|
|
105
|
+
...(typeof customProps === 'function'
|
|
106
|
+
? await (customProps as PropsFunction<CustomProps, GetStaticPropsContext>)(
|
|
107
|
+
context,
|
|
108
|
+
staticProps,
|
|
109
|
+
)
|
|
110
|
+
: customProps),
|
|
111
|
+
});
|
|
112
|
+
};
|
|
113
|
+
}
|
|
@@ -1,8 +1,26 @@
|
|
|
1
|
-
export {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
export {
|
|
2
|
+
getCategoryPageServerSideProps,
|
|
3
|
+
getCategoryPageStaticPaths,
|
|
4
|
+
getCategoryPageStaticProps,
|
|
5
|
+
type CategoryPageProps,
|
|
6
|
+
} from './category';
|
|
7
|
+
export {
|
|
8
|
+
getGalleryPageServerSideProps,
|
|
9
|
+
getGalleryPageStaticProps,
|
|
10
|
+
type GalleryPageProps,
|
|
11
|
+
} from './gallery';
|
|
12
|
+
export {
|
|
13
|
+
getGalleryAlbumPageServerSideProps,
|
|
14
|
+
getGalleryAlbumPageStaticPaths,
|
|
15
|
+
getGalleryAlbumPageStaticProps,
|
|
16
|
+
type GalleryAlbumPageProps,
|
|
17
|
+
} from './galleryAlbum';
|
|
18
|
+
export { getHomepageServerSideProps, getHomepageStaticProps, type HomePageProps } from './home';
|
|
19
|
+
export { getSearchPageServerSideProps, getSearchPageStaticProps } from './search';
|
|
6
20
|
export * from './sitemap';
|
|
7
|
-
export {
|
|
21
|
+
export {
|
|
22
|
+
getStoryPageServerSideProps,
|
|
23
|
+
getStoryPageStaticPaths,
|
|
24
|
+
getStoryPageStaticProps,
|
|
25
|
+
} from './story';
|
|
8
26
|
export { getStoryPreviewPageServerSideProps } from './storyPreview';
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import type { GetServerSidePropsContext } from 'next';
|
|
1
|
+
import type { GetServerSidePropsContext, GetStaticPropsContext } from 'next';
|
|
2
2
|
|
|
3
3
|
import type { PageProps } from '../../../types';
|
|
4
4
|
|
|
5
|
-
export type PropsFunction<
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
) => CustomProps | Promise<CustomProps>;
|
|
5
|
+
export type PropsFunction<
|
|
6
|
+
CustomProps,
|
|
7
|
+
Context extends GetServerSidePropsContext | GetStaticPropsContext = GetServerSidePropsContext,
|
|
8
|
+
> = (context: Context, props: PageProps) => CustomProps | Promise<CustomProps>;
|
|
@@ -1,7 +1,13 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
GetServerSidePropsContext,
|
|
3
|
+
GetServerSidePropsResult,
|
|
4
|
+
GetStaticPropsContext,
|
|
5
|
+
} from 'next';
|
|
2
6
|
|
|
3
7
|
import { getNewsroomServerSideProps } from '../getNewsroomServerSideProps';
|
|
8
|
+
import { getNewsroomStaticProps } from '../getNewsroomStaticProps';
|
|
4
9
|
import { processRequest } from '../processRequest';
|
|
10
|
+
import { processStaticRequest } from '../processStaticRequest';
|
|
5
11
|
|
|
6
12
|
import type { PropsFunction } from './lib/types';
|
|
7
13
|
|
|
@@ -25,3 +31,23 @@ export function getSearchPageServerSideProps<CustomProps extends Record<string,
|
|
|
25
31
|
);
|
|
26
32
|
};
|
|
27
33
|
}
|
|
34
|
+
|
|
35
|
+
export function getSearchPageStaticProps<CustomProps extends Record<string, any>>(
|
|
36
|
+
customProps: CustomProps | PropsFunction<CustomProps>,
|
|
37
|
+
) {
|
|
38
|
+
return async function getStaticProps(
|
|
39
|
+
context: GetServerSidePropsContext,
|
|
40
|
+
): Promise<GetServerSidePropsResult<CustomProps>> {
|
|
41
|
+
const { staticProps } = await getNewsroomStaticProps(context);
|
|
42
|
+
|
|
43
|
+
return processStaticRequest(context, {
|
|
44
|
+
...staticProps,
|
|
45
|
+
...(typeof customProps === 'function'
|
|
46
|
+
? await (customProps as PropsFunction<CustomProps, GetStaticPropsContext>)(
|
|
47
|
+
context,
|
|
48
|
+
staticProps,
|
|
49
|
+
)
|
|
50
|
+
: customProps),
|
|
51
|
+
});
|
|
52
|
+
};
|
|
53
|
+
}
|
|
@@ -1,9 +1,16 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
GetServerSidePropsContext,
|
|
3
|
+
GetServerSidePropsResult,
|
|
4
|
+
GetStaticPropsContext,
|
|
5
|
+
GetStaticPropsResult,
|
|
6
|
+
} from 'next';
|
|
2
7
|
|
|
3
8
|
import { getPrezlyApi } from '../../data-fetching';
|
|
4
9
|
import { DUMMY_DEFAULT_LOCALE } from '../../intl';
|
|
5
10
|
import { getNewsroomServerSideProps } from '../getNewsroomServerSideProps';
|
|
11
|
+
import { getNewsroomStaticProps } from '../getNewsroomStaticProps';
|
|
6
12
|
import { processRequest } from '../processRequest';
|
|
13
|
+
import { processStaticRequest } from '../processStaticRequest';
|
|
7
14
|
|
|
8
15
|
import type { PropsFunction } from './lib/types';
|
|
9
16
|
|
|
@@ -45,3 +52,47 @@ export function getStoryPageServerSideProps<CustomProps extends Record<string, a
|
|
|
45
52
|
});
|
|
46
53
|
};
|
|
47
54
|
}
|
|
55
|
+
|
|
56
|
+
export function getStoryPageStaticProps<CustomProps extends Record<string, any>>(
|
|
57
|
+
customProps: CustomProps | PropsFunction<CustomProps>,
|
|
58
|
+
) {
|
|
59
|
+
return async function getStaticProps(
|
|
60
|
+
context: GetStaticPropsContext,
|
|
61
|
+
): Promise<GetStaticPropsResult<CustomProps>> {
|
|
62
|
+
const api = getPrezlyApi();
|
|
63
|
+
|
|
64
|
+
const { slug } = context.params as { slug?: string };
|
|
65
|
+
const story = slug ? await api.getStoryBySlug(slug) : null;
|
|
66
|
+
if (!story) {
|
|
67
|
+
return { notFound: true };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const { staticProps } = await getNewsroomStaticProps(context, { story });
|
|
71
|
+
|
|
72
|
+
return processStaticRequest(context, {
|
|
73
|
+
...staticProps,
|
|
74
|
+
newsroomContextProps: {
|
|
75
|
+
...staticProps.newsroomContextProps,
|
|
76
|
+
currentStory: story,
|
|
77
|
+
},
|
|
78
|
+
...(typeof customProps === 'function'
|
|
79
|
+
? await (customProps as PropsFunction<CustomProps, GetStaticPropsContext>)(
|
|
80
|
+
context,
|
|
81
|
+
staticProps,
|
|
82
|
+
)
|
|
83
|
+
: customProps),
|
|
84
|
+
});
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export async function getStoryPageStaticPaths() {
|
|
89
|
+
const api = getPrezlyApi();
|
|
90
|
+
const stories = await api.getAllStories();
|
|
91
|
+
|
|
92
|
+
const paths = stories.map(({ slug }) => ({ params: { slug } }));
|
|
93
|
+
|
|
94
|
+
return {
|
|
95
|
+
paths,
|
|
96
|
+
fallback: 'blocking',
|
|
97
|
+
};
|
|
98
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { GetStaticPropsContext, GetStaticPropsResult } from 'next';
|
|
2
|
+
|
|
3
|
+
import type { PageProps, ServerSidePageProps } from '../types';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* This function combines the props returned from `getNewsroomStaticProps` and your optional custom props for the page.
|
|
7
|
+
* See README for usage examples.
|
|
8
|
+
*
|
|
9
|
+
* @param context Next.js context passed to the `getStaticProps` method of the page
|
|
10
|
+
* @param props Object containing the return of `getNewsroomServerSideProps` and your optional custom props.
|
|
11
|
+
* @param revalidateTimeout Optional: timeout in seconds to use with Next ISR.
|
|
12
|
+
*/
|
|
13
|
+
export function processStaticRequest<Props>(
|
|
14
|
+
context: GetStaticPropsContext,
|
|
15
|
+
props: Props & PageProps & ServerSidePageProps,
|
|
16
|
+
revalidateTimeout?: number,
|
|
17
|
+
): GetStaticPropsResult<Props> {
|
|
18
|
+
const { locale: nextLocale } = context;
|
|
19
|
+
|
|
20
|
+
const { localeResolved, ...pageProps } = props;
|
|
21
|
+
|
|
22
|
+
// If no locale was provided by Next, it most likely means that the theme is not supporting multi-language.
|
|
23
|
+
// In that case we won't show 404 page.
|
|
24
|
+
if (!localeResolved && nextLocale) {
|
|
25
|
+
return { notFound: true };
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return {
|
|
29
|
+
props: pageProps as Props & PageProps,
|
|
30
|
+
revalidate: revalidateTimeout,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
@@ -45,7 +45,7 @@ interface GetStoriesOptions {
|
|
|
45
45
|
|
|
46
46
|
interface GetGalleriesOptions {
|
|
47
47
|
page?: number;
|
|
48
|
-
pageSize
|
|
48
|
+
pageSize?: number;
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
export class PrezlyApi {
|
|
@@ -210,10 +210,13 @@ export class PrezlyApi {
|
|
|
210
210
|
searchStories: typeof PrezlySDK.prototype.stories.search = (options) =>
|
|
211
211
|
this.sdk.stories.search(options);
|
|
212
212
|
|
|
213
|
-
async getGalleries({ page
|
|
213
|
+
async getGalleries({ page, pageSize }: GetGalleriesOptions) {
|
|
214
214
|
return this.sdk.newsroomGalleries.list(this.newsroomUuid, {
|
|
215
215
|
limit: pageSize,
|
|
216
|
-
offset:
|
|
216
|
+
offset:
|
|
217
|
+
typeof page === 'undefined' || typeof pageSize === 'undefined'
|
|
218
|
+
? undefined
|
|
219
|
+
: (page - 1) * pageSize,
|
|
217
220
|
scope: getGalleriesQuery(),
|
|
218
221
|
});
|
|
219
222
|
}
|
|
@@ -248,7 +251,7 @@ export class PrezlyApi {
|
|
|
248
251
|
}
|
|
249
252
|
|
|
250
253
|
async getNewsroomServerSideProps(
|
|
251
|
-
request
|
|
254
|
+
request?: IncomingMessage,
|
|
252
255
|
nextLocaleIsoCode?: string,
|
|
253
256
|
story?: Story,
|
|
254
257
|
): Promise<PageProps & ServerSidePageProps> {
|
|
@@ -285,4 +288,9 @@ export class PrezlyApi {
|
|
|
285
288
|
localeResolved: Boolean(currentLanguage),
|
|
286
289
|
};
|
|
287
290
|
}
|
|
291
|
+
|
|
292
|
+
async getNewsroomDefaultLanguage() {
|
|
293
|
+
const languages = await this.getNewsroomLanguages();
|
|
294
|
+
return getDefaultLanguage(languages);
|
|
295
|
+
}
|
|
288
296
|
}
|