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,41 +2,35 @@ 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 const path = query.id
static async getInitialProps({ req, res, query }) { const parameter = path.length >= 19 && path.indexOf('.') < 0 ? path : null
const path = query.id
const parameter = path.length >= 19 && path.indexOf('.') < 0 ? path : null
let snippet let snippet
if (parameter) { if (parameter) {
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
if (res) {
res.writeHead(302, {
Location: '/',
})
res.end()
} else {
Router.push('/')
}
} }
return {} // 404 Not found
if (res) {
res.writeHead(302, {
Location: '/',
})
res.end()
} else {
Router.push('/')
}
} }
render() { return { props: {} }
return <IndexPage {...this.props} />
}
} }
export default IdPage export default React.memo(function IdPage(props) {
return <IndexPage {...props} />
})

@ -2,41 +2,35 @@ 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 const path = query.id
static async getInitialProps({ req, res, query }) { const parameter = path.length >= 19 && path.indexOf('.') < 0 ? path : null
const path = query.id
const parameter = path.length >= 19 && path.indexOf('.') < 0 ? path : null
let snippet let snippet
if (parameter) { if (parameter) {
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
if (res) {
res.writeHead(302, {
Location: '/embed',
})
res.end()
} else {
Router.push('/embed')
}
} }
return {} // 404 Not found
if (res) {
res.writeHead(302, {
Location: '/embed',
})
res.end()
} else {
Router.push('/embed')
}
} }
render() { return { props: {} }
return <EmbedPage {...this.props} />
}
} }
export default EmbedIdPage export default React.memo(function EmbedIdPage(props) {
return <EmbedPage {...props} />
})

Loading…
Cancel
Save