Skip to content

Instantly share code, notes, and snippets.

View djm's full-sized avatar

Darian Moody djm

View GitHub Profile
@gaearon
gaearon / 00-README-NEXT-SPA.md
Last active May 4, 2024 12:36
Next.js SPA example with dynamic client-only routing and static hosting

Next.js client-only SPA example

Made this example to show how to use Next.js router for a 100% SPA (no JS server) app.

You use Next.js router like normally, but don't define getStaticProps and such. Instead you do client-only fetching with swr, react-query, or similar methods.

You can generate HTML fallback for the page if there's something meaningful to show before you "know" the params. (Remember, HTML is static, so it can't respond to dynamic query. But it can be different per route.)

Don't like Next? Here's how to do the same in Gatsby.

@AndrewIngram
AndrewIngram / letting_agent.py
Last active January 24, 2023 14:41
Django Queryset capabilites example
"""
This snippet will return all the letting agents, bucketed into discrete bands based on distance from
an origin point (for example, the user's current location), then sorted alphabetically within each
band. The goal being to prioritise agents in close proximity, but not excessively so -- all agents
within the same band should be given equal distance-related priority.
If there's a search term, we then boost the distance score based on the name's similiarity to the
search query.
"""
qs = LettingAgentLocation.objects.select_related("agent").filter(postcode__isnull=False)
@bennylope
bennylope / migrate.py
Last active July 18, 2023 05:43
PostgreSQL migration script, Heroku -> Crunchy
#!/usr/bin/env python
import argparse
import os
import subprocess
import sys
import time
# Required so we don't generate tons of logs during restore
disable_logging_sql = "ALTER USER postgres RESET pgaudit.log;"
@AndrewIngram
AndrewIngram / example.sql
Last active October 28, 2021 19:26
Batch pagination
-- This is one technique for batching up the *first* page of results when doing pagination.
-- Potentially useful when using GraphQL and Dataloader.
--
-- In this example, we return the first 10 books whose author_id is 1,2,3,4 or 5 and genre
-- is "biography", ordered by "title"
--
-- For cursor-based (keyset) pagination, this technique won't work for anything after the
-- first "page", because the where clause needs to be the same for every entry in the
-- batch, which means you can't use different cursors. In practice, there isn't usually a
-- need to batch up subsequent pages, because your "next page" GQL queries will typically
@kepano
kepano / obsidian-web-clipper.js
Last active May 3, 2024 07:21
Obsidian Web Clipper Bookmarklet to save articles and pages from the web (for Safari, Chrome, Firefox, and mobile browsers)
javascript: Promise.all([import('https://unpkg.com/turndown@6.0.0?module'), import('https://unpkg.com/@tehshrike/readability@0.2.0'), ]).then(async ([{
default: Turndown
}, {
default: Readability
}]) => {
/* Optional vault name */
const vault = "";
/* Optional folder name such as "Clippings/" */
@dhh
dhh / tracker_blocking.rb
Last active July 27, 2023 14:19
Current list of spy pixels named'n'shamed in HEY, as of April 23, 2020
module Entry::TrackerBlocking
extend ActiveSupport::Concern
included do
has_many :blocked_trackers
end
email_service_blockers = {
"ActiveCampaign" => /lt\.php(.*)?l\=open/,
"AWeber" => "openrate.aweber.com",
@srfrog
srfrog / cuid.sql
Created November 27, 2019 03:49
CUIDs for PL/PgSQL
-- Collision-resistant ids optimized for horizontal scaling and performance, for PL/PgSQL.
-- Based on https://github.com/ericelliott/cuid
-- Version 1.0.0
-- Usage: SELECT cuid();
-- BEGIN CONFIG ---
-- Put a unique host ID (int) here per server instance.
-- Once set, this value should not be changed.
@kjagiello
kjagiello / Dockerfile
Last active February 1, 2021 09:46
An example Dockerfile showing how to build Facebook's watchman for your application (a life saver when running Django locally)
# Stage: watchman
# Build watchman as a separate stage in the build process, as we do not want
# all the build dependencies to end up in the final image.
FROM python:3.7 AS watchman
ARG WATCHMAN_VERSION=v4.9.0
ENV WATCHMAN_VERSION=${WATCHMAN_VERSION}
WORKDIR /tmp
defmodule Test do
def thing() do
1+1
end
defmacro my_macro do
quote do
1 + 1
end
end
@ddahan
ddahan / pipenvdiff.py
Created August 6, 2018 11:18
Compare two Pipfile.lock files
# Python 3.6 min
import json
import re
import argparse
# CONSTS
DEFAULT = 'default'
VERSION = 'version'