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",
|
||||
"mz": "^2.4.0",
|
||||
"next": "^9.0.2",
|
||||
"next-absolute-url": "^1.1.1",
|
||||
"react": "^16.8.6",
|
||||
"react-dom": "^16.8.6",
|
||||
"react-redux": "^4.4.8",
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
import React from 'react'
|
||||
import axios from 'axios'
|
||||
import Head from 'next/head'
|
||||
import { App } from '../../src/components/App'
|
||||
import Article from '../../src/components/Article'
|
||||
import Html from '../../src/components/Html'
|
||||
import menuModel from '../../src/models/menu'
|
||||
import { buildMenu } from '../../src/lib/menu'
|
||||
import { createApiUrl } from '../../src/next-lib'
|
||||
import { createApiUrl, requestGet } from '../../src/next-lib'
|
||||
|
||||
const Page = (props) => (
|
||||
<App
|
||||
@@ -34,8 +33,7 @@ const Page = (props) => (
|
||||
Page.getInitialProps = async (ctx) => {
|
||||
const pageUrl = '/gallery'
|
||||
const id = ctx.query.album
|
||||
const res = await axios.get(createApiUrl(ctx.req, `/api/albums/${id}`))
|
||||
const album = res.data
|
||||
const album = await requestGet(createApiUrl(ctx.req, `/api/albums/${id}`), {})
|
||||
|
||||
return {
|
||||
album,
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import React from 'react'
|
||||
import axios from 'axios'
|
||||
import Head from 'next/head'
|
||||
import { App } from '../../src/components/App'
|
||||
import Article from '../../src/components/Article'
|
||||
@@ -8,7 +7,7 @@ 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 } from '../../src/next-lib'
|
||||
import { createApiUrl, requestGet } from '../../src/next-lib'
|
||||
|
||||
const Page = (props) => (
|
||||
<App
|
||||
@@ -38,8 +37,7 @@ const Page = (props) => (
|
||||
Page.getInitialProps = async (ctx) => {
|
||||
const pageUrl = '/'
|
||||
const id = ctx.query.article
|
||||
const res = await axios.get(createApiUrl(ctx.req, `/api/articles/${id}`))
|
||||
const article = res.data
|
||||
const article = await requestGet(createApiUrl(ctx.req, `/api/articles/${id}`), {})
|
||||
const image = article.preview.artifacts.fb
|
||||
|
||||
return {
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import React from 'react'
|
||||
import axios from 'axios'
|
||||
import Head from 'next/head'
|
||||
import { App } from '../../src/components/App'
|
||||
import { ArticleCardList } from '../../src/components/ArticleCardList'
|
||||
@@ -7,7 +6,7 @@ import menuModel from '../../src/models/menu'
|
||||
import { buildMenu } from '../../src/lib/menu'
|
||||
import { meta } from '../../src/lib/meta'
|
||||
import { Meta } from '../../src/components/Meta'
|
||||
import { createApiUrl } from '../../src/next-lib'
|
||||
import { createApiUrl, requestGet } from '../../src/next-lib'
|
||||
|
||||
const Page = (props) => (
|
||||
<App
|
||||
@@ -33,10 +32,10 @@ Page.getInitialProps = async (ctx) => {
|
||||
const pageUrl = '/'
|
||||
const page = parseInt(ctx.query.page)
|
||||
const pageSize = parseInt(ctx.query.pageSize) || 15
|
||||
const res = await axios.get(createApiUrl(ctx.req, `/api/articles?page=${page}&pageSize=${pageSize}`))
|
||||
const articles = res.data.items
|
||||
const nextPage = res.data.nextPage
|
||||
const prevPage = res.data.prevPage
|
||||
const res = await requestGet(createApiUrl(ctx.req, `/api/articles?page=${page}&pageSize=${pageSize}`), {})
|
||||
const articles = res.items
|
||||
const nextPage = res.nextPage
|
||||
const prevPage = res.prevPage
|
||||
const title = 'События'
|
||||
|
||||
return {
|
||||
@@ -51,4 +50,4 @@ Page.getInitialProps = async (ctx) => {
|
||||
}
|
||||
}
|
||||
|
||||
export default Page
|
||||
export default Page
|
||||
@@ -1,5 +1,4 @@
|
||||
import React from 'react'
|
||||
import axios from 'axios'
|
||||
import Head from 'next/head'
|
||||
import { App } from '../src/components/App'
|
||||
import { Meta } from '../src/components/Meta'
|
||||
@@ -9,7 +8,7 @@ import { CardList } from '../src/components/CardList'
|
||||
import menuModel from '../src/models/menu'
|
||||
import { buildMenu } from '../src/lib/menu'
|
||||
import { meta } from '../src/lib/meta'
|
||||
import { createApiUrl } from '../src/next-lib'
|
||||
import { createApiUrl, requestGet } from '../src/next-lib'
|
||||
|
||||
const Page = (props) => (
|
||||
<App
|
||||
@@ -51,15 +50,16 @@ const Page = (props) => (
|
||||
Page.getInitialProps = async (ctx) => {
|
||||
const pageUrl = ctx.req.url
|
||||
|
||||
const res = await axios.get(createApiUrl(ctx.req, '/api/persons'))
|
||||
const persons = res.data.items
|
||||
const res = await requestGet(createApiUrl(ctx.req, '/api/persons'), {})
|
||||
const persons = res.items || []
|
||||
const title = 'Преподаватели Шлиссельбургской ДХШ'
|
||||
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 {
|
||||
persons,
|
||||
image: resImage.data.artifacts.large,
|
||||
image,
|
||||
pageUrl,
|
||||
title,
|
||||
meta: meta({
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import React from 'react'
|
||||
import axios from 'axios'
|
||||
import Head from 'next/head'
|
||||
import { App } from '../../src/components/App'
|
||||
import Document from '../../src/components/Document'
|
||||
@@ -8,7 +7,7 @@ import menuModel from '../../src/models/menu'
|
||||
import { buildMenu } from '../../src/lib/menu'
|
||||
import { meta } from '../../src/lib/meta'
|
||||
import { Meta } from '../../src/components/Meta'
|
||||
import { createApiUrl } from '../../src/next-lib'
|
||||
import { createApiUrl, requestGet } from '../../src/next-lib'
|
||||
|
||||
const Page = (props) => (
|
||||
<App
|
||||
@@ -30,8 +29,7 @@ const Page = (props) => (
|
||||
Page.getInitialProps = async (ctx) => {
|
||||
const pageUrl = '/documents'
|
||||
const id = ctx.query.document
|
||||
const res = await axios.get(createApiUrl(ctx.req, `/api/files/${id}`))
|
||||
const file = res.data
|
||||
const file = await requestGet(createApiUrl(ctx.req, `/api/files/${id}`), {})
|
||||
|
||||
return {
|
||||
file,
|
||||
|
||||
+3
-4
@@ -1,5 +1,4 @@
|
||||
import React from 'react'
|
||||
import axios from 'axios'
|
||||
import Head from 'next/head'
|
||||
import { App } from '../src/components/App'
|
||||
import { Meta } from '../src/components/Meta'
|
||||
@@ -9,7 +8,7 @@ import { meta } from '../src/lib/meta'
|
||||
|
||||
import DocumentList from '../src/components/DocumentList'
|
||||
import { unique } from '../src/utils/common'
|
||||
import { createApiUrl } from '../src/next-lib'
|
||||
import { createApiUrl, requestGet } from '../src/next-lib'
|
||||
|
||||
function getSorted(documents) {
|
||||
const compose = (...fns) => value => fns
|
||||
@@ -112,8 +111,8 @@ const Page = (props) => (
|
||||
|
||||
Page.getInitialProps = async (ctx) => {
|
||||
const pageUrl = '/documents'
|
||||
const res = await axios.get(createApiUrl(ctx.req, '/api/files'))
|
||||
const files = res.data.items
|
||||
const res = await requestGet(createApiUrl(ctx.req, '/api/files'), {})
|
||||
const files = res.items || []
|
||||
const title = 'Документы'
|
||||
const collections = getSorted(files)
|
||||
|
||||
|
||||
+3
-4
@@ -1,5 +1,4 @@
|
||||
import React from 'react'
|
||||
import axios from 'axios'
|
||||
import Head from 'next/head'
|
||||
import { App } from '../src/components/App'
|
||||
// import Album from '../src/components/Album'
|
||||
@@ -9,7 +8,7 @@ import menuModel from '../src/models/menu'
|
||||
import { buildMenu } from '../src/lib/menu'
|
||||
import { splitBy } from '../src/lib/array'
|
||||
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())
|
||||
|
||||
@@ -84,8 +83,8 @@ const Page = (props) => (
|
||||
|
||||
Page.getInitialProps = async (ctx) => {
|
||||
const pageUrl = ctx.req.url
|
||||
const res = await axios.get(createApiUrl(ctx.req, `/api/albums`))
|
||||
const items = res.data.items
|
||||
const res = await requestGet(createApiUrl(ctx.req, `/api/albums`), {})
|
||||
const items = res.items || []
|
||||
const albumCollections = albumsByYear(items)
|
||||
const title = 'Галерея'
|
||||
|
||||
|
||||
+5
-6
@@ -1,5 +1,4 @@
|
||||
import React from 'react'
|
||||
import axios from 'axios'
|
||||
import Head from 'next/head'
|
||||
import { App } from '../src/components/App'
|
||||
import { Meta } from '../src/components/Meta'
|
||||
@@ -8,7 +7,7 @@ import { HudozkaTitle } from '../src/components/HudozkaTitle'
|
||||
import menuModel from '../src/models/menu'
|
||||
import { meta } from '../src/lib/meta'
|
||||
import { buildMenu } from '../src/lib/menu'
|
||||
import { createApiUrl } from '../src/next-lib'
|
||||
import { createApiUrl, requestGet } from '../src/next-lib'
|
||||
|
||||
const Page = (props) => (
|
||||
<App
|
||||
@@ -37,10 +36,10 @@ Page.getInitialProps = async (ctx) => {
|
||||
const pageUrl = '/'
|
||||
const page = 1
|
||||
const pageSize = ctx.query.pageSize || 15
|
||||
const res = await axios.get(createApiUrl(ctx.req, `/api/articles?page=${page}&pageSize=${pageSize}`))
|
||||
const articles = res.data.items
|
||||
const nextPage = res.data.nextPage
|
||||
const prevPage = res.data.prevPage
|
||||
const res = await requestGet(createApiUrl(ctx.req, `/api/articles?page=${page}&pageSize=${pageSize}`), {})
|
||||
const articles = res.items
|
||||
const nextPage = res.nextPage
|
||||
const prevPage = res.prevPage
|
||||
const title = 'Шлиссельбургская ДХШ'
|
||||
|
||||
return {
|
||||
|
||||
+4
-6
@@ -1,5 +1,4 @@
|
||||
import React from 'react'
|
||||
import axios from 'axios'
|
||||
import Head from 'next/head'
|
||||
import { App } from '../src/components/App'
|
||||
import Page from '../src/components/Page'
|
||||
@@ -7,14 +6,14 @@ 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 } from '../src/next-lib'
|
||||
import { createApiUrl, requestGet } from '../src/next-lib'
|
||||
|
||||
const Index = (props) => (
|
||||
<App
|
||||
menu={buildMenu(props.pageUrl, menuModel)}
|
||||
showAuthor={true}
|
||||
menuPadding={true}
|
||||
>
|
||||
>
|
||||
<Head>
|
||||
<title>{props.title}</title>
|
||||
<Meta meta={props.meta} />
|
||||
@@ -33,9 +32,8 @@ const Index = (props) => (
|
||||
Index.getInitialProps = async (ctx) => {
|
||||
const pageUrl = ctx.req.url
|
||||
// // const pageUrl = '/neighbors/2018'
|
||||
const res = await axios.get(createApiUrl(ctx.req, `/api/page?page=${pageUrl}`))
|
||||
const page = res.data
|
||||
const image = page.preview.artifacts.fb
|
||||
const page = await requestGet(createApiUrl(ctx.req, `/api/page?page=${pageUrl}`), {})
|
||||
const image = page.preview ? page.preview.artifacts.fb : {}
|
||||
|
||||
return {
|
||||
content: page.data,
|
||||
|
||||
+2
-4
@@ -1,12 +1,11 @@
|
||||
import React from 'react'
|
||||
import axios from 'axios'
|
||||
import Head from 'next/head'
|
||||
import { App } from '../src/components/App'
|
||||
import Schedule from '../src/components/Schedule'
|
||||
import Share from '../src/components/Share'
|
||||
import menuModel from '../src/models/menu'
|
||||
import { buildMenu } from '../src/lib/menu'
|
||||
import { createApiUrl } from '../src/next-lib'
|
||||
import { createApiUrl, requestGet } from '../src/next-lib'
|
||||
|
||||
const Page = (props) => (
|
||||
<App
|
||||
@@ -32,8 +31,7 @@ Page.getInitialProps = async (ctx) => {
|
||||
const pageUrl = ctx.req.url
|
||||
const period = '2016-2017'
|
||||
const semester = 'spring'
|
||||
const res = await axios.get(createApiUrl(ctx.req, `/api/schedule?period=${period}&semester=${semester}`))
|
||||
const schedule = res.data
|
||||
const schedule = await requestGet(createApiUrl(ctx.req, `/api/schedule?period=${period}&semester=${semester}`), {})
|
||||
|
||||
return {
|
||||
schedule,
|
||||
|
||||
@@ -7,7 +7,7 @@ import { Meta } from '../../src/components/Meta'
|
||||
import menuModel from '../../src/models/menu'
|
||||
import { buildMenu } from '../../src/lib/menu'
|
||||
import { meta } from '../../src/lib/meta'
|
||||
import { createApiUrl } from '../../src/next-lib'
|
||||
import { createApiUrl, requestGet } from '../../src/next-lib'
|
||||
|
||||
const Index = (props) => (
|
||||
<App
|
||||
@@ -30,11 +30,10 @@ const Index = (props) => (
|
||||
Index.getInitialProps = async (ctx) => {
|
||||
const pageUrl = '/collective'
|
||||
const id = ctx.query.person
|
||||
const res = await axios.get(createApiUrl(ctx.req, `/api/persons/${id}`))
|
||||
const person = res.data
|
||||
const person = await requestGet(createApiUrl(ctx.req, `/api/persons/${id}`), {})
|
||||
const name = person.name || []
|
||||
const title = name.join(' ')
|
||||
const image = person.preview.artifacts.fb
|
||||
const image = person.preview ? person.preview.artifacts.fb : {}
|
||||
|
||||
return {
|
||||
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) {
|
||||
const { origin } = absoluteUrl(req, 'localhost:3000')
|
||||
|
||||
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