Wagtail headless and NextJS frontend - Lucas Moeskops

This video features Lucas Moeskops at Wagtail Space NL 2022 in Arnhem, Netherlands.

Wagtail headless and NextJS frontend - Lucas Moeskops
0:26:32
Published June 30, 2022
1,013 views

Summary

Lucas Moeskops explains how to use Wagtail as a headless CMS with a Next.js and React frontend, using the Wagtail Bakery demo as a practical example. He covers catch-all routing, page-type templates, StreamField and rich-text rendering, responsive images, caching, static generation, preview and admin features, and forms. He argues that Wagtail’s API already supports much of this integration, but would benefit from improvements such as better rendition and rich-text handling, along with a reusable Wagtail–Next.js package; forms, internationalisation, routable pages, and cache invalidation still need further work.

Key takeaways

  • Next.js provides a React-based frontend with useful support for SEO, image optimisation, and static or server-side rendering.
  • A catch-all Next.js route can ask Wagtail for the page associated with a requested URL, allowing editors to control the site structure.
  • Wagtail page types and StreamField blocks can be mapped to React components, while rich text requires conversion to handle internal links correctly.
  • Next.js image handling works well with Wagtail renditions, including Wagtail’s focal-point cropping, although external image services require additional integration.
  • Incremental static generation can provide fast pages, but Wagtail needs to trigger cache invalidation when content changes.
  • Forms, routable pages, internationalisation, preview support, and a general-purpose integration package remain areas for further development.

Summarised automatically from the transcript.

Chapters

  1. 0:00 Headless Wagtail and Next.js Lucas introduces the case for a headless Wagtail CMS and a Next.js frontend for design-focused websites.
  2. 2:18 Next.js Advantages The talk covers React, search-engine optimization, image handling, and the practical benefits of Next.js.
  3. 3:04 Integration Approaches Lucas compares existing Wagtail and Next.js projects and explains his goal of a more flexible integration.
  4. 6:08 Rendering and Caching Strategies The presentation outlines the main integration challenges, including routing, rendering modes, incremental static generation, and caching.
  5. 8:26 Page Routing The coding demonstration shows how Next.js catch-all routes can use the Wagtail API to resolve requested pages.
  6. 10:43 Templates and Page Metadata Lucas explains page rendering, SEO and Open Graph metadata, template mapping, and dynamic component loading.
  7. 13:46 Stream Fields and Rich Text The talk demonstrates querying nested Wagtail stream fields, mapping blocks to React components, and converting rich text.
  8. 16:52 Responsive Images Lucas shows how Next.js image handling can work with Wagtail renditions, focus points, and external image loaders.
  9. 19:36 Caching and Revalidation The presentation compares server-side rendering with static generation and describes invalidating generated pages when Wagtail content changes.
  10. 21:57 Preview, Admin Tools, and Forms Lucas discusses draft previews, the Wagtail admin user bar, and the remaining challenges of integrating forms.
  11. 23:30 Summary and Future Work The talk summarizes improvements needed in the Wagtail API and identifies future work around forms, routable pages, internationalization, and caching.
  12. 25:27 Questions Lucas answers questions about the Wagtail REST API and interoperability with other frontend frameworks.

Questions this talk answers

Why use Wagtail as a headless CMS with Next.js?

A headless setup separates backend and frontend logic, allowing multiple applications to use the same CMS and API while making redesigns easier. Next.js also provides React’s ecosystem along with built-in support for SEO, images, and other common website needs.

Discussed at 0:47

How do you route Wagtail pages in a Next.js frontend?

Use a catch-all Next.js route that captures the requested path, sends that URL to the Wagtail API, and then renders the page returned by Wagtail. This lets Wagtail control arbitrary page and URL structures.

Discussed at 9:12

How do you map Wagtail page types to React templates in Next.js?

Map each Wagtail page model or template name to a corresponding React component, then render the matching component for the returned page. Next.js can load only the code needed for the selected page type.

Discussed at 11:30

How do you render Wagtail StreamField content in Next.js?

The Wagtail API can return nested StreamField data, including each block’s type and value. A block-to-component mapping lets the frontend loop over the stream and render the appropriate React component for every block.

Discussed at 13:46

How do you render Wagtail rich text in React and Next.js?

Convert Wagtail’s rich-text HTML into React elements, replacing elements such as internal anchors with Next.js components. This also enables Next.js link prefetching for faster navigation.

Discussed at 15:19

How do you handle Wagtail images in Next.js?

Map Wagtail image data to Next.js’s Image component, including the source, dimensions, and layout. Wagtail renditions can preserve CMS focus-point and cropping behavior, while Next.js handles further optimization and resizing.

Discussed at 16:52

How do you invalidate statically generated Next.js pages when Wagtail content changes?

