Snippets

SNP 001–153

Loosely organized collection of code snippets. Maybe you'll find something useful here.

Showing 20 of 153 snippets

  1. 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 stati eta
  2. 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 js webdev
  3. 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 cmd linux
  4. 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 stati eta
  5. 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 git
  6. 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 css
  7. 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 cmd ssh linux
  8. 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 docker cmd
  9. 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 css a11y webdev
  10. 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 sql
  11. 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.

  12. 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 js gamedev
  13. 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.

  14. 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 css
  15. 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.

  16. 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 js node
  17. 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 nginx cmd
  18. 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 js node
  19. 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 ffmpeg cmd
  20. 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 js vue