Skip to content

Instantly share code, notes, and snippets.

View bravetheheat's full-sized avatar

David Zhao bravetheheat

  • Wowa Leads, Inc.
  • Toronto, ON
View GitHub Profile
@bravetheheat
bravetheheat / DeferNextScript.tsx
Created December 19, 2020 17:47
NEXT.js Async to Defer Pagespeed Hack
import { NextScript } from 'next/document';
function dedupe<T extends { file: string }>(bundles: T[]): T[] {
const files = new Set<string>();
const kept: T[] = [];
for (const bundle of bundles) {
if (files.has(bundle.file)) continue;
files.add(bundle.file);
kept.push(bundle);
@bravetheheat
bravetheheat / LanguageProvider.tsx
Last active December 4, 2020 03:51
A React wrapper for next-i18next that switches languages based on pathname.
const LanguageProvider = ({ children }) => {
const router = useRouter();
const { i18n } = useTranslation();
useEffect(() => {
const handleRouteChangeComplete = (path: string) => {
// Find the lng for the path
const findPathReturnLng = (): string => {
let i;
for (i = 0; i < routes.length; i++) {
Type Protocol Port Range Source
All traffic All All 0.0.0.0/0
All traffic All All ::/0
@bravetheheat
bravetheheat / algo-security-group-inbound-rules.csv
Last active November 2, 2019 01:39
Algo AWS Security Group Inbound Rules
Type Protocol Port Range Source
Custom UDP Rule UDP 51820 0.0.0.0/0
SSH TCP 22 0.0.0.0/0
Custom UDP Rule UDP 4500 0.0.0.0/0
Custom UDP Rule UDP 500 0.0.0.0/0
exports.handler = (event, context, callback) => {
const request = event.Records[0].cf.request;
const headers = request.headers;
const user_agent = headers['user-agent'];
const host = headers['host'];
if (user_agent && host) {
var prerender = /googlebot|adsbot\-google|Feedfetcher\-Google|bingbot|yandex|baiduspider|Facebot|facebookexternalhit|twitterbot|rogerbot|linkedinbot|embedly|quora link preview|showyoubot|outbrain|pinterest|slackbot|vkShare|W3C_Validator/i.test(user_agent[0].value);
prerender = prerender || /_escaped_fragment_/.test(request.querystring);
prerender = prerender && ! /\.(js|css|xml|less|png|jpg|jpeg|gif|pdf|doc|txt|ico|rss|zip|mp3|rar|exe|wmv|doc|avi|ppt|mpg|mpeg|tif|wav|mov|psd|ai|xls|mp4|m4a|swf|dat|dmg|iso|flv|m4v|torrent|ttf|woff|svg|eot)$/i.test(request.uri);
if (prerender) {