When a Wagtail page is saved, a Wagtail-side cache hook can call Next.js’s API to revalidate the affected page. Menu changes are more difficult, so the speaker suggests periodically refreshing the broader set of Next.js pages as well.

Discussed at 20:22

Open full transcript Markdown

Transcript

3,927 words · auto-generated · en Show transcript

Automatically transcribed, so expect mistakes in names and technical terms.

Speaker 1

I want to talk about using NextS as a format for websites built in Lactive. Next CS is a framework Built on React. But first with Ludwig. I'm Lukas, a software developer from Fabric, designer office in Amsterdam, and we mainly build websites in the museum sector and sector. and we focus heavily on front end design. So a lot of our developers are front end developers. I really like the idea of has a CMS

For building websites because for years we struggled a lot with the possibilities of the Django tempted language and for our float-end business to learn those and at the same time there's a lot of improvements in the JavaScript center where frameworks are more common and people have more and more experience with frameworks And less experience with playing HTML and JavaScript. So I think it's nice if the atlas option for Wagtail would be developed very well and easy to use with frameworks. great for separating the back

end to front end logic so we could more easily have well multiple apps that use the same API and same CMS and the back end and you can also more easily decouple stuff. You could have a new design for it and still keep the same API. I think this is at the moment a major struggle often. So yeah, I think headless is Nice. I also like NextJS. It's a relatively new framework built on React and it solves a lot of the problems why we didn't want to use frameworks mainly support for search engines. optimization and

Well images and a lot of stuff is just out of the box in XCS and they have a nice access community so I hope that stays away. I think it's nice to try to build websites with that. Also it's built on React. Of course you everyone has their favourite framework, but I think React sta is already developed for many years and there's a lot of stuff available so. it's way easier to find solutions for common problems in React often for developers than it is in well nearly JavaScript it's pretty hard often and well in more obscure frameworks like Svelte, it seems really nice. I'm still worried that it's too new

for not sure how long it will stay. So I think this is a nice one. Yeah so how can we combine WebTel and SES in a nice way. I've been looking how to work on that and how we can easily integrate it and make it logic and easy for everyone to build website with it. Yeah there is already existing work on this. There's a repository called VegTil Pivot, which is by a Swedish company, I think it's called Freud. They did very nice work on building a cookie cutter project with WegTil and Nex. I like a lot what they've been doing and they have been solving a lot of common problems

like review modes and proper protective paces and stuff like that. But they also are very opinionated. in my opinion. You have to use the computer cutter is very custom, it doesn't use a vector API mostly, it's just uses Rust Framework API and it has integration with a lot of stuff and storybook which I really like. But for me it feels also that you don't really have your own choices anymore because you have to do it that way. So I was looking a bit further like how can we make it more general? Then I noticed hope. But they will also have It was operating with Next. js. WebTel currently has a fairly called RVS yet

where they are doing also some research on Next. js, which is really nice and they keep track of what how far they are. So I was surprised that WebTil themselves are also interested in Next ES and Heavy. So then I arrived at the WebTil space and wanted to improve on the Next ES stuff and I noticed that a lot of people from WebTil and other classes are also interested in XS because this is the stuff great. So what I wanted to do to show better how to integrate WebTil and XGS is use the next the webtail bakery jet demo to turn it into projects product with

webtail and next together so that bakery demo the bakery demo is a webtail demo site for new development. To learn to work with Wagtil and see what the possibilities of the CMS are. So it's a repository you can check out and then you can play around with WebTil. I think it's nice to turn it into Correct, where it uses NextGS as a front end and you can learn how to how you can solve the problems with Next Yes and you can also talk about it and see how it can be done even better. And maybe you can make a Wactail helper package to better integrate next year later. So I want to talk about

how I sold some stuff for the Wactail Baker sites and some common topics that are occurring when you have when you try to make ha has CMS front end like routing of pages and different templates and how can we make stream fields render correctly and risk text is pretty hard. Images are interesting. and well then you have the more advanced adding features. And then you have caching and forms. Casting mainly because the front framework also offers and actually has the option to do service address rendering. That's the incremental static generation that's I really like and then the cut the basis generated on the website, the static

sorry on the server, the static generation. And for next request you only have to download static cases. you know and it will be really fast. Then there's incremental study generation that means that the faces are not necessarily generated on server, but they are generated on server when the user first requests them. So I think This is very useful for Vectail because people will make new pages all the time and you don't necessarily want to immediately generate everything all the time. So this is a feature I really like. Server-side rendering on the other hand just means that every request by any client in the browser it will do a request to NextCS and to BackTill. So you can more customly build the base

so it has less issues with caching, static generation has issues with caching because well when you change something you cannot use the static generated HTML again. So then we also need solutions for that. Alright so the typical NetVS is a is that NetVS is a server serve the on the server and on the client. And the client just requests a page from NatJS and NGS says like, oh it's a Dincast so I can just send to or NGS Tinks. I don't have DNCS and I need to build it and then I need Web to help me build page. And then you can serve it to the client and the client can from then on also call the Wacto API to further build the page

