dynamic page update

This commit is contained in:
tmshv committed 2020-02-15 05:28:16 +03:00
1 parent 2ca613e916
commit 411f7eee62
1 file changed
+48 -21
+48 -21
View File
@@ -1,13 +1,12 @@
import React from 'react'
import Head from 'next/head' import Head from 'next/head'
import { App } from 'src/components/App'
import { App } from '../src/components/App' import { tail } from 'lodash'
import { Page } from '../src/components/Page' import { Page } from 'src/components/Page'
import menuModel from '../src/models/menu' import menuModel from 'src/models/menu'
import { buildMenu } from '../src/lib/menu' import { buildMenu } from 'src/lib/menu'
import { Meta } from '../src/components/Meta' import { Meta } from 'src/components/Meta'
import { meta } from '../src/lib/meta' import { meta } from 'src/lib/meta'
import { createApiUrl, requestGet, wrapInitialProps } from '../src/next-lib' import { createApiUrl, requestGet, wrapInitialProps, IResponseItems } from 'src/next-lib'
function array<T>(value: T | T[]) { function array<T>(value: T | T[]) {
return Array.isArray(value) return Array.isArray(value)
@@ -24,7 +23,7 @@ const Index = props => (
> >
<Head> <Head>
<title>{props.title}</title> <title>{props.title}</title>
<Meta meta={props.meta} /> {/* <Meta meta={props.meta} /> */}
</Head> </Head>
<Page <Page
@@ -35,8 +34,13 @@ const Index = props => (
</App> </App>
) )
Index.getInitialProps = wrapInitialProps(async (ctx) => { export const unstable_getStaticProps = async (ctx: any) => {
const slug = '/' + array(ctx.query.slug).join('/') 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) const page = await requestGet(createApiUrl(`/api/page?page=${slug}`), null)
if (!page) { if (!page) {
throw new Error(`Not found: ${slug}`) throw new Error(`Not found: ${slug}`)
@@ -44,16 +48,39 @@ Index.getInitialProps = wrapInitialProps(async (ctx) => {
const image = page.preview ? page.preview.artifacts.fb : {} const image = page.preview ? page.preview.artifacts.fb : {}
return { return {
content: page.data, props: {
pageUrl: slug, content: page.data,
title: page.title, pageUrl: slug,
meta: meta({
title: page.title, title: page.title,
image: image.src, meta: meta({
imageWidth: image.width, title: page.title,
imageHeight: image.height, image: image.src,
}), imageWidth: image.width,
imageHeight: image.height,
}),
}
} }
}) }
export const unstable_getStaticPaths = async () => {
console.log('call all unstable_getStaticPaths')
const urls = await requestGet<IResponseItems<string>>(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 export default Index