From 8c57c695838cf6cc8d818b16282c8f2568ef59ee Mon Sep 17 00:00:00 2001 From: Roman Timashev Date: Sun, 23 Feb 2020 22:54:15 +0300 Subject: [PATCH 01/16] Meta component changes in definition --- src/components/Meta/index.tsx | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/components/Meta/index.tsx b/src/components/Meta/index.tsx index 102936eb..e5397ac7 100644 --- a/src/components/Meta/index.tsx +++ b/src/components/Meta/index.tsx @@ -1,11 +1,10 @@ -import * as React from 'react' -import { IMeta } from '../../types' +import { IMeta } from 'src/types' -export interface IMetaProps { +export type MetaProps = { meta: IMeta } -export const Meta: React.FC = ({ meta }) => ( +export const Meta: React.FC = ({ meta }) => ( <> {/* Search Engine */} From 0fc762c418e6918de5e6cfd7501b6a9276d70630 Mon Sep 17 00:00:00 2001 From: Roman Timashev Date: Sun, 23 Feb 2020 22:54:43 +0300 Subject: [PATCH 02/16] meta lib refactoring: to ts; add meta builder --- src/lib/meta.js | 23 ------------------- src/lib/meta.ts | 60 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 23 deletions(-) delete mode 100644 src/lib/meta.js create mode 100644 src/lib/meta.ts diff --git a/src/lib/meta.js b/src/lib/meta.js deleted file mode 100644 index 5cda25ab..00000000 --- a/src/lib/meta.js +++ /dev/null @@ -1,23 +0,0 @@ -export function meta(props) { - const path = props.url || '/' - const url = `https://art.shlisselburg.org${path}` - - return { - title: 'Шлиссельбургская ДХШ', - description: 'Сайт Шлиссельбургской художественной школы', - image: 'https://art.shlisselburg.org/entrance.jpg', - imageWidth: 1200, - imageHeight: 630, - - siteName: 'Шлиссельбургская Детская Художественная Школа', - locale: 'ru_RU', - type: 'website', - domain: 'art.shlisselburg.org', - twitterCard: 'summary_large_image', - twitterSite: '@', - twitterCreator: '@tmshv', - - ...props, - url, - } -} diff --git a/src/lib/meta.ts b/src/lib/meta.ts new file mode 100644 index 00000000..a4327842 --- /dev/null +++ b/src/lib/meta.ts @@ -0,0 +1,60 @@ +import { IMeta, IImage } from 'src/types' + +export function meta(props: Partial): IMeta { + const path = props.url || '/' + const url = `https://art.shlisselburg.org${path}` + + return { + title: 'Шлиссельбургская ДХШ', + description: 'Сайт Шлиссельбургской художественной школы', + image: 'https://art.shlisselburg.org/entrance.jpg', + imageWidth: 1200, + imageHeight: 630, + + siteName: 'Шлиссельбургская Детская Художественная Школа', + locale: 'ru_RU', + type: 'website', + domain: 'art.shlisselburg.org', + twitterCard: 'summary_large_image', + twitterSite: '@', + twitterCreator: '@tmshv', + + ...props, + url, + } +} + +export class MetaBuilder { + private image: IImage + private title: string + private description: string + private data: Partial + + setData(param: Partial) { + this.data = param + return this + } + + setTitle(param: string) { + this.title = param + return this + } + + setDescription(param: string) { + this.description = param + return this + } + + setImage(param: IImage) { + this.image = param + return this + } + + build() { + return meta({ + ...this.data, + title: this.title, + description: this.description, + }) + } +} From 88d5a56fecf1f201017cabaa8c7e8c14e0815dce Mon Sep 17 00:00:00 2001 From: Roman Timashev Date: Sun, 23 Feb 2020 22:55:06 +0300 Subject: [PATCH 03/16] teacher page uses meta builder --- pages/teacher/[person].tsx | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/pages/teacher/[person].tsx b/pages/teacher/[person].tsx index 87eee949..e2bf19f2 100644 --- a/pages/teacher/[person].tsx +++ b/pages/teacher/[person].tsx @@ -2,7 +2,7 @@ import Head from 'next/head' import { App } from 'src/components/App' import { Article } from 'src/components/Article' import { Meta } from 'src/components/Meta' -import { meta } from 'src/lib/meta' +import { MetaBuilder } from 'src/lib/meta' import { createApiUrl, requestGet, IResponseItems } from 'src/next-lib' import { NextPage } from 'next' import { IMeta } from 'src/types' @@ -66,16 +66,23 @@ export const unstable_getStaticProps = async (ctx: any) => { throw new Error(`post kek ${id}`) } + const meta = (new MetaBuilder()) + .setData({ + title, + image: image.src, + imageWidth: image.width, + imageHeight: image.height, + }) + .setImage(person.preview) + .setTitle(title) + .setDescription(person.position) + .build() + return { props: { person, title, - meta: meta({ - title, - image: image.src, - imageWidth: image.width, - imageHeight: image.height, - }) + meta, } } } From 3f8941b613bd0b710f8bcfd95211607931076d0d Mon Sep 17 00:00:00 2001 From: Roman Timashev Date: Mon, 24 Feb 2020 00:27:41 +0300 Subject: [PATCH 04/16] person page rm debugging code --- pages/teacher/[person].tsx | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/pages/teacher/[person].tsx b/pages/teacher/[person].tsx index e2bf19f2..7957556e 100644 --- a/pages/teacher/[person].tsx +++ b/pages/teacher/[person].tsx @@ -47,12 +47,7 @@ const Index: NextPage = props => { } export const unstable_getStaticProps = async (ctx: any) => { - console.log('call unstable_getStaticProps', ctx) - - // const id = ctx.query.person - // const id = ctx.params.person ?? 'va-sarzhin' - const id = ctx.params.person //?? 'mg-timasheva' - + const id = ctx.params.person if (!id) { throw new Error('sry') } @@ -88,10 +83,7 @@ export const unstable_getStaticProps = async (ctx: any) => { } export const unstable_getStaticPaths = async () => { - console.log('call unstable_getStaticPaths') - const urls = await requestGet>(createApiUrl(`/api/persons/urls`), null) - // const urls = {items: ['/teacher/va-sarzhin']} return { paths: urls.items From dbf09f608af8eae33062c49e8e292f4e4a87c3c2 Mon Sep 17 00:00:00 2001 From: Roman Timashev Date: Mon, 24 Feb 2020 00:30:33 +0300 Subject: [PATCH 05/16] meta builder; person page refactoring --- pages/teacher/[person].tsx | 31 +++++++++++++++++++------------ src/lib/meta.ts | 14 +++++++++++--- 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/pages/teacher/[person].tsx b/pages/teacher/[person].tsx index 7957556e..8cd36744 100644 --- a/pages/teacher/[person].tsx +++ b/pages/teacher/[person].tsx @@ -5,9 +5,25 @@ import { Meta } from 'src/components/Meta' import { MetaBuilder } from 'src/lib/meta' import { createApiUrl, requestGet, IResponseItems } from 'src/next-lib' import { NextPage } from 'next' -import { IMeta } from 'src/types' +import { IMeta, ImageArtifact, ImageDefinition } from 'src/types' import { Html } from 'src/components/Html' +type Person = { + id: string + position: string + name: [string, string, string] + post: string + diploma: string + edu: string + file: string + hash: string + shortName: string + status: string + url: string + picture: ImageArtifact + preview: ImageDefinition +} + interface IProps { title: string meta: IMeta @@ -52,22 +68,13 @@ export const unstable_getStaticProps = async (ctx: any) => { throw new Error('sry') } - const person = await requestGet(createApiUrl(`/api/persons/${id}`), null) - const name = person.name || [] - const title = name.join(' ') - const image = person.preview ? person.preview.artifacts.fb : {} - + const person = await requestGet(createApiUrl(`/api/persons/${id}`), null) if (!person?.post) { throw new Error(`post kek ${id}`) } + const title = person.name.join(' ') const meta = (new MetaBuilder()) - .setData({ - title, - image: image.src, - imageWidth: image.width, - imageHeight: image.height, - }) .setImage(person.preview) .setTitle(title) .setDescription(person.position) diff --git a/src/lib/meta.ts b/src/lib/meta.ts index a4327842..db518e94 100644 --- a/src/lib/meta.ts +++ b/src/lib/meta.ts @@ -1,4 +1,4 @@ -import { IMeta, IImage } from 'src/types' +import { IMeta, IImage, ImageDefinition } from 'src/types' export function meta(props: Partial): IMeta { const path = props.url || '/' @@ -25,7 +25,7 @@ export function meta(props: Partial): IMeta { } export class MetaBuilder { - private image: IImage + private image: ImageDefinition private title: string private description: string private data: Partial @@ -45,14 +45,22 @@ export class MetaBuilder { return this } - setImage(param: IImage) { + setImage(param: ImageDefinition) { this.image = param return this } build() { + const artifact = this.image.artifacts.fb + const image = { + image: artifact.src, + imageWidth: artifact.width, + imageHeight: artifact.height, + } + return meta({ ...this.data, + ...image, title: this.title, description: this.description, }) From b8d0c7aa9129c73f57bde03e26f558ba3fb83950 Mon Sep 17 00:00:00 2001 From: Roman Timashev Date: Mon, 24 Feb 2020 02:27:28 +0300 Subject: [PATCH 06/16] added person and image related types --- src/types.ts | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/types.ts b/src/types.ts index c64d9bcd..1875539f 100644 --- a/src/types.ts +++ b/src/types.ts @@ -67,3 +67,42 @@ export interface IArticle { title: string preview: IImage } + +export type Person = { + id: string + position: string + name: [string, string, string] + post: string + diploma: string + edu: string + file: string + hash: string + shortName: string + status: string + url: string + picture: ImageArtifact + preview: ImageDefinition +} + +export type ImageArtifact = { + width: number + height: number + src: string + set: Array<{ + density: number + url: string + }> +} + +export type ImageDefinition = { + file: string + hash: string + artifacts: { + original: ImageArtifact + large?: ImageArtifact + big?: ImageArtifact + medium?: ImageArtifact + small?: ImageArtifact + fb?: ImageArtifact + } +} From cdeece2b0bd7ec0da405dcc071884b4a61c0ceb8 Mon Sep 17 00:00:00 2001 From: Roman Timashev Date: Mon, 24 Feb 2020 02:27:52 +0300 Subject: [PATCH 07/16] person page cleanup --- pages/teacher/[person].tsx | 20 ++------------------ 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/pages/teacher/[person].tsx b/pages/teacher/[person].tsx index 8cd36744..edfb04eb 100644 --- a/pages/teacher/[person].tsx +++ b/pages/teacher/[person].tsx @@ -5,25 +5,9 @@ import { Meta } from 'src/components/Meta' import { MetaBuilder } from 'src/lib/meta' import { createApiUrl, requestGet, IResponseItems } from 'src/next-lib' import { NextPage } from 'next' -import { IMeta, ImageArtifact, ImageDefinition } from 'src/types' +import { IMeta, Person } from 'src/types' import { Html } from 'src/components/Html' -type Person = { - id: string - position: string - name: [string, string, string] - post: string - diploma: string - edu: string - file: string - hash: string - shortName: string - status: string - url: string - picture: ImageArtifact - preview: ImageDefinition -} - interface IProps { title: string meta: IMeta @@ -63,7 +47,7 @@ const Index: NextPage = props => { } export const unstable_getStaticProps = async (ctx: any) => { - const id = ctx.params.person + const id = ctx.params.person if (!id) { throw new Error('sry') } From 76e7a7e9bb90173787f8bc7508dca5ec3d3da3f4 Mon Sep 17 00:00:00 2001 From: Roman Timashev Date: Mon, 24 Feb 2020 02:29:01 +0300 Subject: [PATCH 08/16] collective page card style changed; pass common card data to grid --- pages/collective.tsx | 63 +++++++++++++++++++++++++++----------------- 1 file changed, 39 insertions(+), 24 deletions(-) diff --git a/pages/collective.tsx b/pages/collective.tsx index 95345efa..98cc04c9 100644 --- a/pages/collective.tsx +++ b/pages/collective.tsx @@ -4,24 +4,27 @@ import { App } from 'src/components/App' import { Meta } from 'src/components/Meta' import { CollectiveImage } from 'src/components/CollectiveImage' import { meta } from 'src/lib/meta' -import { IMeta, IImage } from 'src/types' +import { IMeta, Person } from 'src/types' import { createApiUrl, requestGet, IResponseItems } from 'src/next-lib' import { NextPage, NextPageContext } from 'next' import { CardGrid } from 'src/components/CardGrid' import { Card } from 'src/components/Card' +import { Block } from 'src/components/Block' -interface IPerson { - url: string - name: string - position: string - picture: IImage +type CardData = { + title: string + caption?: string + imageSrc: string + imageAlt: string + imageSrcSet: string + href: string } type Props = { title: string image: string meta: IMeta - data: IPerson[] + data: CardData[] } const Page: NextPage = props => { @@ -52,24 +55,26 @@ const Page: NextPage = props => { - {props.data.map((item, index) => ( + {props.data.map(item => ( -

- {item.name[0]} {item.name[1]} {item.name[2]} -

- {item.position} + + {item.title} + + {item.caption} + +
))}
@@ -78,14 +83,24 @@ const Page: NextPage = props => { } export const unstable_getStaticProps = async (ctx: NextPageContext) => { - // const pageUrl = ctx.req.url - - const res = await requestGet>(createApiUrl('/api/persons'), null) - const data = res?.items || [] + const res = await requestGet>(createApiUrl('/api/persons'), null) const title = 'Преподаватели Шлиссельбургской ДХШ' const imageFile = 'Images/HudozkaCollective2017.jpg' const resImage = await requestGet(createApiUrl(`/api/image?file=${imageFile}`), null) const image = get(resImage, 'artifacts.large', null) + const data: CardData[] = (res?.items || []) + .map(x => { + const title = `${x.name[0]} ${x.name[1]} ${x.name[2]}` + return { + title, + caption: x.position, + href: x.url, + imageSrc: x.picture.src, + imageAlt: title, + imageSrcSet: '', + } + }) + return { props: { From e7de465e2c4e4064de5cab03cb33aa41aa035b3e Mon Sep 17 00:00:00 2001 From: Roman Timashev Date: Mon, 24 Feb 2020 20:09:56 +0300 Subject: [PATCH 09/16] wildcard page meta updated --- pages/[...slug].tsx | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/pages/[...slug].tsx b/pages/[...slug].tsx index 38a64cf1..188eabd8 100644 --- a/pages/[...slug].tsx +++ b/pages/[...slug].tsx @@ -3,8 +3,8 @@ import { App } from 'src/components/App' import { tail } from 'lodash' import { Page } from 'src/components/Page' import { Meta } from 'src/components/Meta' -import { meta } from 'src/lib/meta' -import { createApiUrl, requestGet, wrapInitialProps, IResponseItems } from 'src/next-lib' +import { MetaBuilder } from 'src/lib/meta' +import { createApiUrl, requestGet, IResponseItems } from 'src/next-lib' import { NextPage } from 'next' import { IBreadcumbsPart, IMeta, IPage } from 'src/types' @@ -15,7 +15,6 @@ function array(value: T | T[]) { } type Props = { - page: any title: string content: string breadcrumb: IBreadcumbsPart[] @@ -32,7 +31,7 @@ const Index: NextPage = props => ( > {props.title} - {/* */} + { if (!page) { throw new Error(`Not found: ${slug}`) } - const image = page.preview ? page.preview.artifacts.fb : {} + const description = page.description ?? '' const breadcrumbSize = page.breadcrumb?.length ?? 0 const breadcrumb = breadcrumbSize < 2 ? null : page.breadcrumb + const meta = (new MetaBuilder()) + .setImage(page.preview) + .setTitle(page.title) + .setDescription(description) + .build() + return { props: { - page, content: page.data, title: page.title, - meta: meta({ - title: page.title, - image: image.src, - imageWidth: image.width, - imageHeight: image.height, - }), + meta, breadcrumb, } } } export const unstable_getStaticPaths = async () => { - console.log('call all unstable_getStaticPaths') - const urls = await requestGet>(createApiUrl(`/api/pages/urls`), null) if (!urls) { return null From 6aaeb7e048af92479fffc13e34082bc10ff1b9b6 Mon Sep 17 00:00:00 2001 From: Roman Timashev Date: Mon, 24 Feb 2020 23:40:50 +0300 Subject: [PATCH 10/16] wildcard page description default to null --- pages/[...slug].tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pages/[...slug].tsx b/pages/[...slug].tsx index 188eabd8..50e8b060 100644 --- a/pages/[...slug].tsx +++ b/pages/[...slug].tsx @@ -53,7 +53,7 @@ export const unstable_getStaticProps = async (ctx: any) => { if (!page) { throw new Error(`Not found: ${slug}`) } - const description = page.description ?? '' + const description = page.description ?? null const breadcrumbSize = page.breadcrumb?.length ?? 0 const breadcrumb = breadcrumbSize < 2 ? null : page.breadcrumb From f8e2e9fc32d63b485f16723a2ecdecbbfc42e1c0 Mon Sep 17 00:00:00 2001 From: Roman Timashev Date: Mon, 24 Feb 2020 23:41:28 +0300 Subject: [PATCH 11/16] meta imports optimisation --- src/lib/meta.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/meta.ts b/src/lib/meta.ts index db518e94..078209fd 100644 --- a/src/lib/meta.ts +++ b/src/lib/meta.ts @@ -1,4 +1,4 @@ -import { IMeta, IImage, ImageDefinition } from 'src/types' +import { IMeta, ImageDefinition } from 'src/types' export function meta(props: Partial): IMeta { const path = props.url || '/' From 66b26bc778397f00a4d07e1dd5107e6a7a2b1ae3 Mon Sep 17 00:00:00 2001 From: Roman Timashev Date: Mon, 24 Feb 2020 23:41:43 +0300 Subject: [PATCH 12/16] Page type updated --- src/types.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/types.ts b/src/types.ts index 1875539f..990c421f 100644 --- a/src/types.ts +++ b/src/types.ts @@ -13,7 +13,8 @@ export interface IPage { title: string url: string data: string - preview: any //IImage + preview: ImageDefinition + description: string tokens: any[] breadcrumb?: IBreadcumbsPart[] } From 21a89c7d66547d0db8a0918b6a67eb53e6266d09 Mon Sep 17 00:00:00 2001 From: Roman Timashev Date: Tue, 25 Feb 2020 00:14:00 +0300 Subject: [PATCH 13/16] article page input data refactoring --- pages/article/[slug].tsx | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/pages/article/[slug].tsx b/pages/article/[slug].tsx index 93f8ee02..e670d2df 100644 --- a/pages/article/[slug].tsx +++ b/pages/article/[slug].tsx @@ -4,12 +4,13 @@ import { App } from 'src/components/App' import { Article } from 'src/components/Article' import { Html } from 'src/components/Html' import { Meta } from 'src/components/Meta' -import { meta } from 'src/lib/meta' +import { meta, MetaBuilder } from 'src/lib/meta' import { createApiUrl, requestGet, IResponseItems } from 'src/next-lib' import { NextPage } from 'next' import { IArticle } from 'src/types' type Props = { + title: string meta: any data: IArticle } @@ -25,11 +26,11 @@ const Page: NextPage = props => { showAuthor={true} > - {props.data.title} + {props.title}
= props => { } export const unstable_getStaticProps = async (ctx: any) => { - const pageUrl = '/' const id = ctx.params.slug - const data = await requestGet(createApiUrl(`/api/articles/${id}`), null) + const data = await requestGet(createApiUrl(`/api/articles/${id}`), null) if (!data) { return null } - const image = get(data, 'preview.artifacts.fb', {}) + const meta = (new MetaBuilder()) + .setImage(data.preview) + .setTitle(data.title) + .build() return { props: { + title: data.title, data, - meta: meta({ - title: data.title, - image: image.src, - imageWidth: image.width, - imageHeight: image.height, - }), + meta, } } } From 9e5ec860e8ddc58d0d68fcaaf78542553814b025 Mon Sep 17 00:00:00 2001 From: Roman Timashev Date: Tue, 25 Feb 2020 01:19:36 +0300 Subject: [PATCH 14/16] article cleanup + small changes --- pages/article/[slug].tsx | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/pages/article/[slug].tsx b/pages/article/[slug].tsx index e670d2df..8e8ded5a 100644 --- a/pages/article/[slug].tsx +++ b/pages/article/[slug].tsx @@ -1,17 +1,16 @@ import Head from 'next/head' -import { get } from 'lodash' import { App } from 'src/components/App' import { Article } from 'src/components/Article' import { Html } from 'src/components/Html' import { Meta } from 'src/components/Meta' -import { meta, MetaBuilder } from 'src/lib/meta' +import { MetaBuilder } from 'src/lib/meta' import { createApiUrl, requestGet, IResponseItems } from 'src/next-lib' import { NextPage } from 'next' import { IArticle } from 'src/types' type Props = { title: string - meta: any + meta: IMeta data: IArticle } @@ -65,8 +64,6 @@ export const unstable_getStaticProps = async (ctx: any) => { } export const unstable_getStaticPaths = async () => { - console.log('call article unstable_getStaticPaths') - const urls = await requestGet>(createApiUrl(`/api/articles/urls`), null) if (!urls) { return null From 488ea6ec06f4ace9600c5dd53c09cf9f35ad48d4 Mon Sep 17 00:00:00 2001 From: Roman Timashev Date: Tue, 25 Feb 2020 02:01:57 +0300 Subject: [PATCH 15/16] article related pages meta update and other improvements --- pages/article/[slug].tsx | 2 +- pages/articles/[slug].tsx | 26 +++++++++++--------------- pages/index.tsx | 17 ++++++++--------- src/components/ArticleGrid/index.tsx | 6 ++++-- src/lib/meta.ts | 24 ++++++++++++++++-------- src/types.ts | 2 +- 6 files changed, 41 insertions(+), 36 deletions(-) diff --git a/pages/article/[slug].tsx b/pages/article/[slug].tsx index 8e8ded5a..f8deabe6 100644 --- a/pages/article/[slug].tsx +++ b/pages/article/[slug].tsx @@ -6,7 +6,7 @@ import { Meta } from 'src/components/Meta' import { MetaBuilder } from 'src/lib/meta' import { createApiUrl, requestGet, IResponseItems } from 'src/next-lib' import { NextPage } from 'next' -import { IArticle } from 'src/types' +import { IArticle, IMeta } from 'src/types' type Props = { title: string diff --git a/pages/articles/[slug].tsx b/pages/articles/[slug].tsx index cc8dce36..4d31efc9 100644 --- a/pages/articles/[slug].tsx +++ b/pages/articles/[slug].tsx @@ -2,17 +2,18 @@ import Head from 'next/head' import { range } from 'lodash' import { App } from 'src/components/App' import { ArticleGrid } from 'src/components/ArticleGrid' -import { meta } from 'src/lib/meta' +import { MetaBuilder } from 'src/lib/meta' import { Meta } from 'src/components/Meta' import { createApiUrl, requestGet, IResponseItems } from 'src/next-lib' import { NextPage } from 'next' +import { IArticle } from 'src/types' const pageSize = Number(process.env.APP_ARTICLES_PAGE_SIZE) type Props = { meta: any title: string - data: any //(list of article) + data: IArticle[] nextPage: number prevPage: number } @@ -42,20 +43,19 @@ const Page: NextPage = props => { } export const unstable_getStaticProps = async (ctx: any) => { - // const page = parseInt(ctx.query.page) const page = parseInt(ctx.params.slug) - const res = await requestGet>( - createApiUrl(`/api/articles?page=${page}&pageSize=${pageSize}`), - null - ) + const res = await requestGet>(createApiUrl(`/api/articles?page=${page}&pageSize=${pageSize}`), null) if (!res) { return null } - const data = res.items || [] - const nextPage = (res as any).nextPage - const prevPage = (res as any).prevPage + const data = (res.items || []) + const nextPage = res.nextPage + const prevPage = res.prevPage const title = 'События' + const meta = (new MetaBuilder()) + .setTitle(title) + .build() return { props: { @@ -63,16 +63,12 @@ export const unstable_getStaticProps = async (ctx: any) => { nextPage, prevPage, title, - meta: meta({ - title, - }) + meta, } } } export const unstable_getStaticPaths = async () => { - console.log('call articles unstable_getStaticPaths') - const urls = await requestGet>(createApiUrl(`/api/articles/urls`), null) if (!urls) { return null diff --git a/pages/index.tsx b/pages/index.tsx index c0bf8e06..64e2f261 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -3,7 +3,7 @@ import { App } from 'src/components/App' import { Meta } from 'src/components/Meta' import { ArticleGrid } from 'src/components/ArticleGrid' import { HudozkaTitle } from 'src/components/HudozkaTitle' -import { meta } from 'src/lib/meta' +import { meta, MetaBuilder } from 'src/lib/meta' import { createApiUrl, requestGet, IResponseItems } from 'src/next-lib' import { NextPage } from 'next' import { IArticle } from 'src/types' @@ -45,13 +45,15 @@ export const unstable_getStaticProps = async () => { const pageSize = process.env.APP_ARTICLES_PAGE_SIZE const res = await requestGet>(createApiUrl(`/api/articles?page=${page}&pageSize=${pageSize}`), null) const articles = (res.items || []) - .map(x => ({ - ...x, - featured: ['2019-kotiki', 'veloparking'].includes(x.id) - })) const nextPage = res.nextPage const prevPage = res.prevPage const title = 'Шлиссельбургская ДХШ' + const meta = (new MetaBuilder()) + .setTitle(title) + .setData({ + url: '/', + }) + .build() return { props: { @@ -59,10 +61,7 @@ export const unstable_getStaticProps = async () => { nextPage, prevPage, title, - meta: meta({ - title, - url: '/', - }) + meta, } } } diff --git a/src/components/ArticleGrid/index.tsx b/src/components/ArticleGrid/index.tsx index 780ad842..2758ff68 100644 --- a/src/components/ArticleGrid/index.tsx +++ b/src/components/ArticleGrid/index.tsx @@ -29,7 +29,9 @@ export const ArticleGrid: React.FC = ({ articles, nextPage, pr {articles.map((article, i) => { const gridColumn = article.featured ? 'span 2' : 'auto' const layout: CardLayout = article.featured ? 'featured' : 'simple' - const srcSet = article.preview.set.map(({ url, density }) => `${url} ${density}x`).join(' ') + const preview = article.preview.artifacts.medium + const src = preview.src + const srcSet = preview.set.map(({ url, density }) => `${url} ${density}x`).join(' ') const content = !article.featured ? ( @@ -52,7 +54,7 @@ export const ArticleGrid: React.FC = ({ articles, nextPage, pr href={article.url} img={{ alt: article.title, - src: article.preview.src, + src, srcSet, }} layout={layout} diff --git a/src/lib/meta.ts b/src/lib/meta.ts index 078209fd..338f5a16 100644 --- a/src/lib/meta.ts +++ b/src/lib/meta.ts @@ -51,18 +51,26 @@ export class MetaBuilder { } build() { - const artifact = this.image.artifacts.fb - const image = { - image: artifact.src, - imageWidth: artifact.width, - imageHeight: artifact.height, - } + const image = this.buildImage() return meta({ ...this.data, ...image, - title: this.title, - description: this.description, + title: this.title ?? undefined, + description: this.description ?? undefined, }) } + + private buildImage() { + if (!this.image) { + return null + } + + const artifact = this.image.artifacts.fb + return { + image: artifact.src, + imageWidth: artifact.width, + imageHeight: artifact.height, + } + } } diff --git a/src/types.ts b/src/types.ts index 990c421f..baf86823 100644 --- a/src/types.ts +++ b/src/types.ts @@ -66,7 +66,7 @@ export interface IArticle { featured: boolean url: string title: string - preview: IImage + preview: ImageDefinition } export type Person = { From b62a0588aea36767b58f062dd08fe781ff7562c5 Mon Sep 17 00:00:00 2001 From: Roman Timashev Date: Tue, 25 Feb 2020 02:28:31 +0300 Subject: [PATCH 16/16] wildcard page meta stability improved --- pages/[...slug].tsx | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/pages/[...slug].tsx b/pages/[...slug].tsx index 50e8b060..e632e75f 100644 --- a/pages/[...slug].tsx +++ b/pages/[...slug].tsx @@ -18,7 +18,7 @@ type Props = { title: string content: string breadcrumb: IBreadcumbsPart[] - meta: IMeta + meta?: IMeta } const Index: NextPage = props => ( @@ -31,7 +31,9 @@ const Index: NextPage = props => ( > {props.title} - + {!props.meta ? null : ( + + )} { if (!page) { throw new Error(`Not found: ${slug}`) } - const description = page.description ?? null - const breadcrumbSize = page.breadcrumb?.length ?? 0 + const description = page.description ?? undefined + const breadcrumbSize = page?.breadcrumb?.length ?? 0 const breadcrumb = breadcrumbSize < 2 ? null : page.breadcrumb - const meta = (new MetaBuilder()) .setImage(page.preview) .setTitle(page.title)