# Headless Wagtail Strategies

Source: https://djangotv.com/videos/wagtail-space-nl/2024/remy-sanchez-headless-wagtail-strategies/
Date: 2024-06-27
Speakers: Rémy Sanchez

## Transcript

### [00:00] Introduction to Headless Wagtail

[00:09] Yep, here we go. All right. Hello everyone. Thanks so much for coming. So I'm going to talk about headless workl. That is I guess a hot topic. and that has probably different solutions so I'm going to talk about which one we retained. So I'm Remy, I'm co-founder at WIT, we are a web agency in Madrid. We have about 40 people and we work for lots of brands to create like kind of public-facing websites. that are going to be having you know some deep integration to their system but usually the content is a dependency of what we do so we need let's say a go-to

[00:54] solution to be able to have content management for those websites And obviously that is Wagtail. So first of all, I'm I'm going to open already open doors, right? But what is headless, just to be sure on the definition. The idea is that you're going to put in between your browser and your work tail a front-end framework like Nuxt, React, whatever. And the CMS becomes a repository for the content and then it's available through some kind of a API and then it's rendered by some other framework for that's more dedicated to frontend. And it's definitely something hot, you know, in the web development. Lots of customers ask is from us.

### [01:40] The Case for Headless Development

[01:40] But is it really a good idea? Like because I know I I've had the debate many times with people that are like come on, it's this JavaScript thing is it's not for me, I don't like it. And that's perfectly understandable, but Let's say my perspective is that I have an agency to run, right? So if you look at the cost of different things for me It's switching people from project to project, configuring different infrastructure for different projects. I have many, many projects to run at the same time, right? And so the JavaScript bundle size is cost zero in comparison to everything else So if using a front-end framework solves you know the ability to move on different things it's

[02:27] It's pretty important. So It's important for me that sales project managers, UX designers and so on, they know what to do and what to offer to the customers. They also, you know, know how the pricing works. And obviously the price for this kind of stuff is unfortunately not the same you know added value as it was ten years ago so we have lots of juniors working on this we don't need to be we need to be able to get them working without training them for too long And of course we need to consider inside the agency that the talent pool is uniform. So if we have many different people frameworks. The people we need to train them from thing to thing and we cannot switch, you know, like if there's an origin

[03:12] bunk, not everybody can jump on the project and save it. And of course the customer is always right in matters of features. Like if they ask for something, we say yes because otherwise they go somewhere else, right? But project might start simple and ten years later you end up with a huge pile of different features that you did not anticipate and that can blow up. So If you have a stack that does not allow this, you will end up let's say stuck with two choices. Either you decide to go with some hack like band the stack over and make it work somehow but then you create technical depth very quickly

### [03:58] Choosing Wagtail and Nuxt

[03:58] Or you need to rewrite everything to another stack that works for you. So bottom line is I'm really looking for a full stack that will allow me to do everything that the customers always ask me to do and that is for the back end wagtail because few reasons that you probably all know but the Django models. And the migration system is amazing and it allows me to have you know a lot of guarantees on my data, the stream fields that I use a lot, probably a lot more than intended the ability to fall back to Django for anything that Rectel does not support, so that is you know great because it's not WordPress that exists just to create content. It's a

[04:44] full framework behind. It is secure and it is defendable. We have lots of security audits. It's very easy to say yeah we use Wagtail, it's a secure solution. State of the art, blah. And of course the talent pool you can find people that are able to work in Wagtail or at least in Django and upgrade them very quic quickly to Wagtail And again in front of customers it's very important. Why SSR framework like next in our case? Because the it's the same template in the front end and the back end. So you get like classical type SEO. and performance because the content is already generated as opposed to SPA where you need to download the JavaScript and then generate the content.

[05:31] You can have any kind of interactivity you want. So if the customer goes, yeah sure now can we turn this into a Facebook like comment section where you can like the things and so on? It's very annoying to be doing the template in jQuery and mimic it's the template from the Django and so on. So It's very important to have the same on both sides. Comes at the cost of smashing the CPU and the network a little bit, but it's perfectly acceptable And also it solves some hard work problems that are the organization of the code which I guess is a matter of taste, but single file components in front end from ox basically you put javascript html and css in the same file

