From 82c9b88f7790bd24fef1f09f7c92a08bff727055 Mon Sep 17 00:00:00 2001 From: Roman Timashev Date: Tue, 16 Feb 2021 20:53:28 +0300 Subject: [PATCH] slug page improvements --- pages/[...slug].tsx | 18 ++++++------------ src/remote/factory.ts | 3 +++ src/remote/lib.ts | 6 ++++++ 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/pages/[...slug].tsx b/pages/[...slug].tsx index 3938a70b..985d490f 100644 --- a/pages/[...slug].tsx +++ b/pages/[...slug].tsx @@ -7,14 +7,14 @@ import { Markdown } from 'src/components/Markdown' import { Meta } from 'src/components/Meta' import { MetaBuilder } from 'src/lib/meta' import { apiGet } from '@/next-lib' -import { NextPage } from 'next' +import { GetStaticProps, NextPage } from 'next' import { IBreadcumbsPart, IMeta, ITag, FileTokenData, Token } from '@/types' import { joinTokens } from 'src/lib/tokens' import { size, ext } from 'src/lib/file' import { Html } from 'src/components/Html' import { Youtube } from '@/components/Youtube' import { createPage, createPageUrls } from '@/remote/factory' -import { asArray } from '@/remote/lib' +import { paramsToSlug } from '@/remote/lib' const File: React.SFC = props => { const fileSize = size(props.file_size) @@ -137,16 +137,10 @@ const Index: NextPage = props => ( ) -export const getStaticProps = async (ctx: any) => { - let slug = null - if (ctx.query) { - slug = '/' + asArray(ctx.query.slug).join('/') - } else { - slug = '/' + asArray(ctx.params.slug).join('/') - } - - let apiUrl = `https://hudozka.tmshv.com/pages?slug=${slug}` - const page = await apiGet(createPage)(apiUrl, null) +export const getStaticProps: GetStaticProps = async ctx => { + const slug = paramsToSlug(ctx.params.slug) + const url = `https://hudozka.tmshv.com/pages?slug=${slug}` + const page = await apiGet(createPage)(url, null) if (!page) { return { notFound: true, diff --git a/src/remote/factory.ts b/src/remote/factory.ts index cd5e7b2c..b78569f6 100644 --- a/src/remote/factory.ts +++ b/src/remote/factory.ts @@ -65,6 +65,9 @@ export function createPageTokens(components: StrapiComponent[]): Token[] { export function createPage(res: StrapiPage | StrapiPage[]): IPage { const item = asItem(res) + if (!item) { + return null + } return { title: item.title, diff --git a/src/remote/lib.ts b/src/remote/lib.ts index c3b6db0a..136e2355 100644 --- a/src/remote/lib.ts +++ b/src/remote/lib.ts @@ -9,3 +9,9 @@ export function asItem(value: T | T[]) { ? value[0] : value } + +export function paramsToSlug(parts: string | string[]): string { + const slug = asArray(parts).join('/') + + return `/${slug}` +} \ No newline at end of file