From 411f7eee62310ae13b11f05fe28754709a9d955e Mon Sep 17 00:00:00 2001 From: Roman Timashev Date: Sat, 15 Feb 2020 05:28:16 +0300 Subject: [PATCH] dynamic page update --- pages/[...slug].tsx | 69 +++++++++++++++++++++++++++++++-------------- 1 file changed, 48 insertions(+), 21 deletions(-) diff --git a/pages/[...slug].tsx b/pages/[...slug].tsx index 92f15c88..23e3b9ba 100644 --- a/pages/[...slug].tsx +++ b/pages/[...slug].tsx @@ -1,13 +1,12 @@ -import React from 'react' import Head from 'next/head' - -import { App } from '../src/components/App' -import { Page } from '../src/components/Page' -import menuModel from '../src/models/menu' -import { buildMenu } from '../src/lib/menu' -import { Meta } from '../src/components/Meta' -import { meta } from '../src/lib/meta' -import { createApiUrl, requestGet, wrapInitialProps } from '../src/next-lib' +import { App } from 'src/components/App' +import { tail } from 'lodash' +import { Page } from 'src/components/Page' +import menuModel from 'src/models/menu' +import { buildMenu } from 'src/lib/menu' +import { Meta } from 'src/components/Meta' +import { meta } from 'src/lib/meta' +import { createApiUrl, requestGet, wrapInitialProps, IResponseItems } from 'src/next-lib' function array(value: T | T[]) { return Array.isArray(value) @@ -24,7 +23,7 @@ const Index = props => ( > {props.title} - + {/* */} ( ) -Index.getInitialProps = wrapInitialProps(async (ctx) => { - const slug = '/' + array(ctx.query.slug).join('/') +export const unstable_getStaticProps = async (ctx: any) => { + let slug = null + if (ctx.query) { + slug = '/' + array(ctx.query.slug).join('/') + } else { + slug = '/' + array(ctx.params.slug).join('/') + } const page = await requestGet(createApiUrl(`/api/page?page=${slug}`), null) if (!page) { throw new Error(`Not found: ${slug}`) @@ -44,16 +48,39 @@ Index.getInitialProps = wrapInitialProps(async (ctx) => { const image = page.preview ? page.preview.artifacts.fb : {} return { - content: page.data, - pageUrl: slug, - title: page.title, - meta: meta({ + props: { + content: page.data, + pageUrl: slug, title: page.title, - image: image.src, - imageWidth: image.width, - imageHeight: image.height, - }), + meta: meta({ + title: page.title, + image: image.src, + imageWidth: image.width, + imageHeight: image.height, + }), + } } -}) +} +export const unstable_getStaticPaths = async () => { + console.log('call all unstable_getStaticPaths') + + const urls = await requestGet>(createApiUrl(`/api/pages/urls`), null) + if (!urls) { + return null + } + + return { + paths: urls.items + .map(path => { + const slug = tail(path.split('/')) + + return { + params: { + slug, + }, + } + }), + } +} export default Index