This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import App from 'next/app'; | |
async function fetchUser(ctx) { | |
const headers = {}; | |
if (ctx.req) { | |
const { token } = ctx.req.cookies; | |
if (token) { | |
headers['Authorization'] = `Bearer ${token}`; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export async function getStaticPaths() { | |
const res = await fetch('https://api.github.com/users/akellbl4/repos'); | |
const repos = await res.json(); | |
const paths = repos.map(({ name }) => ({ name })); | |
return { | |
paths, | |
fallback: false, | |
}; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export async function getStaticProps() { | |
const res = await fetch('https://api.github.com/repos/akellbl4/akellbl4'); | |
const repo = await res.json(); | |
return { | |
props: { start: repo.stargazers_count }, | |
}; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Repos.getInitialProps = async () => { | |
const res = await fetch('https://api.github.com/repos/akellbl4/akellbl4'); | |
const repo = await res.json(); | |
return { stars: repo.stargazers_count }; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function getServerSideProps() { | |
return { | |
redirect: { | |
destination: '/path/to/target', | |
permanent: false, | |
}, | |
}; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// That how redirects work in `getInitialProps` and you should google for it | |
Page.getInitialProps = ({ res }) => { | |
if (res) { | |
res.writeHead(307, { Location: '/path/to/target' }); | |
res.end(); | |
} | |
Router.replace('/path/to/target'); | |
return {}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// pages/profile.js | |
import compose from 'lib/compose'; | |
import session from 'lib/session'; | |
import fetchArticles from 'data/articles'; | |
import fetchFriends from 'data/friends'; | |
import Profile from 'components/Profile'; | |
import ArticlesList from 'components/ArticlesList'; | |
import FriendsList from 'components/FriendsList'; | |
export const getServerSideProps = compose(auth, getUserArticles, getUserFriends); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// lib/compose.js | |
// Pass middlewares as arguments to out composer | |
export function compose(...middlewares) { | |
// Return getServerSideProps handler | |
return async function composer(ctx) { | |
let prevIndex = -1; | |
const pageProps = { props: {} }; | |
// Create middlewares runner |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function authWrapper(next) { | |
return async function auth(ctx) { | |
const user = await session.getUser(); | |
if (!user) { | |
return { | |
redirect: { | |
destination: '/sign-in', | |
permanent: false, | |
}, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const http = require("http"); | |
const fetch = require("isomorphic-unfetch"); | |
const { API_URL, PORT, HOST } = process.env; | |
const map = new Map(); | |
async function handler(req, res) { | |
if (map.has(req.url)) { | |
const data = await map.get(req.url); |