by François Vaux

Recursively invalidating require cache in Node.js

While working on Fluor.js's website, I found myself needing to reload node modules when in watch mode in order to update components used by the generated pages (I'm using Server Side Rendering with React to build the website).

Here's the snippet I used to recursively invalidate a module's cache entry and all its dependencies:

function removeFromCache(moduleId) {
  const cached = require.cache[moduleId]

  if (!cached) {

  // LIB_ROOT and SITE_ROOT are where my source files reside. I avoid reloading
  // node_modules as it causes some issues with the rendering process
  const ownModule = (mod) =>
    mod.path.startsWith(LIB_ROOT) || mod.path.startsWith(SITE_ROOT)

  cached.children.filter(ownModule).forEach((mod) => removeFromCache(
  delete require.cache[moduleId]

Five-line TailwindCSS-based styled components

I recently discovered TailwindCSS and I think it's a great tool to be able to quickly build fancy websites without spending too much time fiddling with the CSS.

I came up with a five-line React component that let's you write styled-components-like components using Tailwind class names:

import React from "react"
import clsx from "clsx"

export default (Component, ...tailwindStyles) => ({ className, ...props }) => (
  <Component className={clsx(...tailwindStyles, className)} {...props} />

(I know, that's actually 6 lines with the blank one 😏)

Usage is very simple as well:

const Warning = styled("div", [

Bonus points for the regular syntax instead of the funky (read: ugly) mix of template strings and CSS that you're used to with styled-components.

EDIT a few hours later: I realized I was missing the ability to derive styles from props, so I added it!

import React from "react"
import clsx from "clsx"

function isFunction(object) {
  return Boolean(object && object.constructor && && object.apply)

export default (Component, ...tailwindStyles) => ({ className, ...props }) => {
  const resolvedStyles = =>
    isFunction(style) ? style(props) : style,
  return <Component className={clsx(...resolvedStyles, className)} {...props} />

// Example usage:
export default styled(
  (props) => [
    props.disabled ? "bg-green-200" : "bg-green-400",
    props.disabled ? "text-green-500" : "text-blue-900",
  (props) => ({
    "hover:bg-green-300": !props.disabled,
    "hover:text-green-800": !props.disabled,
    "cursor-not-allowed": props.disabled,

Thinking in React Hooks

React Hooks are a great way to replace class-based components with functional ones in React. This article from Amelia Wattenberger gives an in depth comparison between both approaches, with a lot of helpful visualizations.

The Ultimate Guide to handling JWTs on frontend clients

A great article about how to properly handle JSON Web Tokens in frontend applications.

How I ditched Redux

Update March 2020: Use useReducer!

Redux is probably the most used state management library for React. However, even its own creator has trouble using it, probably because it is one massive piece of bloat and boilerplate.

I used to solve that problem using elfi, a dead simple state management library that I wrote a few years ago. Elfi is a 50 LOC lib that does not carry any difficult to grasp concepts like reducers, thunks or whatever, but instead only relies on functions.

But given the recent additions to React, even elfi is useless now. Using React Contexts, I simply implement state management in my top level component and pass the state management functions through the context:

// AppContext.js
export const AppContext = React.createContext()

// App.js
function App() {
  // The global application state. We use an object here but an
  // immutable value is also a pretty solid choice
  const [state, setState] = useState({ counter: 0 })

  // The dispatch function updates our state by applying a given
  // function on the previous state
  const dispatch = (fn, ...args) => setState(fn(state, ...args))

  return (
    <AppContext.Provider value={{ state, dispatch }}>
      <Button />

// Button.js
function Button() {
  const { state, dispatch } = useContext(AppContext)

  return <button onClick={() => dispatch(increment)}>{state.counter}</button>

function increment(state) {
  return { ...state, counter: state.counter + 1 }

Try it out on CodePen