[06:17] or together and basically it means that if you delete this component, you delete all the dependencies of this component I mean you delete all the content of this component from one file. You don't have to go find it in your view file, in your template file, and so on and so on. And vice versa, when you dynamically include the dependencies. So the compilers are able to make the tree and make bundles that are optimized for exactly what you are going to display. So if you have a homepage you're going to statically to load you know just the content of this homepage and then when you have a mapping component that is taking one megabyte of JavaScript. Then it will only be loaded when you go on the map page and not in the whole javascript bundle.

### [07:03] Headless Integration Challenges

[07:03] And doing this with just Wagtail is a bit cumbersome. I mean it's it's more you know bring your own solution to this usually. So what's the catch? Well the catch is First thing is routing because voices routing is kind of hard-coded. This is problematic for regular pages, but most importantly is very problematic for previews because the rules for previews are a bit weird and they are also session based so that is not intuitive how to make it work And overall Wagtail is already messing a little bit with Django's routing, so adding some more layers on top of that is not a great idea.

[07:50] I am not using Wagtail Headless Preview, that is a package that is made for this. For several reasons. You'll see that basically the solution that I offer that does not need it. Because it is token based instead of session based so you need more awareness in the in your code you need to pass the token around and you need like all the parents of the code need to know what they are doing and I wanted to avoid that. The second big problem that I have is the images, okay, because the front end must take image decisions because it's the only one that knows the size of the image, how it wants to display it what are the different formats and variants of this image. You want to apply filters and so forth and so forth.

[08:38] And So that really depends where the image is displayed, you don't know in advance where this image is going to be, so you cannot pre compute the formats and so on But on the other hand, the front end cannot take image decisions because of the security risk. If someone asks for the same image of one pixel, two pixel, three pixel, and so on. you can very easily smash down the server by asking a million random formats. So and there's two solutions for this but it's it's always generated by the server right you can either do the render from the server and then give the url or use the dynamic render url but it's it's the same issue. So I scratched my head a little bit when I had to make my first you know

### [09:23] The Front End as a Proxy

[09:23] headless project and figured this. So for context it was conversion of classical work tail project into headless one. So in the end you need to consider that the front end is a proxy, right? Like you can use to give an example you can use Cloudflare to compress your images. So all the JPEG images are going to be compressed into WebP dynamically by Cloudflare. So the same URL is just content transformation so why not do the same thing with the with the front end, right? So the browser will ask for this page to next. Hello can I get slash foo And then Nux will ask from Wagtail can I get slash foo?

[10:09] Then Wagtail returns the content and Nuxt transforms this content into HTML. That is properly you know presented with CSS and so forth. So that that's kind of the mentality that allows you to also solve things like okay if there's a redirection on this URL then you proxy it. So you like you reply what the server is saying. If the server says Redirect you have a direct if it's an error, it's an error, etcetera. So it's making it a lot more simple to see it as just a proxy So like basically you make a catch all a route on your front end framework and everything that is asked you proxy through the other advantage is that when you do this, you

[10:56] are on the same domain for Django and for the next. So the cookies they go through. And there's never need for calls you can call the API as much as you want. There's only one trip to the API in a two, etc. etc. So it's really simplifying things a lot. So basically you just say everything that is I don't know slash API goes to the API and everything else goes to the front and that's it. Previews. are a little bit tricky but not that hard in the end. So how does it work? Like basically when you make an edit in a Wagtail it's going to first make a post to the preview URL to send the new content. And then Wagtail is going to store it into the session and then is

[11:41] going to make a get, like the you know the live preview on the site, it's going to make a get. on the page to display the preview. So it's pretty easy in the end. With like for example I use a HTTP proxy middleware that I put in the VID server that is you know rendering next and so forth. I have a regular expression to catch preview pages and if it's a post I send it directly to Wagtail and otherwise I put it through the rendering process. So that is actually relatively easy to do and with that you get the session-based previews. They are rendered with exactly the same other code as the rest of the website and basically Wagdale and Nuxta are both oblivious

### [12:27] Server-Templated Components

