← Project

Case study

Computravel case study: a Nuxt and Directus travel site — Truelathe

Rebuilding a travel site in Nuxt and Directus, with a Thompsons API integration, fast search and caching

Origin

I was tasked with rebuilding the Computravel website, which was built a while ago in php. There was a need for a branding revamp as well as an integration with the Thompsons travel packages API.

User flow

The site did not need to have user accounts. It needed to be a brochure of available holiday packages, featuring a very intuitive search interface, offering content by location, category and price. Users would need to be able to submit general enquiry forms as well as package-based enquiry forms.

Data analysis

I found the mapping of the data coming from the API to be extremely confusing, as the fields were named according to the Thompson's data schema which I did not have access to. and Computravel wanted to organize the data according to geographical principles rather than according to Thompsons business logic.

Stack choice

I decided to use Directus for a private backend, and Nuxt for the frontend server.

Directus is a headless CMS system, written in node, that allows you to use any database system. It sets up user management with roles and permissions, as well as full image api, alongside the app data, all configurable via an admin dashboard. One can do schema changes using the UI, but more about that later.

I find this to provide a lot of tooling along with a lot of freedom to customize and solve problems flexibly.

Nuxt.js is similar to Next.js. It is a fullstack framework built on Vue.js.
I enjoy working with Vue, and Nuxt supports SSR, file based routing and smart imports.
The SSR has helped greatly with performance, along with SWR caching and cloudflare CDN image caching.

The Directus admin dash was vital for the client in this case to manage content and keep the site up to date. But as far as updating the schema goes during development, I found that using the data model user interface, while useful, led to headaches in terms of keeping my dev environment database in step with production.

It makes more sense to either create migrations in the codebase and then run them in both environments, or to create scripts that use the directus API to update the schema that way , also in both environmnents. One could do that from local without even SSHing into the server, but that means keeping an admin token which is not a good habit ( see this blog ). So I ended up running these scripts as part of the CI custom worklow that I set up.

Architecture

Diagram of a web app deployment using the BFF pattern, with Nuxt and Directus

The above shows the architecture of computravel.
The pattern is an example of BFF ( backend for frontend) architecture, in that the Nuxt server is a proxy for all document requests and subsequent resource requests from the browser. Only binary image requests come directly from the Directus Image API. This means that there are no CORS( cross origin requests) taking place, as image requests do not contain Javascript.
The Nuxt server takes care of the caching and cache purging layer and also of shaping responses for the frontend from various directus endpoints and fields, with filtering according to the needs of the frontend. So it takes on a kind of concierge responsibility that prepares light responses for the network, after talking to the local directus server's ORM layer.
It also stores tokens scoped for the specific access required by each route. This is to prevent tokens needing to be stored in the browser.

Thinking this through was an eyeopener for me as the fact that that the frontend has no tokens is good , however one must realize that this means that anyone can talk to the server using using curl or whatever. Same origin policy in the browser will prevent requests being sent on the user's behalf, but that doesn't mean anything when it comes to an endpoint that is basically wide open by design. So this is where limiting the scope of what is offered by the public API is vital, in the case of a site like Computravel that has enquiry forms and no user login.

Performance issues

When I first put the site online, performance was really not great. I added a long fade to all routes to hide the janky page shifts taking place as the homepage was being built, which was a temporary fix to try to improve user experience and probably worsened the contentfull paint lighthouse score. What needed to be set up was :

  • server side rendered html cached for all popular pages, seamlessly hydrated with js in the browser

  • a Cloudflare CDN providing a well cached edge network. i.e. a hot image server near the user

  • the browser app ready for subsequent client navigation requests. The html gets rendered on the fly from API data that is cached. ( but cached where? Browser or Cloudflare or Nuxt or Directus?)

  • all layouts should not resize elements as they render, and also as users interact with the page

Server side caching

I chose the Nuxt server to take the responsibility of caching the rendered html. Why?
If it was in Directus, then we would be caching serialized JSONs, and the html rendering would need to be done for every request.
Doing caching in Cloudflare is possible, but then we would be creating separate caches for each data center. Meaning many users would end up getting slow responses because Cloudflare would request the page from Nuxt. Also , Cloudflare would drop caches of rarely visited pages. This would mean that computravel's 800 package pages would mostly stay cold. On the server we can choose to keep caches until a purge.

This site has no user login, so pages can be cached for all users because the server doesn't need to look at a cookie or a request header when it renders a page. The homepage takes about thirteen Directus round trips from the Nuxt server to render from cold. So this can be done only when the cache window set for the page has expired. Various pages have different cache windows depending on how fresh the data needs to be .

But if an admin updates content, then Directus needs to tell Nuxt to rerender immediately. A Directus hook watches every create, update and delete. It waits for 15 seconds without an edit, then purges Nuxt's cache once, so an editor publishing a batch of changes triggers one purge rather than twenty.

Also, we need to make sure that expired travel packages dont stay cached. Packages expire on their book_before date, so a scheduled task clears the cache at five past midnight, and destination pages stop listing deals that expired overnight.

A new container starts with an empty cache, so to get the response time snappy for all users after a deploy, I added trigger for a deploy script to warm the cache by requesting all pages on the site, except the 800-odd holiday pages, which are each visited rarely.