Snippets
SNP 001–153
Loosely organized collection of code snippets. Maybe you'll find something useful here.
Showing 20 of 153 snippets
-
SNP053
Partials with props in Stati
A card component that takes data, and the one line that keeps it from breaking every page.
9 lines of eta in 2 blocks, 22 lines of explanation with 4 notes.
#359 -
SNP052
Fetch with a timeout
fetch() waits forever by default. Give up after a few seconds.
6 lines of js in 2 blocks, 4 lines of explanation.
#25A -
SNP051
Crontab cheat sheet
The five fields, the shortcuts and the two mistakes everybody makes.
11 lines of sh in 3 blocks, 5 lines of explanation.
#941 -
SNP050
Eta tags in Stati templates
The three tags, the one variable, and the comment that breaks a template.
22 lines of eta in 2 blocks, 18 lines of explanation with 3 notes.
#B14 -
SNP049
Named stashes
stash@{3} means nothing to anyone. Give your stashes names.
4 lines of sh in 3 blocks, 5 lines of explanation.
#624 -
SNP048
Truncate text after a few lines
Clamp a description to three lines with an ellipsis, in CSS only.
11 lines of css in 2 blocks, 4 lines of explanation.
#31B -
SNP047
Copy files with rsync over SSH
Like scp, but it only sends what changed, and it can resume.
1 line of sh in 1 block, 9 lines of explanation with 4 notes.
#459 -
SNP046
Open a shell in a running container
Poke around inside a container to see what it actually looks like in there.
3 lines of sh in 3 blocks, 4 lines of explanation.
#B27 -
SNP045
Respect prefers-reduced-motion
Some people get dizzy from your fancy animations. The browser tells you who, so listen.
10 lines of css and js in 2 blocks, 6 lines of explanation.
#164 -
SNP044
Latest row per group in SQL
The newest order of every customer, in one query.
14 lines of sql in 2 blocks, 5 lines of explanation.
#A35 -
SNP043
Create a new user with sudo rights on Ubuntu
Stop working as root on a fresh server.
3 lines of sh in 2 blocks, 7 lines of explanation.
#591 -
SNP042
Shuffle an array properly
The sort(() => Math.random() - 0.5) trick is biased. Fisher-Yates is just as short and actually random.
9 lines of js in 1 block, 5 lines of explanation.
#2B4 -
SNP041
Create a swap file on Ubuntu
Small VPS, no swap, and npm install gets killed. Give it some room.
7 lines of sh in 3 blocks, 2 lines of explanation.
#369 -
SNP040
Hide the scrollbar but keep scrolling
For horizontal carousels and tab bars where the scrollbar is just noise.
8 lines of css in 1 block, 5 lines of explanation.
#14B -
SNP039
MariaDB in Docker for local development
A database for the project that you can throw away and recreate in a second.
17 lines of yaml and sh in 2 blocks, 4 lines of explanation.
#952 -
SNP038
Read a JSON file in Node
Two ways, one of them without any fs code at all.
4 lines of js in 2 blocks, 4 lines of explanation.
#416 -
SNP037
Redirect www and http to https in nginx
One canonical address for your site, everything else gets a 301.
23 lines of nginx and sh in 2 blocks, 1 line of explanation.
#529 -
SNP036
Sleep in Node without writing a timeout function
Node ships a promise-based setTimeout, so the little helper from my old snippet isn't needed there.
6 lines of js in 2 blocks, 6 lines of explanation.
#B63 -
SNP035
Trim a video without re-encoding with ffmpeg
Cut a clip out of a long recording in a second, with no quality loss.
2 lines of sh in 2 blocks, 4 lines of explanation.
#397 -
SNP034
Watch a nested property in Vue
Your watcher doesn't fire when something deep inside an object changes. Here is why and what to do.
2 lines of js in 2 blocks, 6 lines of explanation.
#6B1
No snippets match this filter.