and get more out of it. Yes, so I want to show some coding. Alright, so here we have the Webflow bakery demo CMS with the Webflow 3. 0 million bars. Yeah, so over this webflow. And here we have the bakery demo. This is already running on Next Yes. I don't have every component copied. I'm just going gradually over it. I want to talk about is routing. So when you go somewhere you would like this is the routing URLs, so you want them

to be able to manage them. And Next H provides routing options. They have a directory called pages and you can build your own routes here. Here indexes is a phone name, but any name you put here as a file is like a URL segment that you can use as a page. Then it also has variable pages. So in this case I just grab the whole path as an array as an option from S and this will be a catch-all that will just find any page that a user requests. And what I'm actually doing then is I call the webfile API endpoints and it will give me I can send an HTML URL to it and then it will give me the

a redirect URL to the page That is connected to that URL. And then I can get the page data from that. Actually this function is doing that. So that's that allows me to get the vector API. For any page that I that is requested by the user. And I think that's the best way to solve routing for now because from back there you can do any routing you like, you can create any page you like. And actually So just ask Wegtail which space is actually needed and then you can render it. Yeah so what happens when a page arrives? We basically get the page

data from Wegtail. Currently I say revalidate after 12 hours well based on how much you like the guessing I think it's mostly should be less. And then I just render base components which will be like the base HTML from vector templates like the basic page. So next you have an option to create a head element which will fill the head for a certain URL. So it's very eas useful for social OG tags and search engine optimization because any page can have a different URL, a different tags. And you can also add components to it in different in different components you can add

stuff to it by just using this hat rubber and next yes we'll gather everything together and put it in the head. Yeah so currently I render here the basic structure of the vector base with this a menu bar Red crumbs obviously and a template. So yeah, of course you can have different templates because in vector you have the base models, so e every base model can have a different template. So we can solve that by mapping the templates from WebTL to templates from React. So here I say if I have home gates, I want to render

And if I have a breath page I want to run the bread text templates and etc. The nice part is also that this dynamic allows the code only to be loaded when it's necessary, so your adjust with code that needs to be downloaded is smaller. Yeah, so then we would load, for example, the homepage in this case. And then it will render basically I copied the Wagtail homepage templates and rewrote it to React. It's it's kind of the same as HTML for the most part. And I think the react looks pretty clean and nice. That's what I like about it. Yeah, so what I noticed, what I didn't know when working with a web API, it's actually already quite powerful. You can

query the fields you like at any moment, and you can query lists of stuff and you can a lot of it is already inside, so actually I had less problems making Web Nexus components than I assumed. But I do think it would be nice if we have problems that we can see if the vector FI needs to be improved for some points. Yeah, so that's nice to research. So for example for in this space, the bread space. I 'm sorry. So here we have a selection of reds and we have destination. So in the React component I can actually just call the WebTel API and say, hey I want six threads and in alphabetical order or whatever order I want.

And then when I go to the next page I can query different selection neck of things. So it's well it's dynamic loading network. pretty fast and well I think. So how can we render stream fields? In the admin multiple stream fields can be defined and each has their own rendering. But the nice thing is that WebTel API already supports nest nested fuel stream fields to API. I can also show by the JSON nested score this page. This isn't this is an example of a complex query you can do you can just basically puts a query what stuff you want and you can

specify what skills you want and then you get like really pistoning. Oh So for example for numbers you guys. Yeah, so basically components and then the extreme fields, for example, I think it's called body. Yeah, so then we get a paragraph work as simple as a type telling ID. And then we next years we can also render these by well for example. I can make a mapping from

blocks just like the templates to templates that will be rendered for the block and then I can actually loop over the strengths and just render these blocks. For that I also made the component vectors. So then I get this risk text. Yeah, so risk text is also pretty hard because BackTelf by default it will create references in the rich text output. So I think that's something that could be improved in the API. And I think someone was working also on it in the in this in this sprint, so that's super nice. That the page URLs are actually URLs and not links to pages because that's hard to

to find the correct page for in WebT or in X. So I'm basically using to convert the RIST text is a utility where I found this rewrites HML to react and I can turn elements into next JS elements. For example the anchor element. MaxDS likes the hasn't link component which does which recognize that the link is an internal link on the website and then it can already prefetch links on the page that you are on. So loading will be a lot faster. So it's really nice to make use of that to go around the website because it

will feel very fast and not flashy. So I process each graph text element with these with this function to turn it into react actually and then I can manipulate it. So it's pretty useful I think. Yeah then Images are always hard to responsive images, but NextGS has a nice image component that actually maps pretty well to the Vector API output. So in the end for simple images I only need I can almost directly map them to