[12:27] to this except for a few lines of code here and there Where I lied a little bit is that the content is HTML, right, in a previous example. So I have different strategies, but that's the first one. You're going to say it's a bit wet, but bear with me. So the idea is that as I said, the first project that we did was a migration So we already had the templates. We wanted to just redesign things a little bit and make it make it server headless. So what we do is that we do single file components but without the template and the template comes from the webtail page. So basically instead of serving JSON

[13:14] From the API we serve HTML from the API that is the regular template. Actually we did not even modify the Wagtail source code for this thing. We just made JavaScript that was able to you know pass this HTML and inject it as a template into the view single file component and like generate dynamically the components with the template from the server and javascript from compile time. And that is that gets rendered into DOM. So basically it's it's kind of a hack some would call me an abomination, but pros and cons, right? Pros

[13:59] it works. So As long as it works, right? It is workable in the sense that it's not creating tremendous amounts of technical depth. You can you can deal with it, it's not blowing up over time. It's it scales without being annoying. I mean without getting more annoying than it is initially. It's lowering kind of the cost of converting an existing project into a next project. And you can still get the core web vitals passing, etc. etc. So that's really not that bad. It's kind of trade-off that sounds a bit demonic, but it's it's okay. Cons though is that it's really getting hard on the CPU because you need to compile all those templates at load time.

[14:46] So that's kind of annoying. It's not extremely future proof either because it's kind of hacky, you know, so it's really on the edge. And the last thing is that You need a runtime compiler, so typically for Vue it works, but for Svelte or React I guess not because they are compiled at compile time and Vue is you know initially parsing HTML itself So that's a way to go, but that's not the definitive way to go, I would say. So another strategy that we are developing is to serialize the pages into JSON using the serializer, the page serializer from Rugtail that is pretty easy to use. We override the serve method to return JSON instead of HTML.

### [15:33] JSON Serialization and Images

[15:33] So we like we just call the stylizer. render the page according to this JSON and put the right components and so on. The only issue is the images, right? So how do we solve the images because we still don't know the formats in advance. So by thinking about it a little bit more, turns out that you need about six, seven formats that you can statically define. Because these days it's not, you know, like there's all the screen densities, screen width, and so on and so on. And so basically at the end of the day you cannot predict the formats that you're going to need anyway. So you kind of need to take a probabilistic approach to say, okay, if I have seven sizes, is

[16:20] going to be there's always going to be one size that is ten percent away from the size I really need and that's going to be good enough. And you could think also that is preventing you from cropping and zooming and so on. But on the other hand it's an opportunity to do it in the front end. So instead of having the cropping done statically in the back end, you can have the cropping done in a responsive way. So when you rescale, you know the page is still cropped according to the feature from the WagClear thing. So that will require to develop a specific component, but it's also a pretty good opportunity to like improve the image management. So as a conclusion the front end is a proxy

### [17:06] Conclusions and Open-Source Tools

[17:06] That's really the core idea behind this approach is to consider that it's just a pass-through with some modifications on the content. So we kind of give style to the worktail content There's two approaches to this. There's the server templated component approach that is kind of a hack but definitely works. So you send HTML, you transform the HTML into better HTML, let's say with JavaScript and CSS. And there's the API-based approach that is overriding the serve method and that uses a fixed set of images to do the rendering. This whole thing is open source, so the documentation is not excellent probably because it's an entire project, but we're trying to improve it at every release.

[17:57] We have what we call a model W that is you know the same as model T you can use any framework as you want as long as it's the same as in the model W. The idea is that we use this internally at the NGC because everybody can knows it and we have tooling around this. So basically you can check it out on GitHub and try it. And you can have the project maker. So that with this one liner it starts an assistant to basically create a project that is already scaffolded with a next and wagtail and that allows you to have like everything that I just explained with the server templated components That is already running, so you just need to start coding basically like all the modifications have been

[18:47] have been made. And the Zvel -Kit version that is going to be dealing with JSON instead of HTML. We're going to release it in October. So it will you know be released there. So we like we release every four months. So it's not going to make the cut for July but it's definitely going to make the cut for October. So thank you very much. I don't know if anybody has questions about this. I think we have thirty seconds more.
