Use getServerSideProps (#982)

* use getServerSideProps

* return host too
main
Michael Fix 5 years ago committed by GitHub
parent ae9b4445f1
commit fdcde85a48
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -2,13 +2,10 @@ import React from 'react'
import Router from 'next/router' import Router from 'next/router'
import IndexPage from './index' import IndexPage from './index'
import ApiContext from '../components/ApiContext'
import api from '../lib/api' import api from '../lib/api'
class IdPage extends React.PureComponent { export async function getServerSideProps({ req, res, query }) {
static contextType = ApiContext
static async getInitialProps({ req, res, query }) {
const path = query.id const path = query.id
const parameter = path.length >= 19 && path.indexOf('.') < 0 ? path : null const parameter = path.length >= 19 && path.indexOf('.') < 0 ? path : null
@ -17,7 +14,7 @@ class IdPage extends React.PureComponent {
const host = req ? req.headers.host : undefined const host = req ? req.headers.host : undefined
snippet = await api.snippet.get(parameter, { host }) snippet = await api.snippet.get(parameter, { host })
if (snippet) { if (snippet) {
return { snippet } return { props: { snippet, host } }
} }
// 404 Not found // 404 Not found
@ -31,12 +28,9 @@ class IdPage extends React.PureComponent {
} }
} }
return {} return { props: {} }
} }
render() { export default React.memo(function IdPage(props) {
return <IndexPage {...this.props} /> return <IndexPage {...props} />
} })
}
export default IdPage

@ -2,13 +2,10 @@ import React from 'react'
import Router from 'next/router' import Router from 'next/router'
import EmbedPage from './index' import EmbedPage from './index'
import ApiContext from '../../components/ApiContext'
import api from '../../lib/api' import api from '../../lib/api'
class EmbedIdPage extends React.PureComponent { export async function getServerSideProps({ req, res, query }) {
static contextType = ApiContext
static async getInitialProps({ req, res, query }) {
const path = query.id const path = query.id
const parameter = path.length >= 19 && path.indexOf('.') < 0 ? path : null const parameter = path.length >= 19 && path.indexOf('.') < 0 ? path : null
@ -17,7 +14,7 @@ class EmbedIdPage extends React.PureComponent {
const host = req ? req.headers.host : undefined const host = req ? req.headers.host : undefined
snippet = await api.snippet.get(parameter, { host }) snippet = await api.snippet.get(parameter, { host })
if (snippet /* && snippet.gist_id */) { if (snippet /* && snippet.gist_id */) {
return { snippet } return { props: { snippet } }
} }
// 404 Not found // 404 Not found
@ -31,12 +28,9 @@ class EmbedIdPage extends React.PureComponent {
} }
} }
return {} return { props: {} }
} }
render() { export default React.memo(function EmbedIdPage(props) {
return <EmbedPage {...this.props} /> return <EmbedPage {...props} />
} })
}
export default EmbedIdPage

Loading…
Cancel
Save