Mr Treublaan 1-3
1097 DP Amsterdam
The Netherlands

Headless Drupal | Serving a XML sitemap of Drupal via Node.js / Express

19 Nov, 2015 Drupal

Home Blog Headless Drupal | Serving...

Drupal headless

In our search to develop a headless Drupal website, we stumbled across the XML sitemap. This is needed to inform search engines, like Google, about the organization of your site content and to submit content updates. As a result you will receive reports about indexed links, broken links, duplicate content and other errors on your Drupal website.

With this knowledge it is possible to implement optimizations and it let’s your Drupal website achieve a higher SEO score. You can post the XML sitemap in the Search console of Google (Tools for webmasters).

Generating a sitemap in Drupal is relatively easy with the XML sitemap module. This is a stable module that let’s you generate a sitemap relatively quickly.

But there is no such ready-to-use content in Node.js / Express JS. To put it in simple words, the content is loaded externally (from Drupal) and transferred to the client. But since the XML sitemap is essential for SEO, we searched for a way to implement it in Node.JS. Here’s how we pulled this off:

1. Install the XML sitemap module in Drupal

You can generate the sitemap via Drupal and then ‘stream’ it via Node.js / Express JS to the preferred search engine. So the first step is to produce the sitemap in Drupal, which also manages your content. If you are working on a decoupled Drupal installation, then I expect the configuration of the XML sitemap should not form any problems. But to be sure, you will find the instructions here.

2. Install the Request module in Node.JS / Express JS

After having tried several NPM modules, the Request module appeared to be most suitable and in the end a quick solution to ‘stream’ the sitemap (load and transfer). So install this module in your Node.js installation and don’t forget to import it in Node.js / Express JS, see (1) in the image below.

If you want to know more about installing NPM modules, then check Headless Drupal & Node.js | Part 2/3: Node.js introduction.

Stream the sitemap using ‘pipe’

First create a var in which you define the source of the XML sitemap in your Drupal installation (2).

app.get('/sitemap.xml', function (req, res) { var sitemap = request(sitemapxml); req.pipe(sitemap); sitemap.pipe(res); });

==> This ensures that an incoming request on /sitemap.xml transfers the sitemap from the Drupal source. So this allows the stream / pipe.

Posting the sitemap to, for example, the Google Search console is possible using the link jouwdomein.nl/sitemap.xml, where jouwdomein.nl stands for the Node.JS installation.

Done, wrap up

That’s it. While reading it seems quite easy and even more so once it works. But gathering information and testing takes a lot of time, which I hope you can now save as a result of this blog.

Questions, feedback? Let me know!

Written by Joris Snoek | Nov 19, 2015
Let's Talk

Please tell me all about your project!
Mail , or send a message:

Got some more time?

Related content
09 May, 2023 Drupal

Save time, frustration, and potential content loss.


07 Jun, 2021 Drupal

Programmatically in multiple Views


02 Jun, 2021 Drupal