From fe5cc3618286faf5363ce91115bdc7dfd94a4d96 Mon Sep 17 00:00:00 2001 From: Roman Timashev Date: Sun, 13 Oct 2019 19:48:52 +0300 Subject: [PATCH] added wrapper to getInitialProps to prevent api calls in production --- pages/album/[album].jsx | 6 +++--- pages/article/[article].jsx | 6 +++--- pages/articles/[page].jsx | 6 +++--- pages/collective.jsx | 6 +++--- pages/document/[document].jsx | 6 +++--- pages/documents.jsx | 6 +++--- pages/gallery.jsx | 6 +++--- pages/index.jsx | 6 +++--- pages/next.jsx | 6 +++--- pages/schedule.jsx | 6 +++--- pages/teacher/[person].jsx | 6 +++--- src/next-lib.js | 16 ++++++++++++++++ 12 files changed, 49 insertions(+), 33 deletions(-) diff --git a/pages/album/[album].jsx b/pages/album/[album].jsx index 81c77d28..b688845c 100644 --- a/pages/album/[album].jsx +++ b/pages/album/[album].jsx @@ -5,7 +5,7 @@ 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, requestGet } from '../../src/next-lib' +import { createApiUrl, requestGet, wrapInitialProps } from '../../src/next-lib' const Page = (props) => ( ( ) -Page.getInitialProps = async (ctx) => { +Page.getInitialProps = wrapInitialProps(async (ctx) => { const pageUrl = '/gallery' const id = ctx.query.album const album = await requestGet(createApiUrl(ctx.req, `/api/albums/${id}`), {}) @@ -39,6 +39,6 @@ Page.getInitialProps = async (ctx) => { album, pageUrl, } -} +}) export default Page \ No newline at end of file diff --git a/pages/article/[article].jsx b/pages/article/[article].jsx index 79016ba4..dfad639e 100644 --- a/pages/article/[article].jsx +++ b/pages/article/[article].jsx @@ -8,7 +8,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, requestGet } from '../../src/next-lib' +import { createApiUrl, requestGet, wrapInitialProps } from '../../src/next-lib' const Page = (props) => ( ( ) -Page.getInitialProps = async (ctx) => { +Page.getInitialProps = wrapInitialProps(async (ctx) => { const pageUrl = '/' const id = ctx.query.article const article = await requestGet(createApiUrl(ctx.req, `/api/articles/${id}`), {}) @@ -51,6 +51,6 @@ Page.getInitialProps = async (ctx) => { imageHeight: image.height, }), } -} +}) export default Page \ No newline at end of file diff --git a/pages/articles/[page].jsx b/pages/articles/[page].jsx index 8ee29d6d..555c3ad1 100644 --- a/pages/articles/[page].jsx +++ b/pages/articles/[page].jsx @@ -6,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, requestGet } from '../../src/next-lib' +import { createApiUrl, requestGet, wrapInitialProps } from '../../src/next-lib' const Page = (props) => ( ( ) -Page.getInitialProps = async (ctx) => { +Page.getInitialProps = wrapInitialProps(async (ctx) => { const pageUrl = '/' const page = parseInt(ctx.query.page) const pageSize = parseInt(ctx.query.pageSize) || 15 @@ -48,6 +48,6 @@ Page.getInitialProps = async (ctx) => { title, }) } -} +}) export default Page diff --git a/pages/collective.jsx b/pages/collective.jsx index 598a5069..4371a0a2 100644 --- a/pages/collective.jsx +++ b/pages/collective.jsx @@ -9,7 +9,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, requestGet } from '../src/next-lib' +import { createApiUrl, requestGet, wrapInitialProps } from '../src/next-lib' const Page = (props) => ( ( ) -Page.getInitialProps = async (ctx) => { +Page.getInitialProps = wrapInitialProps(async (ctx) => { const pageUrl = ctx.req.url const res = await requestGet(createApiUrl(ctx.req, '/api/persons'), {}) @@ -69,6 +69,6 @@ Page.getInitialProps = async (ctx) => { description: 'Преподаватели Шлиссельбургской ДХШ', }) } -} +}) export default Page \ No newline at end of file diff --git a/pages/document/[document].jsx b/pages/document/[document].jsx index 6845d8bd..6c9ecc45 100644 --- a/pages/document/[document].jsx +++ b/pages/document/[document].jsx @@ -7,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, requestGet } from '../../src/next-lib' +import { createApiUrl, requestGet, wrapInitialProps } from '../../src/next-lib' const Page = (props) => ( ( ) -Page.getInitialProps = async (ctx) => { +Page.getInitialProps = wrapInitialProps(async (ctx) => { const pageUrl = '/documents' const id = ctx.query.document const file = await requestGet(createApiUrl(ctx.req, `/api/files/${id}`), {}) @@ -38,6 +38,6 @@ Page.getInitialProps = async (ctx) => { title: file.meta, }) } -} +}) export default Page \ No newline at end of file diff --git a/pages/documents.jsx b/pages/documents.jsx index 9df5f187..cad37812 100644 --- a/pages/documents.jsx +++ b/pages/documents.jsx @@ -8,7 +8,7 @@ import { meta } from '../src/lib/meta' import DocumentList from '../src/components/DocumentList' import { unique } from '../src/utils/common' -import { createApiUrl, requestGet } from '../src/next-lib' +import { createApiUrl, requestGet, wrapInitialProps } from '../src/next-lib' function getSorted(documents) { const compose = (...fns) => value => fns @@ -109,7 +109,7 @@ const Page = (props) => ( ) -Page.getInitialProps = async (ctx) => { +Page.getInitialProps = wrapInitialProps(async (ctx) => { const pageUrl = '/documents' const res = await requestGet(createApiUrl(ctx.req, '/api/files'), {}) const files = res.items || [] @@ -125,6 +125,6 @@ Page.getInitialProps = async (ctx) => { description: 'Документы Шлиссельбургской ДХШ', }) } -} +}) export default Page \ No newline at end of file diff --git a/pages/gallery.jsx b/pages/gallery.jsx index 7e5bdab0..2f3db5ff 100644 --- a/pages/gallery.jsx +++ b/pages/gallery.jsx @@ -8,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, requestGet } from '../src/next-lib' +import { createApiUrl, requestGet, wrapInitialProps } from '../src/next-lib' const albumsByYear = splitBy(album => new Date(album.date).getFullYear()) @@ -81,7 +81,7 @@ const Page = (props) => ( ) -Page.getInitialProps = async (ctx) => { +Page.getInitialProps = wrapInitialProps(async (ctx) => { const pageUrl = ctx.req.url const res = await requestGet(createApiUrl(ctx.req, `/api/albums`), {}) const items = res.items || [] @@ -97,6 +97,6 @@ Page.getInitialProps = async (ctx) => { description: 'Галерея работ учащихся Шлиссельбургской Детской Художественной Школы' }) } -} +}) export default Page \ No newline at end of file diff --git a/pages/index.jsx b/pages/index.jsx index ef0d3e87..df3e7fa4 100644 --- a/pages/index.jsx +++ b/pages/index.jsx @@ -7,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, requestGet } from '../src/next-lib' +import { createApiUrl, requestGet, wrapInitialProps } from '../src/next-lib' const Page = (props) => ( ( ) -Page.getInitialProps = async (ctx) => { +Page.getInitialProps = wrapInitialProps(async (ctx) => { const pageUrl = '/' const page = 1 const pageSize = ctx.query.pageSize || 15 @@ -53,6 +53,6 @@ Page.getInitialProps = async (ctx) => { url: pageUrl, }) } -} +}) export default Page \ No newline at end of file diff --git a/pages/next.jsx b/pages/next.jsx index 9db46e6e..d806899a 100644 --- a/pages/next.jsx +++ b/pages/next.jsx @@ -6,7 +6,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, requestGet } from '../src/next-lib' +import { createApiUrl, requestGet, wrapInitialProps } from '../src/next-lib' const Index = (props) => ( ( ) -Index.getInitialProps = async (ctx) => { +Index.getInitialProps = wrapInitialProps(async (ctx) => { const pageUrl = ctx.req.url // // const pageUrl = '/neighbors/2018' const page = await requestGet(createApiUrl(ctx.req, `/api/page?page=${pageUrl}`), {}) @@ -46,6 +46,6 @@ Index.getInitialProps = async (ctx) => { imageHeight: image.height, }), } -} +}) export default Index \ No newline at end of file diff --git a/pages/schedule.jsx b/pages/schedule.jsx index 41894576..37aaac6a 100644 --- a/pages/schedule.jsx +++ b/pages/schedule.jsx @@ -5,7 +5,7 @@ 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, requestGet } from '../src/next-lib' +import { createApiUrl, requestGet, wrapInitialProps } from '../src/next-lib' const Page = (props) => ( ( ) -Page.getInitialProps = async (ctx) => { +Page.getInitialProps = wrapInitialProps(async (ctx) => { const pageUrl = ctx.req.url const period = '2016-2017' const semester = 'spring' @@ -38,6 +38,6 @@ Page.getInitialProps = async (ctx) => { pageUrl, title: 'Расписание', } -} +}) export default Page \ No newline at end of file diff --git a/pages/teacher/[person].jsx b/pages/teacher/[person].jsx index 5f4f5a04..3ad105eb 100644 --- a/pages/teacher/[person].jsx +++ b/pages/teacher/[person].jsx @@ -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, requestGet } from '../../src/next-lib' +import { createApiUrl, requestGet, wrapInitialProps } from '../../src/next-lib' const Index = (props) => ( ( ) -Index.getInitialProps = async (ctx) => { +Index.getInitialProps = wrapInitialProps(async (ctx) => { const pageUrl = '/collective' const id = ctx.query.person const person = await requestGet(createApiUrl(ctx.req, `/api/persons/${id}`), {}) @@ -46,6 +46,6 @@ Index.getInitialProps = async (ctx) => { imageHeight: image.height, }) } -} +}) export default Index \ No newline at end of file diff --git a/src/next-lib.js b/src/next-lib.js index 1a62e523..40d54426 100644 --- a/src/next-lib.js +++ b/src/next-lib.js @@ -10,6 +10,22 @@ export async function requestGet(url, defaultResponse) { } } +/** + * This wrapper prevents fetching api request in static export mode and production environment + * because nextjs call getInitialProps on the client side + * + * @param {*} fn + */ +export function wrapInitialProps(fn) { + return async ctx => { + if (process.env.NODE_ENV === 'production' && process.browser) { + return __NEXT_DATA__.props.pageProps; + } + + return fn(ctx) + } +} + export function createApiUrl(req, path) { const { origin } = absoluteUrl(req, 'localhost:3000')