I had been working on oEmbed into WP-Gistpen, so I wrote this tutorial on customizing oEmbed content from WordPress yourself. How have you been using the new oEmbed feature in WordPress?

This post is part of the thread: Personal Milestone - an ongoing story on this site. View the thread timeline for more context on this post.

Chatr: Implementing Server Side Rendering with React

In our introduction to the series, we got a simple static page rendered with express.js and handlebars. Now that we’ve got everything going, we’re going to leverage React’s server-side rendering to move from just sending some static HTML to sending a rendered component’s HTML. On the client side, we’ll bootstrap React into the rendered HTML using the same state that produced the HTML on the server, and the application can just pick up where it left off.

I was actually pretty impressed with how easy it was to get the actual server-side rendering working. However, the issue I had when I started was that I didn’t actually start with ES6 and JSX transpiling on the server to start, but it makes a lot of sense that, if your goal is to build an isomorphic JavaScript application, you should use the same syntax on both sides of the wire. Since you’re definitely going to need JSX transpiling on the server anyway, it’s easier to go all-out with the es2015 preset as well.

I got that working in the introduction to the series, even though it didn’t happen in that order in real life, so if you need a refresher, check that out. Now before we build our first React component, make sure you install react and react-dom via npm; these are the two main React tools we’re going to use in this project.

Just to get started with React, we’ll replicate the current Hello World! setup with a simple App component that takes a required headline as its props.


If this is your first introduction to React, you’ll notice the XML-like JSX syntax in the render function. This is transpiled by babel‘s JSX plugin into React calls that create the modeled DOM structure. For the most part, JSX works a like HTML, with JavaScript weaved into it, giving you a really powerful way of describing your UI state. Because it’s still JavaScript and class is a reserved word, you see above one of the many differences with plain HTML; we have to use className to give the DOM node a class. If we were to render it with headline as "Hello World!", the resulting HTML would look like this:


As for the other two props on the object: both them (displayName and propTypes) are primarily useful for development. One of React’s greatest strengths is the ecosystem of development tools that have cropped up around it; this feature comes built-in! You get console messages when the component receives props of the wrong type.

Ensuring that you’re validating your props’ values and types during development helps ensure nothing unexpected happens during production, so definitely get in the habit of defining your components’ propTypes along with its render method as part of a standard component. The React docs on "reusable components" has the full list of types and constraints you can put in your propTypes. The displayName is used in these log methods to indicate which component has the error, making it easier to debug where the problem is coming from.

This is a really simple component, but now we need to pass it props and render it. The process for doing that on the server side mirrors the client side, so let’s get it running on the server first.

In our main server.js file, we have to change the root (/) route to render this component instead of the static string we provided earlier. Here’s the new server.js code:


react-dom is React’s DOM rendering tools. These used to be bundled with React but they were split off in v0.14. They were split off from the main React package because React has ambitions beyond just the DOM, like react-native and others, so separating the packages makes sense for the project. These tools allow us to render the React components on the server as well as to the DOM; in this case, we’re using it to render the React component to a string, so the express server can send it to the client.

The first thing we do is set up the initial page state variable. As we build out the full application, this would be the point where we fetch the information from the database that’s required to render the page state. In our case, we’re just going to set our headline string.

From there, we just call ReactDOMServer.renderToString on the App component, passing in the state variable using JSX’s spread attributes to pass the object’s properties as the component’s props. This JSX syntax is modeled after ES6’s spread operator, allowing you to pass in the entire state object rather than just the props the component needs, which can be a bit more cumbersome for larger components.

Lastly, we stringify the state object so we can pass it into the view, where it’ll get received by the client code to boostrap the same React component.

On the client-side, we just need to bootstrap off the DOM node and state object we originally rendered with:


On the client side, we call the render method with the component as well as the DOM element to render onto. React will automatically pick up the fact that this is React-sourced HTML and instead of rerendering the whole page, will just attach the event listeners to the DOM, using the rendered data-reactid attributes.

From there, you can bootstrap your application however you’d like, depending upon how you choose to structure your application. In the next article, we’re going to start wiring up the RxJS streams, building a stream that will model our state as a series of messages as well functioning as a clearinghouse for all of the messages running through the client application. In this way, we’ll be able to direct those messages to and from the server and throughout the server application, with the UI just responding to state refreshes from this main stream.

This post is part of the thread: RxJS & React w/ Chatr - an ongoing story on this site. View the thread timeline for more context on this post.

CSS: Expanding a div to take up the remaining space in a row

I ran into a problem today. I had two elements next to each other on a row. I needed the first element to just be contained to the width of its child elements, while the second element needed to take up the rest of the space.

Here’s some dummy markup to get the idea:


The problem is, when I was using float left and right, I was setting the width of the second element manually using media queries, which worked at first but turned into a real bit of trouble when I attempted to make the containing element (a column wrapping the row) more responsive.

I couldn’t keep adding breakpoints and reset the size. Or I could, but it would be really brittle and would need to be updated every time the breakpoints of the containing element changed. I floundered around a bit, trying to find a solution, until I settled on this one, which relies on the oft-maligned table.

Not a <table> per se, but display: table. I have not used this feature much, since my CSS knowledge is generally pretty minimal, but there are a lot of places, like this one, where it’s going to be a lot more functional than the standard "float the div" method we’re all used to for building responsive layouts. Here’s how it works:

First, I needed to add an extra wrapper, so our HTML now looks like this:


And our CSS looks like this:


The nifty thing to notice is the .contained CSS. We set the width to be impossibly small, but tell the cell not wrap its contents. This has the effect of forcing the cell to expand to be the width of its contents, while allowing the .expanded div to take over the remaining space, achieving the effect I was looking for.