To an XMS. Here I'm actually doing an experience experiment with Optimizing the width and height, but basically I can just render the next image, convert the source depending on how your server settings are to the to the correct SVC RL, and I can just map the width and hydro the image to it and it will work

Speaker 2

absolutely

Speaker 1

pretty well. Next year supports multiple layouts. So here I run an image and I say the fill layout and then it will fill a goal container just like an original and other Otherwise you can also use the regular layouts and you have some similar some different layouts based on what you exactly need for images because we have different use cases. So what I also do for running images on the back end I use API fields from Webfield. So WebFill API fields that and then you can put database fields and model fields from your API you can expose them

and often you will need some customization on it so then you can use seri serializes their Rust framework concepts that allows You to customize the output of certain fields. So, for example, for images, I use a rendition field because I like to keep the Wactio focus points functionality and that's currently in Wactio. So I first make a basic cutout from the image and then I let Next Yes handle the image. An X built-in image handler is already pretty good. It uses the sharp JavaScript library. And It's pretty good at resizing and rescaling so

this will work pretty fast in working in my experience. I'm happy with that. Also More advanced options if you really want faster images then there's support for external loaders. So that these are external services that can prop and use your images and serve your images really fast. I think they are very interesting to explore, but then you also have to find out how to combine it with a work tail focus points functionality because you would like to keep the CMS behavior from what you expect to get and what the MS will give you. Yeah, then caching is always an issue, so the server-size rendering is by far the easiest option.

For simple sites I think it's nice to use the standard generation for really fast sites and Next year I wrote a simple backhand for the rectal frontend cast. That will since recently Next CS is support. With its own API which allows you to invalidate static generated pages. So with a vector funded cache I can see if someone saves the vector page and then the vector funded cache will call the next API extra. and request the base to be first and then I can use

well I think have guest static pages and on save I can have them refreshed, which is really nice. Well for menu changes it's harder of course, so therefore I think it's nice to at least refresh your whole next pages every 10 minutes or whatever. Oh that 's my presentation. Just

Oh yeah, for view modes I made Like a hacky version of V mode that will work for draft pages and also it's protected with a password. But I notice now that Wactor has their own headless review page, which I think is way better and supports everything, so I still need to see how I can integrate it because that's great. Then the admin user bar is also a very nice feature for users. I think it shows the people at Torchbox called the edit It's the word in the bottom right that allows you to edit the current page. I think it's really useful for admin users. But it can of course not be cast on a static page. So on every page I do an extra call to the back

end to see if the user is logged in. And if so, I just rendered a static HTML from the template tag and put it here and it works, so it's nice. That's for that. Well forms are actually pretty hard. I thought I'll f solve forums also, but they still need some more support from the Vectual FI possibly, but I also have to research it and I think also someone from Overcast was doing some work on it. This is nice. So I hope actually to make progress on this soon and be able to integrate forms also easily. But it's not very easy at the moment. Okay, well, so to summarize,

I think the Waco API is already pretty good and it needs some minor tweaks to for a little bit better support, like maybe basically basic rendition field support for stream field blocks and risk text conversion for stream field. I think it would be nice if you could have at some point like a vector package for Next VS or a vector package. So that it will be even more even easier to use next and have the great and have an easy integration. Yeah so each topic has actually multiple ways of handling them instead I really like our next CS that it's so open that you can still investigate

So yeah, stuff I would like to do further research on now is the form support and routerable pages, but also is related to form support in a way. Initialization has internationalization and backfield has international nationalisation but I haven't they work together but I haven't done it very thorough thoroughly so I might have been some problems and also someone from Blackfield space was having some problems with it. There's some stuff to fix there. And then as fun statement features, I think the pipett Wagtail packets solve some of those, so they are very interesting to look at. And also Wagtail solved some of those with has a P package so well. That's a ri there's progress being made. That's nice.

And then there's guessing. I'm not sure. I think we'll see later about how that's I think it's basically very custom what you need for guessing. So that's what I want to share. So thanks. Any questions?

Speaker 3

Any questions? Anybody?

Speaker 1

Yes, yes. Vector API is basically a REST framework so it's built on REST framework and it basically uses all features from REST framework. But

Speaker 4

but the data format is not the error standard. But JSON API defines relationships that kind of stuff. Of course you can you can add an URL and so that it you can use another framework than Next PS to talk to the to the API without needing to write a new matteration data.

Speaker 1

Yeah compare items, no for inside actually , look nice

Presenters

Conference

Wagtail Space NL

June 15 - 17, 2022

Arnhem, Netherlands

Note: We understand that names change, people change, and bodies change. We respect each individual's journey and privacy. If you have any concerns about a video or need us to remove content, please don't hesitate to contact us. We will handle your request with care and promptly address any issues.

More videos from Wagtail Space NL