Never regret in life?

This age that you have, this age you will have the most enthusiasm, brain cells and hair. After some years you will neither have that much enthusiasm nor brain cells and hair. This chance that life…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Building an optimized Wordpress theme as a SPA

Webpack and Turbolinks to the rescue

We chose Wordpress for its simplicity of use, “SSR built-in feature” (👋 classic and normal web), and its admin panel… well, no need to present it.

We didn’t use the JSON API to build a full JS app for timeline reasons. I don’t think it would have been the best solution anyway: it’s really just a static website. There are no user forms or live sections… everything is static.

We started by updating our tools, using Webpack v.4 and Babel v.7.

We wanted to hot reload our application when we were developing. In our case, we had to reload the entire page every time we changed something in JS, but dynamically reload the CSS on runtime.

For the CSS, we used SASS. We had a combination of sass-loader , postcss-loader , css-loader and style-loader , all allowing hot reloading and source mapping during development.

We needed to load different files or execute different functions depending if we were in development or production mode.

From the start, we wanted to have different JS chunks depending on the current page you are on. We pushed the concept further, including the CSS needed for each page directly in each JS chunks, so it could be injected when needed.

Therefore, we created a main SASS entry point that was later compiled/optimized/minified, and injected in all the pages as CSS critical. This main CSS contains the fonts, grid system, main UI, header and footer… Everything that is in common in all of the pages.

It’s not the perfect CSS critical approach, but a relatively simple and smart solution that allows us to inject inline styles without injecting the whole application at once.

Turbolinks is a lightweight, awesome library that transforms your classic website into a SPA. What it does on a basic level is simple: It captures every click, loads the content of the page you clicked on, and swaps the html of the <body> for you.

On the JS side, it’s really simple to implement. It triggers events you can listen to, so you can have you own logic on top of it.

Turbolinks caches your HTML internally, so if you use the back button it doesn’t request the page again. It also shows a loader bar when it loads a new page (you can easily override the CSS if needed)

Before looking at the JS app, we configured Webpack so it could generate chunks:

On the front-end side, a very naïve, yet working way of implementing it is to load the correct chunk based on the Wordpress routing.

Well, not exactly. In our case, once the new HTML was injected in the body thanks to turbolinks, we looked at the id attribute of the fresh new <div> injected in our #content section, and dynamically loaded the chunk “view”.

We had to “manually” specify every single case so Webpack knew how to build the chunks on build time.

Then, in each view, we imported the SCSS we want to inject dynamically:

Easy peasy!

We also emptied the /static folder where we wanted to put all the built files used by our Wordpress theme.

Then, in our functions.php we loaded the manifest and transformed it into a php array, so we could access the correct filenames when we rendered the pages.

Not mentioned here, but we’ve implemented a lazyloading using Intersection Observer and its polyfill, used babel preset-env to generate only the needed polyfill (async…), and tested a 32 columns grid with no gutters for the first time. Works like a charm!

We’d love to step up our Wordpress game. We’re looking into full static website deployment, and using Cloudinary for the assets so the front-end could request an image depending of the user context (pixel ratio, connectivity, viewport width…)

Add a comment

Related posts:

The content ecosystem is broken!

The content ecosystem is broken! Which sounds like a bold claim is in fact the status quo in 2018. But what is the content ecosystem? Why is it broken? And why does this especially affect content…

The Swamp

Just like every other day, I find myself walking down to the Swamp. It’s overcast and chilly. I’m tired and achy and my ankles click uncomfortably loudly as I amble down the hill. I am heading to…

How to Balance Daily Life

Our lives consist of so many different strands. It can get pretty overwhelming and upsetting when we look at our lives and we realise we haven’t contacted our friends in months because we’ve been so…