Do you know of any other uses for display: table that can’t be solved with the standard "float the div" method? Let me know in the comments.

Chatr: Exploring React & RxJS with a Chat Application

React.js has pretty well skyrocketed through the JavaScript community since its initial announcement, and with its reactive approach to UI, there’s been a growing interest in functional programming in the JavaScript community as a result. Because JavaScript allows you to pass functions around like objects, it makes it very easy to apply functional concepts to the language. Like much of the community, I’ve also been reading quite a bit about functional programming, but I haven’t had the opportunity to build a full-fledged application on these ideas.

I’ve played around with RxJS & React[^1] a little bit, but I’m not entirely sure what a full-stack architecture might look like (yet). I want to explore the possibilities by building a real-time chat application (with the uninspiring name Chatr) on Node.js with RxJS, React, & Ramda, applying functional programming to a system that can easily be modeled as a stream of messages/events.

With RxJS, I should be able pipe messages around the application, including into React to update the UI on the client side, as well as to and from a data store like Redis or a messaging queue like RabbbitMQ on the server side, with the data streaming between the server & client over a Socket.io stream. This architecture could also be inspired by Flux, ensuring all data flows into a central "store stream," which then pipes out the messages to wherever it needs to go. This primary data store stream probably could be reused on both the client & server side, which allows us to introduce some isomorphism in both our data handling & UI.

My plan is to build this in public and write about it, and see what some of the advantages and drawbacks of this approach for building web applications. You can follow along with the repo on GitHub, or keep up with the thread on this website.

Before we can get started doing anything interesting, we’ve got to get some boilerplate going. Since we want to try out some isomorphic techniques, we’re going to need to, at a minimum, get the JSX compiled on the server side. babel is the default standard for compiling JSX, and they have a very clear example for getting it running on the server. Since we’re already using it, we’re also going to bring in ES6 compilation and use that for both the server- and client-side code.

Following along with the example, we’re going to add nodemon, babel-cli, and the two babel presets we’re going to use, babel-preset-es2015 and babel-preset-react. Since we’re going to be sharing the babel configuration between the server and client, we need a .babelrc file, which babel uses to register presets and plugins:


Finally, add nodemon server.js --exec babel-node to the package.json‘s scripts key as "start", so we can run npm start to run the server. babel-node is shipped with the babel-cli package we installed, and it ensures our server-side code is compiled on-the-fly by babel and then run in node, with nodemon recompiling and restarting the server whenever our code changes.

We’re going to use express.js to handle routing and serve our static assets. Let’s create the server.js file and get some simple routes going:


Pretty basic express server; run npm start in your terminal and then go to localhost:3000. You should see a big "Hello World!". Got it? Good! Let’s get some script and style compilation going.

We’ll start with the scripts. webpack and browserify are both solid options for compiling scripts using babel. I’ve used browserify a lot more than I’ve used webpack, so I’m going to use webpack for this project. Fortunately, the configuration isn’t that complicated for a simple setup like this:


We’re not going to worry about getting any of the really complicated features setup, like hot reloading or dev servers or anything like that. Instead, we’re going start with to create a simple script that uses some ES6 to ensure that we’re compiling our scripts correctly.


If you’ve npm installed webpack, you can just run webpack in your terminal and it should spit out a main.min.js file in your public folder. Pull that up in your browser and you should see the compiled file.

On the style side, we can compile our styles.scss file with node-sass. I’m going to be including Bourbon, Neat, and Bitters for this project, as I like their mixin-only approach for its flexibility and control. Here’s the very basic styles.scss file:


I’d love to get this setup with Eyeglass as well, but we’ll start with this. Install node-sass and run node-sass styles.scss public/styles.css. We should see the CSS file rendered, compressed and compiled correctly, with a long sourcemap appended at the end.

Finally, we’re going to convert the root route to render a template for us instead of using a simple string. In this case, we’re going to register Handlebars using express-handlebars:


which we’re going to use to render a very simple template:


This is set up this way in preparation for implementing is server-side rendering! The app variable in the template context will become the rendered HTML string from React, and the state variable will be the state object that produced the given HTML. Then on the client side, we’ll pull in current state, render the React components on the current state on the page, and bootstrap the application. This is how the Redux docs suggest doing it, and since they know what they’re doing, we’re going to follow their lead.

But we’re going to wire that up in the next tutorial. For now, you should have a simple page page, rendering "Hello World!", with a CSS file and a script that outputs 3 to the console. Next, we’re going to write our first React component, render it on the server, and bootstrap our application.

[^1]: In the next version of WP-Gistpen, the settings page is built with React & RxJS.

This post is part of the thread: RxJS & React w/ Chatr - an ongoing story on this site. View the thread timeline for more context on this post.

I don’t always agree with him, but this is a very astute observation about where the Left really stands with the Democratic Party.

JavaScript reaches another level of Ouroboros.

Coates does an excellent job decrying what passes for leftist “radicalism” when it comes to race.

One does not find anything as damaging as the carceral state in the Sanders platform, but the dissonance between name and action is the same. Sanders’s basic approach is to ameliorate the effects of racism through broad, mostly class-based policies—doubling the minimum wage, offering single-payer health-care, delivering free higher education. This is the same “A rising tide lifts all boats” thinking that has dominated Democratic anti-racist policy for a generation. Sanders proposes to intensify this approach. But Sanders’s actual approach is really no different than President Obama’s. I have repeatedly stated my problem with the “rising tide” philosophy when embraced by Obama and liberals in general. (See here, here, here, and here.) Again, briefly, treating a racist injury solely with class-based remedies is like treating a gun-shot wound solely with bandages. The bandages help, but they will not suffice.