From fdcde85a487233b878be08130b586e2590e99b31 Mon Sep 17 00:00:00 2001 From: Michael Fix Date: Sun, 5 Apr 2020 16:23:12 -0700 Subject: [PATCH] Use getServerSideProps (#982) * use getServerSideProps * return host too --- pages/[id].js | 50 ++++++++++++++++++++------------------------- pages/embed/[id].js | 50 ++++++++++++++++++++------------------------- 2 files changed, 44 insertions(+), 56 deletions(-) diff --git a/pages/[id].js b/pages/[id].js index a4886ab..f238d9a 100644 --- a/pages/[id].js +++ b/pages/[id].js @@ -2,41 +2,35 @@ import React from 'react' import Router from 'next/router' import IndexPage from './index' -import ApiContext from '../components/ApiContext' import api from '../lib/api' -class IdPage extends React.PureComponent { - static contextType = ApiContext - static async getInitialProps({ req, res, query }) { - const path = query.id - const parameter = path.length >= 19 && path.indexOf('.') < 0 ? path : null +export async function getServerSideProps({ req, res, query }) { + const path = query.id + const parameter = path.length >= 19 && path.indexOf('.') < 0 ? path : null - let snippet - if (parameter) { - const host = req ? req.headers.host : undefined - snippet = await api.snippet.get(parameter, { host }) - if (snippet) { - return { snippet } - } - - // 404 Not found - if (res) { - res.writeHead(302, { - Location: '/', - }) - res.end() - } else { - Router.push('/') - } + let snippet + if (parameter) { + const host = req ? req.headers.host : undefined + snippet = await api.snippet.get(parameter, { host }) + if (snippet) { + return { props: { snippet, host } } } - return {} + // 404 Not found + if (res) { + res.writeHead(302, { + Location: '/', + }) + res.end() + } else { + Router.push('/') + } } - render() { - return - } + return { props: {} } } -export default IdPage +export default React.memo(function IdPage(props) { + return +}) diff --git a/pages/embed/[id].js b/pages/embed/[id].js index eb8f8fd..a4dec7e 100644 --- a/pages/embed/[id].js +++ b/pages/embed/[id].js @@ -2,41 +2,35 @@ import React from 'react' import Router from 'next/router' import EmbedPage from './index' -import ApiContext from '../../components/ApiContext' import api from '../../lib/api' -class EmbedIdPage extends React.PureComponent { - static contextType = ApiContext - static async getInitialProps({ req, res, query }) { - const path = query.id - const parameter = path.length >= 19 && path.indexOf('.') < 0 ? path : null +export async function getServerSideProps({ req, res, query }) { + const path = query.id + const parameter = path.length >= 19 && path.indexOf('.') < 0 ? path : null - let snippet - if (parameter) { - const host = req ? req.headers.host : undefined - snippet = await api.snippet.get(parameter, { host }) - if (snippet /* && snippet.gist_id */) { - return { snippet } - } - - // 404 Not found - if (res) { - res.writeHead(302, { - Location: '/embed', - }) - res.end() - } else { - Router.push('/embed') - } + let snippet + if (parameter) { + const host = req ? req.headers.host : undefined + snippet = await api.snippet.get(parameter, { host }) + if (snippet /* && snippet.gist_id */) { + return { props: { snippet } } } - return {} + // 404 Not found + if (res) { + res.writeHead(302, { + Location: '/embed', + }) + res.end() + } else { + Router.push('/embed') + } } - render() { - return - } + return { props: {} } } -export default EmbedIdPage +export default React.memo(function EmbedIdPage(props) { + return +})