next pages uses absolute url resolver to access api from getInitialProps
This commit is contained in:
1 parent
28a4d05fe4
commit
a502ddd4df
14 files changed
+37
-24
No files matched your search
Generated
+5
@@ -13533,6 +13533,11 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"next-absolute-url": {
|
||||||
|
"version": "1.1.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/next-absolute-url/-/next-absolute-url-1.1.1.tgz",
|
||||||
|
"integrity": "sha512-RrRIOJyuoTslfQ3SLQfRlbtFx2ylgOhTb5CaBpUZLPIRrOLUj72/yeHmU2MC1aRfMuW1urL40URypdqECCINdA=="
|
||||||
|
},
|
||||||
"next-server": {
|
"next-server": {
|
||||||
"version": "9.0.2",
|
"version": "9.0.2",
|
||||||
"resolved": "https://registry.npmjs.org/next-server/-/next-server-9.0.2.tgz",
|
"resolved": "https://registry.npmjs.org/next-server/-/next-server-9.0.2.tgz",
|
||||||
|
|||||||
@@ -37,6 +37,7 @@
|
|||||||
"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",
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ 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'
|
||||||
|
|
||||||
const Page = (props) => (
|
const Page = (props) => (
|
||||||
<App
|
<App
|
||||||
@@ -33,8 +34,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 apiUrl = `http://localhost:3000/api/albums/${id}`
|
const res = await axios.get(createApiUrl(ctx.req, `/api/albums/${id}`))
|
||||||
const res = await axios.get(apiUrl)
|
|
||||||
const album = res.data
|
const album = res.data
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -8,6 +8,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'
|
||||||
|
|
||||||
const Page = (props) => (
|
const Page = (props) => (
|
||||||
<App
|
<App
|
||||||
@@ -37,8 +38,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 apiUrl = `http://localhost:3000/api/articles/${id}`
|
const res = await axios.get(createApiUrl(ctx.req, `/api/articles/${id}`))
|
||||||
const res = await axios.get(apiUrl)
|
|
||||||
const article = res.data
|
const article = res.data
|
||||||
const image = article.preview.artifacts.fb
|
const image = article.preview.artifacts.fb
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +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'
|
||||||
|
|
||||||
const Page = (props) => (
|
const Page = (props) => (
|
||||||
<App
|
<App
|
||||||
@@ -32,8 +33,7 @@ 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 apiUrl = `http://localhost:3000/api/articles?page=${page}&pageSize=${pageSize}`
|
const res = await axios.get(createApiUrl(ctx.req, `/api/articles?page=${page}&pageSize=${pageSize}`))
|
||||||
const res = await axios.get(apiUrl)
|
|
||||||
const articles = res.data.items
|
const articles = res.data.items
|
||||||
const nextPage = res.data.nextPage
|
const nextPage = res.data.nextPage
|
||||||
const prevPage = res.data.prevPage
|
const prevPage = res.data.prevPage
|
||||||
|
|||||||
@@ -9,6 +9,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'
|
||||||
|
|
||||||
const Page = (props) => (
|
const Page = (props) => (
|
||||||
<App
|
<App
|
||||||
@@ -49,13 +50,12 @@ const Page = (props) => (
|
|||||||
|
|
||||||
Page.getInitialProps = async (ctx) => {
|
Page.getInitialProps = async (ctx) => {
|
||||||
const pageUrl = ctx.req.url
|
const pageUrl = ctx.req.url
|
||||||
const apiUrl = `http://localhost:3000/api/persons`
|
|
||||||
const res = await axios.get(apiUrl)
|
const res = await axios.get(createApiUrl(ctx.req, '/api/persons'))
|
||||||
const persons = res.data.items
|
const persons = res.data.items
|
||||||
const title = 'Преподаватели Шлиссельбургской ДХШ'
|
const title = 'Преподаватели Шлиссельбургской ДХШ'
|
||||||
const imageFile = 'Images/HudozkaCollective2017.jpg'
|
const imageFile = 'Images/HudozkaCollective2017.jpg'
|
||||||
const imageUrl = `http://localhost:3000/api/image?file=${imageFile}`
|
const resImage = await axios.get(createApiUrl(ctx.req, `/api/image?file=${imageFile}`))
|
||||||
const resImage = await axios.get(imageUrl)
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
persons,
|
persons,
|
||||||
|
|||||||
@@ -8,6 +8,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'
|
||||||
|
|
||||||
const Page = (props) => (
|
const Page = (props) => (
|
||||||
<App
|
<App
|
||||||
@@ -29,8 +30,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 apiUrl = `http://localhost:3000/api/files/${id}`
|
const res = await axios.get(createApiUrl(ctx.req, `/api/files/${id}`))
|
||||||
const res = await axios.get(apiUrl)
|
|
||||||
const file = res.data
|
const file = res.data
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
+2
-2
@@ -9,6 +9,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'
|
||||||
|
|
||||||
function getSorted(documents) {
|
function getSorted(documents) {
|
||||||
const compose = (...fns) => value => fns
|
const compose = (...fns) => value => fns
|
||||||
@@ -111,8 +112,7 @@ const Page = (props) => (
|
|||||||
|
|
||||||
Page.getInitialProps = async (ctx) => {
|
Page.getInitialProps = async (ctx) => {
|
||||||
const pageUrl = '/documents'
|
const pageUrl = '/documents'
|
||||||
const apiUrl = `http://localhost:3000/api/files`
|
const res = await axios.get(createApiUrl(ctx.req, '/api/files'))
|
||||||
const res = await axios.get(apiUrl)
|
|
||||||
const files = res.data.items
|
const files = res.data.items
|
||||||
const title = 'Документы'
|
const title = 'Документы'
|
||||||
const collections = getSorted(files)
|
const collections = getSorted(files)
|
||||||
|
|||||||
+2
-2
@@ -9,6 +9,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'
|
||||||
|
|
||||||
const albumsByYear = splitBy(album => new Date(album.date).getFullYear())
|
const albumsByYear = splitBy(album => new Date(album.date).getFullYear())
|
||||||
|
|
||||||
@@ -83,8 +84,7 @@ const Page = (props) => (
|
|||||||
|
|
||||||
Page.getInitialProps = async (ctx) => {
|
Page.getInitialProps = async (ctx) => {
|
||||||
const pageUrl = ctx.req.url
|
const pageUrl = ctx.req.url
|
||||||
const apiUrl = `http://localhost:3000/api/albums`
|
const res = await axios.get(createApiUrl(ctx.req, `/api/albums`))
|
||||||
const res = await axios.get(apiUrl)
|
|
||||||
const items = res.data.items
|
const items = res.data.items
|
||||||
const albumCollections = albumsByYear(items)
|
const albumCollections = albumsByYear(items)
|
||||||
const title = 'Галерея'
|
const title = 'Галерея'
|
||||||
|
|||||||
+2
-2
@@ -8,6 +8,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'
|
||||||
|
|
||||||
const Page = (props) => (
|
const Page = (props) => (
|
||||||
<App
|
<App
|
||||||
@@ -36,8 +37,7 @@ 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 apiUrl = `http://localhost:3000/api/articles?page=${page}&pageSize=${pageSize}`
|
const res = await axios.get(createApiUrl(ctx.req, `/api/articles?page=${page}&pageSize=${pageSize}`))
|
||||||
const res = await axios.get(apiUrl)
|
|
||||||
const articles = res.data.items
|
const articles = res.data.items
|
||||||
const nextPage = res.data.nextPage
|
const nextPage = res.data.nextPage
|
||||||
const prevPage = res.data.prevPage
|
const prevPage = res.data.prevPage
|
||||||
|
|||||||
+2
-2
@@ -7,6 +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'
|
||||||
|
|
||||||
const Index = (props) => (
|
const Index = (props) => (
|
||||||
<App
|
<App
|
||||||
@@ -32,8 +33,7 @@ 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 apiUrl = `http://localhost:3000/api/page?page=${pageUrl}`
|
const res = await axios.get(createApiUrl(ctx.req, `/api/page?page=${pageUrl}`))
|
||||||
const res = await axios.get(apiUrl)
|
|
||||||
const page = res.data
|
const page = res.data
|
||||||
const image = page.preview.artifacts.fb
|
const image = page.preview.artifacts.fb
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -6,6 +6,7 @@ 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'
|
||||||
|
|
||||||
const Page = (props) => (
|
const Page = (props) => (
|
||||||
<App
|
<App
|
||||||
@@ -31,8 +32,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 apiUrl = `http://localhost:3000/api/schedule?period=${period}&semester=${semester}`
|
const res = await axios.get(createApiUrl(ctx.req, `/api/schedule?period=${period}&semester=${semester}`))
|
||||||
const res = await axios.get(apiUrl)
|
|
||||||
const schedule = res.data
|
const schedule = res.data
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -7,6 +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'
|
||||||
|
|
||||||
const Index = (props) => (
|
const Index = (props) => (
|
||||||
<App
|
<App
|
||||||
@@ -29,8 +30,7 @@ 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 apiUrl = `http://localhost:3000/api/persons/${id}`
|
const res = await axios.get(createApiUrl(ctx.req, `/api/persons/${id}`))
|
||||||
const res = await axios.get(apiUrl)
|
|
||||||
const person = res.data
|
const person = res.data
|
||||||
const name = person.name || []
|
const name = person.name || []
|
||||||
const title = name.join(' ')
|
const title = name.join(' ')
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
import absoluteUrl from 'next-absolute-url'
|
||||||
|
|
||||||
|
export function createApiUrl(req, path) {
|
||||||
|
const { origin } = absoluteUrl(req, 'localhost:3000')
|
||||||
|
|
||||||
|
return `${origin}${path}`
|
||||||
|
}
|
||||||
Reference in new issue
Block a user