Skip to content

Instantly share code, notes, and snippets.

View wilsonpage's full-sized avatar

Wilson Page wilsonpage

View GitHub Profile
@wilsonpage
wilsonpage / swr.ts
Last active April 22, 2024 08:58
An implementation of stale-while-revalidate for Cloudflare Workers
export const CACHE_STALE_AT_HEADER = 'x-edge-cache-stale-at';
export const CACHE_STATUS_HEADER = 'x-edge-cache-status';
export const CACHE_CONTROL_HEADER = 'Cache-Control';
export const CLIENT_CACHE_CONTROL_HEADER = 'x-client-cache-control';
export const ORIGIN_CACHE_CONTROL_HEADER = 'x-edge-origin-cache-control';
enum CacheStatus {
HIT = 'HIT',
MISS = 'MISS',
REVALIDATING = 'REVALIDATING',
@wilsonpage
wilsonpage / copyToClipboard.js
Created August 9, 2019 13:21
iOS Safari copy to clipboard
const fallback = (text) => {
const isIos = navigator.userAgent.match(/ipad|iphone/i);
const textarea = document.createElement('textarea');
// create textarea
textarea.value = text;
// ios will zoom in on the input if the font-size is < 16px
textarea.style.fontSize = '20px';
document.body.appendChild(textarea);
@wilsonpage
wilsonpage / module.js
Created March 3, 2012 17:55
How to define a module for both AMD and CommonJS compatibility
/**
* Registers module as CommonJS or AMD if support is found
* and creates an exports object for the module to use
*
* @param {boolean} cjs
* @param {boolean} amd
* @param {Object} ex
* @return {Object}
*/
var exports = (function(cjs, amd, ex){
@wilsonpage
wilsonpage / storage.js
Created September 11, 2015 14:25
Simple localStorage like wrapper around indexeddb
function Storage(name) {
this.ready = new Promise((resolve, reject) => {
var request = window.indexedDB.open(location.origin);
request.onupgradeneeded = e => {
this.db = e.target.result;
this.db.createObjectStore('store');
};
request.onsuccess = e => {
@wilsonpage
wilsonpage / reqUrl.js
Created November 25, 2011 14:38
A function I made that wraps the node http.request function to make it a little more friendly. In my case I am using it for API route testing.
// module dependencies
var http = require('http'),
url = require('url');
/**
* UrlReq - Wraps the http.request function making it nice for unit testing APIs.
*
* @param {string} reqUrl The required url in any form
* @param {object} options An options object (this is optional)
@wilsonpage
wilsonpage / HydrateScope.tsx
Last active November 17, 2022 09:40
React SSR hydration boundary
import { cloneElement, FC, ReactNode, useRef } from 'react';
import { getAppHasHydrated } from '../MyApp';
export interface HydrateScopeProps {
hydrate: boolean;
render: () => JSX.Element | ReactNode;
}
const IS_SERVER = !process.browser;
@wilsonpage
wilsonpage / app.js
Created November 18, 2020 12:01
How to create a performance measurement that shows on devtools timeline
performance.mark('myMark-start');
// your code
performance.mark('myMark-end');
performance.measure('myPerfMarker', 'myMark-start', 'myMark-end');
import { isIos } from '~/lib/device/utils';
const fallback = (text: string) => {
const textarea = document.createElement('textarea');
// create textarea
textarea.value = text;
// ios will zoom in on the input if the font-size is < 16px
textarea.style.fontSize = '20px';
@wilsonpage
wilsonpage / spotify-markets.json
Last active October 18, 2020 11:30
A JSON list of all Spotify markets (ISO 3166-1) as per https://www.spotify.com/us/select-your-country/
[
"AD",
"AR",
"AU",
"AT",
"BE",
"BO",
"BR",
"BG",
"CA",
import { useRef, CSSProperties } from 'react';
import withSpacing, { WithSpacingProps } from '~/lib/hocs/withSpacing';
import useTestIdAttr from '~/lib/hooks/useTestIdAttr';
import { getAspect } from './utils';
import { ImageProps } from '.';
interface BackgroundImageProps extends ImageProps, WithSpacingProps {
gradient?: string;
borderRadius?: number | string;