Skip to content

Instantly share code, notes, and snippets.

@jamesarosen
jamesarosen / never-next-js.md
Last active January 15, 2025 05:13
Why Next.js is My Least Favorite Framework

Not every web project needs a web framework, but when I pick a framework for a project, the main thing I want from it is to do the things that are hard to get right and that nearly every application needs. For me, the gold standard is Ruby on Rails. Out of the box, you get

  • database connections for development, test, and production
  • database migrations, expressible in either Ruby or SQL, and with up and down support
  • sessions that are secure by default, load automatically on first read, and write automatically on change
  • the Rack API and the full power of community middleware
  • asset compilation, minification, and caching

And that was all in 2012.

@jamesarosen
jamesarosen / ember-xss.md
Created October 28, 2015 16:50
Ember and XSS Safety

TL;DR

In Ember, always use {{...}}, not {{{...}}}. Use Ember.String.htmlSafe as necessary in JavaScript (usually in a component) to mark markup as HTML-safe. Never pass user-entered content directly to Ember.String.htmlSafe.

Details

Ember has great XSS protection built in. The HTMLBars templating library will automatically run any interpolations through htmlEscape for you. So

@jamesarosen
jamesarosen / upgrading.md
Last active August 2, 2024 09:51
Upgrading to ember-qunit 0.3.2

Recently, I upgraded from ember-qunit 0.3.1 to 0.3.2. The main reason I wanted to upgrade was to take advantage of a new way of writing component tests.

Previously, there was no way to test how the component was actually used. A component test could instantiate a component and poke at its internals, but that isn't how we use components. We use them declaratively in templates. The old style also meant it was difficult to test block templates and impossible to test block parameters.

I really wanted the new test style, but it wasn't easy to get there. The following are my upgrade notes.

Gotcha: moduleForComponent Requires Three Arguments

Many of my old component tests looked like

@jamesarosen
jamesarosen / helper-functions-in-redux.md
Last active June 8, 2023 16:45
Helper Functions in the Redux Store

I have a function that generates image URLs. This function combines some relatively static global configuration with some dynamic data that changes on every invocation. I say "relatively static" because the configuration is loaded asynchronously during the application boot, but remains fixed after that.

Option One

export default async function imageUrl(imageId, { size = 'normal' }) {
  if (imageId == null) return null
  
  const constantsResponse = await fetch('/api/constants')
 const imagesRoot = constantsResponse.json().imagesRoot
@jamesarosen
jamesarosen / git_extraction.md
Created October 24, 2012 16:13
Extracting a directory from a git project

Recently, I needed to extract a number of directories from a git project each into their own projects. The original directory structure looked like

project/
  subprojects/
    Foo/
    Bar/
    Baz
@jamesarosen
jamesarosen / addServerTiming.ts
Created October 20, 2022 01:54
addServerTiming is a node function that wraps a sync or async function and adds server-timing headers with the elapsed time.
/**
* @example
* const users = addServerTiming(responseHeaders, 'LU', () => db.fetch('users'))
* @see https://web.dev/custom-metrics/?utm_source=devtools#server-timing-api
*/
export default function addServerTiming<T>(headers: Headers, label: string, callback: () => T): T {
const start = Date.now();
const maybePromise = callback();
if (maybePromise instanceof Promise) {
@jamesarosen
jamesarosen / two-travis-builds.md
Last active June 5, 2021 18:39
Running Two Very Different Travis Builds

I have a project that's been happily chugging along on Travis for a while. Its .travis.yml looks something like

script:
  - node_modules/ember-cli/bin/ember test

I wanted to add a second parallel build that did something very different. I didn't want to run ember test with a different Ember version or some other flag. I wanted to run a completely different command. Specifically, I wanted to run LicenseFinder's audit.

Travis has great docs on customizing parallel builds, but nothing describes how to do two completely different commands.

@jamesarosen
jamesarosen / braintree.tf
Created May 13, 2021 16:55
I'm trying to fetch and parse some JSON in Terraform using jq and I just can't figure it out.
# Braintree publish a list of their CIDR blocks and IP addresses at
# https://assets.braintreegateway.com/json/ips.json
# The body looks like
# {
# "production": {
# "cidrs": [
# "1.2.3.4/5"
# ]
# "ips": [
# "6.7.8.9"
@jamesarosen
jamesarosen / ember-data-notes.md
Created March 6, 2015 19:48
Ember-Data Notes

Ember Data Notes

A Little Background

These are some notes on the workings of Ember-Data. They written from the perspective of migrating a Backbone app to Ember. Below, "Sierra" is the Backbone app, while "Tango" is its successor.

Store

  • central clearinghouse of data
  • singleton
@jamesarosen
jamesarosen / if_less_development.md
Last active July 19, 2020 19:53
On Procedural Polymorphism: I like the idea of replacing ifs with Polymorphism, but I'm not quite comfortable with it yet.

Background

Cory Foy recently wrote an article entitled, Procedural Polymorphism: Is your code really telling you to use an if statement?. Corey Haines has been talking about this same topic for a while. Sandi Metz covers the topic in Practical Object-Oriented Design in Ruby. Ben Rady and Rod Coffin discussed using the Null-Object pattern instead of an if statement in Continuous Testing with Ruby, Rails, and JavaScript.

Printing in Conway's Game of Life

That is, very smart people whom I admire agree that, in general,

class DeadCell