getInitialProps request stability improvements
This commit is contained in:
1 parent
95c8369df2
commit
7ab68f5a8e
13 files changed
+65
-55
No files matched your search
@@ -36,7 +36,6 @@
|
|||||||
"mongodb": "^2.1.14",
|
"mongodb": "^2.1.14",
|
||||||
"mz": "^2.4.0",
|
"mz": "^2.4.0",
|
||||||
"next": "^9.0.2",
|
"next": "^9.0.2",
|
||||||
"next-absolute-url": "^1.1.1",
|
|
||||||
"react": "^16.8.6",
|
"react": "^16.8.6",
|
||||||
"react-dom": "^16.8.6",
|
"react-dom": "^16.8.6",
|
||||||
"react-redux": "^4.4.8",
|
"react-redux": "^4.4.8",
|
||||||
|
|||||||
@@ -1,12 +1,11 @@
|
|||||||
import React from 'react'
|
import React from 'react'
|
||||||
import axios from 'axios'
|
|
||||||
import Head from 'next/head'
|
import Head from 'next/head'
|
||||||
import { App } from '../../src/components/App'
|
import { App } from '../../src/components/App'
|
||||||
import Article from '../../src/components/Article'
|
import Article from '../../src/components/Article'
|
||||||
import Html from '../../src/components/Html'
|
import Html from '../../src/components/Html'
|
||||||
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 { createApiUrl } from '../../src/next-lib'
|
import { createApiUrl, requestGet } from '../../src/next-lib'
|
||||||
|
|
||||||
const Page = (props) => (
|
const Page = (props) => (
|
||||||
<App
|
<App
|
||||||
@@ -34,8 +33,7 @@ const Page = (props) => (
|
|||||||
Page.getInitialProps = async (ctx) => {
|
Page.getInitialProps = async (ctx) => {
|
||||||
const pageUrl = '/gallery'
|
const pageUrl = '/gallery'
|
||||||
const id = ctx.query.album
|
const id = ctx.query.album
|
||||||
const res = await axios.get(createApiUrl(ctx.req, `/api/albums/${id}`))
|
const album = await requestGet(createApiUrl(ctx.req, `/api/albums/${id}`), {})
|
||||||
const album = res.data
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
album,
|
album,
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import React from 'react'
|
import React from 'react'
|
||||||
import axios from 'axios'
|
|
||||||
import Head from 'next/head'
|
import Head from 'next/head'
|
||||||
import { App } from '../../src/components/App'
|
import { App } from '../../src/components/App'
|
||||||
import Article from '../../src/components/Article'
|
import Article from '../../src/components/Article'
|
||||||
@@ -8,7 +7,7 @@ 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 } from '../../src/next-lib'
|
import { createApiUrl, requestGet } from '../../src/next-lib'
|
||||||
|
|
||||||
const Page = (props) => (
|
const Page = (props) => (
|
||||||
<App
|
<App
|
||||||
@@ -38,8 +37,7 @@ const Page = (props) => (
|
|||||||
Page.getInitialProps = async (ctx) => {
|
Page.getInitialProps = async (ctx) => {
|
||||||
const pageUrl = '/'
|
const pageUrl = '/'
|
||||||
const id = ctx.query.article
|
const id = ctx.query.article
|
||||||
const res = await axios.get(createApiUrl(ctx.req, `/api/articles/${id}`))
|
const article = await requestGet(createApiUrl(ctx.req, `/api/articles/${id}`), {})
|
||||||
const article = res.data
|
|
||||||
const image = article.preview.artifacts.fb
|
const image = article.preview.artifacts.fb
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import React from 'react'
|
import React from 'react'
|
||||||
import axios from 'axios'
|
|
||||||
import Head from 'next/head'
|
import Head from 'next/head'
|
||||||
import { App } from '../../src/components/App'
|
import { App } from '../../src/components/App'
|
||||||
import { ArticleCardList } from '../../src/components/ArticleCardList'
|
import { ArticleCardList } from '../../src/components/ArticleCardList'
|
||||||
@@ -7,7 +6,7 @@ import menuModel from '../../src/models/menu'
|
|||||||
import { buildMenu } from '../../src/lib/menu'
|
import { buildMenu } from '../../src/lib/menu'
|
||||||
import { meta } from '../../src/lib/meta'
|
import { meta } from '../../src/lib/meta'
|
||||||
import { Meta } from '../../src/components/Meta'
|
import { Meta } from '../../src/components/Meta'
|
||||||
import { createApiUrl } from '../../src/next-lib'
|
import { createApiUrl, requestGet } from '../../src/next-lib'
|
||||||
|
|
||||||
const Page = (props) => (
|
const Page = (props) => (
|
||||||
<App
|
<App
|
||||||
@@ -33,10 +32,10 @@ Page.getInitialProps = async (ctx) => {
|
|||||||
const pageUrl = '/'
|
const pageUrl = '/'
|
||||||
const page = parseInt(ctx.query.page)
|
const page = parseInt(ctx.query.page)
|
||||||
const pageSize = parseInt(ctx.query.pageSize) || 15
|
const pageSize = parseInt(ctx.query.pageSize) || 15
|
||||||
const res = await axios.get(createApiUrl(ctx.req, `/api/articles?page=${page}&pageSize=${pageSize}`))
|
const res = await requestGet(createApiUrl(ctx.req, `/api/articles?page=${page}&pageSize=${pageSize}`), {})
|
||||||
const articles = res.data.items
|
const articles = res.items
|
||||||
const nextPage = res.data.nextPage
|
const nextPage = res.nextPage
|
||||||
const prevPage = res.data.prevPage
|
const prevPage = res.prevPage
|
||||||
const title = 'События'
|
const title = 'События'
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -51,4 +50,4 @@ Page.getInitialProps = async (ctx) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Page
|
export default Page
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
import React from 'react'
|
import React from 'react'
|
||||||
import axios from 'axios'
|
|
||||||
import Head from 'next/head'
|
import Head from 'next/head'
|
||||||
import { App } from '../src/components/App'
|
import { App } from '../src/components/App'
|
||||||
import { Meta } from '../src/components/Meta'
|
import { Meta } from '../src/components/Meta'
|
||||||
@@ -9,7 +8,7 @@ import { CardList } from '../src/components/CardList'
|
|||||||
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/lib/meta'
|
import { meta } from '../src/lib/meta'
|
||||||
import { createApiUrl } from '../src/next-lib'
|
import { createApiUrl, requestGet } from '../src/next-lib'
|
||||||
|
|
||||||
const Page = (props) => (
|
const Page = (props) => (
|
||||||
<App
|
<App
|
||||||
@@ -51,15 +50,16 @@ const Page = (props) => (
|
|||||||
Page.getInitialProps = async (ctx) => {
|
Page.getInitialProps = async (ctx) => {
|
||||||
const pageUrl = ctx.req.url
|
const pageUrl = ctx.req.url
|
||||||
|
|
||||||
const res = await axios.get(createApiUrl(ctx.req, '/api/persons'))
|
const res = await requestGet(createApiUrl(ctx.req, '/api/persons'), {})
|
||||||
const persons = res.data.items
|
const persons = res.items || []
|
||||||
const title = 'Преподаватели Шлиссельбургской ДХШ'
|
const title = 'Преподаватели Шлиссельбургской ДХШ'
|
||||||
const imageFile = 'Images/HudozkaCollective2017.jpg'
|
const imageFile = 'Images/HudozkaCollective2017.jpg'
|
||||||
const resImage = await axios.get(createApiUrl(ctx.req, `/api/image?file=${imageFile}`))
|
const resImage = await requestGet(createApiUrl(ctx.req, `/api/image?file=${imageFile}`), null)
|
||||||
|
const image = resImage ? resImage.data.artifacts.large : null
|
||||||
|
|
||||||
return {
|
return {
|
||||||
persons,
|
persons,
|
||||||
image: resImage.data.artifacts.large,
|
image,
|
||||||
pageUrl,
|
pageUrl,
|
||||||
title,
|
title,
|
||||||
meta: meta({
|
meta: meta({
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import React from 'react'
|
import React from 'react'
|
||||||
import axios from 'axios'
|
|
||||||
import Head from 'next/head'
|
import Head from 'next/head'
|
||||||
import { App } from '../../src/components/App'
|
import { App } from '../../src/components/App'
|
||||||
import Document from '../../src/components/Document'
|
import Document from '../../src/components/Document'
|
||||||
@@ -8,7 +7,7 @@ import menuModel from '../../src/models/menu'
|
|||||||
import { buildMenu } from '../../src/lib/menu'
|
import { buildMenu } from '../../src/lib/menu'
|
||||||
import { meta } from '../../src/lib/meta'
|
import { meta } from '../../src/lib/meta'
|
||||||
import { Meta } from '../../src/components/Meta'
|
import { Meta } from '../../src/components/Meta'
|
||||||
import { createApiUrl } from '../../src/next-lib'
|
import { createApiUrl, requestGet } from '../../src/next-lib'
|
||||||
|
|
||||||
const Page = (props) => (
|
const Page = (props) => (
|
||||||
<App
|
<App
|
||||||
@@ -30,8 +29,7 @@ const Page = (props) => (
|
|||||||
Page.getInitialProps = async (ctx) => {
|
Page.getInitialProps = async (ctx) => {
|
||||||
const pageUrl = '/documents'
|
const pageUrl = '/documents'
|
||||||
const id = ctx.query.document
|
const id = ctx.query.document
|
||||||
const res = await axios.get(createApiUrl(ctx.req, `/api/files/${id}`))
|
const file = await requestGet(createApiUrl(ctx.req, `/api/files/${id}`), {})
|
||||||
const file = res.data
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
file,
|
file,
|
||||||
|
|||||||
+3
-4
@@ -1,5 +1,4 @@
|
|||||||
import React from 'react'
|
import React from 'react'
|
||||||
import axios from 'axios'
|
|
||||||
import Head from 'next/head'
|
import Head from 'next/head'
|
||||||
import { App } from '../src/components/App'
|
import { App } from '../src/components/App'
|
||||||
import { Meta } from '../src/components/Meta'
|
import { Meta } from '../src/components/Meta'
|
||||||
@@ -9,7 +8,7 @@ import { meta } from '../src/lib/meta'
|
|||||||
|
|
||||||
import DocumentList from '../src/components/DocumentList'
|
import DocumentList from '../src/components/DocumentList'
|
||||||
import { unique } from '../src/utils/common'
|
import { unique } from '../src/utils/common'
|
||||||
import { createApiUrl } from '../src/next-lib'
|
import { createApiUrl, requestGet } from '../src/next-lib'
|
||||||
|
|
||||||
function getSorted(documents) {
|
function getSorted(documents) {
|
||||||
const compose = (...fns) => value => fns
|
const compose = (...fns) => value => fns
|
||||||
@@ -112,8 +111,8 @@ const Page = (props) => (
|
|||||||
|
|
||||||
Page.getInitialProps = async (ctx) => {
|
Page.getInitialProps = async (ctx) => {
|
||||||
const pageUrl = '/documents'
|
const pageUrl = '/documents'
|
||||||
const res = await axios.get(createApiUrl(ctx.req, '/api/files'))
|
const res = await requestGet(createApiUrl(ctx.req, '/api/files'), {})
|
||||||
const files = res.data.items
|
const files = res.items || []
|
||||||
const title = 'Документы'
|
const title = 'Документы'
|
||||||
const collections = getSorted(files)
|
const collections = getSorted(files)
|
||||||
|
|
||||||
|
|||||||
+3
-4
@@ -1,5 +1,4 @@
|
|||||||
import React from 'react'
|
import React from 'react'
|
||||||
import axios from 'axios'
|
|
||||||
import Head from 'next/head'
|
import Head from 'next/head'
|
||||||
import { App } from '../src/components/App'
|
import { App } from '../src/components/App'
|
||||||
// import Album from '../src/components/Album'
|
// import Album from '../src/components/Album'
|
||||||
@@ -9,7 +8,7 @@ import menuModel from '../src/models/menu'
|
|||||||
import { buildMenu } from '../src/lib/menu'
|
import { buildMenu } from '../src/lib/menu'
|
||||||
import { splitBy } from '../src/lib/array'
|
import { splitBy } from '../src/lib/array'
|
||||||
import { meta } from '../src/lib/meta'
|
import { meta } from '../src/lib/meta'
|
||||||
import { createApiUrl } from '../src/next-lib'
|
import { createApiUrl, requestGet } from '../src/next-lib'
|
||||||
|
|
||||||
const albumsByYear = splitBy(album => new Date(album.date).getFullYear())
|
const albumsByYear = splitBy(album => new Date(album.date).getFullYear())
|
||||||
|
|
||||||
@@ -84,8 +83,8 @@ const Page = (props) => (
|
|||||||
|
|
||||||
Page.getInitialProps = async (ctx) => {
|
Page.getInitialProps = async (ctx) => {
|
||||||
const pageUrl = ctx.req.url
|
const pageUrl = ctx.req.url
|
||||||
const res = await axios.get(createApiUrl(ctx.req, `/api/albums`))
|
const res = await requestGet(createApiUrl(ctx.req, `/api/albums`), {})
|
||||||
const items = res.data.items
|
const items = res.items || []
|
||||||
const albumCollections = albumsByYear(items)
|
const albumCollections = albumsByYear(items)
|
||||||
const title = 'Галерея'
|
const title = 'Галерея'
|
||||||
|
|
||||||
|
|||||||
+5
-6
@@ -1,5 +1,4 @@
|
|||||||
import React from 'react'
|
import React from 'react'
|
||||||
import axios from 'axios'
|
|
||||||
import Head from 'next/head'
|
import Head from 'next/head'
|
||||||
import { App } from '../src/components/App'
|
import { App } from '../src/components/App'
|
||||||
import { Meta } from '../src/components/Meta'
|
import { Meta } from '../src/components/Meta'
|
||||||
@@ -8,7 +7,7 @@ import { HudozkaTitle } from '../src/components/HudozkaTitle'
|
|||||||
import menuModel from '../src/models/menu'
|
import menuModel from '../src/models/menu'
|
||||||
import { meta } from '../src/lib/meta'
|
import { meta } from '../src/lib/meta'
|
||||||
import { buildMenu } from '../src/lib/menu'
|
import { buildMenu } from '../src/lib/menu'
|
||||||
import { createApiUrl } from '../src/next-lib'
|
import { createApiUrl, requestGet } from '../src/next-lib'
|
||||||
|
|
||||||
const Page = (props) => (
|
const Page = (props) => (
|
||||||
<App
|
<App
|
||||||
@@ -37,10 +36,10 @@ Page.getInitialProps = async (ctx) => {
|
|||||||
const pageUrl = '/'
|
const pageUrl = '/'
|
||||||
const page = 1
|
const page = 1
|
||||||
const pageSize = ctx.query.pageSize || 15
|
const pageSize = ctx.query.pageSize || 15
|
||||||
const res = await axios.get(createApiUrl(ctx.req, `/api/articles?page=${page}&pageSize=${pageSize}`))
|
const res = await requestGet(createApiUrl(ctx.req, `/api/articles?page=${page}&pageSize=${pageSize}`), {})
|
||||||
const articles = res.data.items
|
const articles = res.items
|
||||||
const nextPage = res.data.nextPage
|
const nextPage = res.nextPage
|
||||||
const prevPage = res.data.prevPage
|
const prevPage = res.prevPage
|
||||||
const title = 'Шлиссельбургская ДХШ'
|
const title = 'Шлиссельбургская ДХШ'
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
+4
-6
@@ -1,5 +1,4 @@
|
|||||||
import React from 'react'
|
import React from 'react'
|
||||||
import axios from 'axios'
|
|
||||||
import Head from 'next/head'
|
import Head from 'next/head'
|
||||||
import { App } from '../src/components/App'
|
import { App } from '../src/components/App'
|
||||||
import Page from '../src/components/Page'
|
import Page from '../src/components/Page'
|
||||||
@@ -7,14 +6,14 @@ 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 } from '../src/next-lib'
|
import { createApiUrl, requestGet } from '../src/next-lib'
|
||||||
|
|
||||||
const Index = (props) => (
|
const Index = (props) => (
|
||||||
<App
|
<App
|
||||||
menu={buildMenu(props.pageUrl, menuModel)}
|
menu={buildMenu(props.pageUrl, menuModel)}
|
||||||
showAuthor={true}
|
showAuthor={true}
|
||||||
menuPadding={true}
|
menuPadding={true}
|
||||||
>
|
>
|
||||||
<Head>
|
<Head>
|
||||||
<title>{props.title}</title>
|
<title>{props.title}</title>
|
||||||
<Meta meta={props.meta} />
|
<Meta meta={props.meta} />
|
||||||
@@ -33,9 +32,8 @@ const Index = (props) => (
|
|||||||
Index.getInitialProps = async (ctx) => {
|
Index.getInitialProps = async (ctx) => {
|
||||||
const pageUrl = ctx.req.url
|
const pageUrl = ctx.req.url
|
||||||
// // const pageUrl = '/neighbors/2018'
|
// // const pageUrl = '/neighbors/2018'
|
||||||
const res = await axios.get(createApiUrl(ctx.req, `/api/page?page=${pageUrl}`))
|
const page = await requestGet(createApiUrl(ctx.req, `/api/page?page=${pageUrl}`), {})
|
||||||
const page = res.data
|
const image = page.preview ? page.preview.artifacts.fb : {}
|
||||||
const image = page.preview.artifacts.fb
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
content: page.data,
|
content: page.data,
|
||||||
|
|||||||
+2
-4
@@ -1,12 +1,11 @@
|
|||||||
import React from 'react'
|
import React from 'react'
|
||||||
import axios from 'axios'
|
|
||||||
import Head from 'next/head'
|
import Head from 'next/head'
|
||||||
import { App } from '../src/components/App'
|
import { App } from '../src/components/App'
|
||||||
import Schedule from '../src/components/Schedule'
|
import Schedule from '../src/components/Schedule'
|
||||||
import Share from '../src/components/Share'
|
import Share from '../src/components/Share'
|
||||||
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 { createApiUrl } from '../src/next-lib'
|
import { createApiUrl, requestGet } from '../src/next-lib'
|
||||||
|
|
||||||
const Page = (props) => (
|
const Page = (props) => (
|
||||||
<App
|
<App
|
||||||
@@ -32,8 +31,7 @@ Page.getInitialProps = async (ctx) => {
|
|||||||
const pageUrl = ctx.req.url
|
const pageUrl = ctx.req.url
|
||||||
const period = '2016-2017'
|
const period = '2016-2017'
|
||||||
const semester = 'spring'
|
const semester = 'spring'
|
||||||
const res = await axios.get(createApiUrl(ctx.req, `/api/schedule?period=${period}&semester=${semester}`))
|
const schedule = await requestGet(createApiUrl(ctx.req, `/api/schedule?period=${period}&semester=${semester}`), {})
|
||||||
const schedule = res.data
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
schedule,
|
schedule,
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import { Meta } from '../../src/components/Meta'
|
|||||||
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/lib/meta'
|
import { meta } from '../../src/lib/meta'
|
||||||
import { createApiUrl } from '../../src/next-lib'
|
import { createApiUrl, requestGet } from '../../src/next-lib'
|
||||||
|
|
||||||
const Index = (props) => (
|
const Index = (props) => (
|
||||||
<App
|
<App
|
||||||
@@ -30,11 +30,10 @@ const Index = (props) => (
|
|||||||
Index.getInitialProps = async (ctx) => {
|
Index.getInitialProps = async (ctx) => {
|
||||||
const pageUrl = '/collective'
|
const pageUrl = '/collective'
|
||||||
const id = ctx.query.person
|
const id = ctx.query.person
|
||||||
const res = await axios.get(createApiUrl(ctx.req, `/api/persons/${id}`))
|
const person = await requestGet(createApiUrl(ctx.req, `/api/persons/${id}`), {})
|
||||||
const person = res.data
|
|
||||||
const name = person.name || []
|
const name = person.name || []
|
||||||
const title = name.join(' ')
|
const title = name.join(' ')
|
||||||
const image = person.preview.artifacts.fb
|
const image = person.preview ? person.preview.artifacts.fb : {}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
pageUrl,
|
pageUrl,
|
||||||
|
|||||||
+27
-1
@@ -1,7 +1,33 @@
|
|||||||
import absoluteUrl from 'next-absolute-url'
|
export async function requestGet(url, defaultResponse) {
|
||||||
|
try {
|
||||||
|
const res = await axios.get(url)
|
||||||
|
|
||||||
|
return res.data
|
||||||
|
} catch (e) {
|
||||||
|
return defaultResponse
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export function createApiUrl(req, path) {
|
export function createApiUrl(req, path) {
|
||||||
const { origin } = absoluteUrl(req, 'localhost:3000')
|
const { origin } = absoluteUrl(req, 'localhost:3000')
|
||||||
|
|
||||||
return `${origin}${path}`
|
return `${origin}${path}`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function absoluteUrl(req, defaultLocalhost) {
|
||||||
|
var protocol = 'https:';
|
||||||
|
var host = req
|
||||||
|
? (req.headers['x-forwarded-host'] || req.headers['host'])
|
||||||
|
: window.location.host;
|
||||||
|
host = host || defaultLocalhost
|
||||||
|
if (host.indexOf('localhost') > -1) {
|
||||||
|
if (defaultLocalhost) host = defaultLocalhost;
|
||||||
|
protocol = 'http:';
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
protocol: protocol,
|
||||||
|
host: host,
|
||||||
|
origin: protocol + '//' + host
|
||||||
|
};
|
||||||
|
}
|
||||||
Reference in new issue
Block a user