Skip to content

Instantly share code, notes, and snippets.

View akellbl4's full-sized avatar

Paul akellbl4

View GitHub Profile
@akellbl4
akellbl4 / _app.js
Last active December 13, 2022 13:08
Code example for the article: "Why I got rid of getInitialProps on my work Next.js project"
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}`;
@akellbl4
akellbl4 / repos-gsp.js
Created March 7, 2021 10:13
Code example for the article: "Why I got rid of getInitialProps on my work Next.js project"
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,
};
}
@akellbl4
akellbl4 / repos-gsp.js
Created March 7, 2021 10:12
Code example for the article: "Why I got rid of getInitialProps on my work Next.js project"
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 },
};
}
@akellbl4
akellbl4 / repos-gip.js
Created March 7, 2021 10:11
Code example for the article: "Why I got rid of getInitialProps on my work Next.js project"
Repos.getInitialProps = async () => {
const res = await fetch('https://api.github.com/repos/akellbl4/akellbl4');
const repo = await res.json();
return { stars: repo.stargazers_count };
};
@akellbl4
akellbl4 / page-ssp.js
Created March 7, 2021 10:10
Code example for the article: "Why I got rid of getInitialProps on my work Next.js project"
export function getServerSideProps() {
return {
redirect: {
destination: '/path/to/target',
permanent: false,
},
};
}
@akellbl4
akellbl4 / page-gip.js
Created March 7, 2021 10:09
Code example for the article: "Why I got rid of getInitialProps on my work Next.js project"
// 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 {};
@akellbl4
akellbl4 / profile.js
Created March 7, 2021 10:08
Code example for the article: "Why I got rid of getInitialProps on my work Next.js project"
// 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);
@akellbl4
akellbl4 / compose.js
Created March 7, 2021 10:06
Code example for the article: "Why I got rid of getInitialProps on my work Next.js project"
// 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
@akellbl4
akellbl4 / auth-wrapper.js
Last active May 20, 2023 13:45
Code example for the article: "Why I got rid of getInitialProps on my work Next.js project"
export function authWrapper(next) {
return async function auth(ctx) {
const user = await session.getUser();
if (!user) {
return {
redirect: {
destination: '/sign-in',
permanent: false,
},
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);