Migrating a Requirejs app from Enonic CMS to XP


#1

This post requires understanding of how requirejs works and why this could be a challange in XP.

Migrating a Requirejs application from Enonic CMS seemed like a challenge, well first of all because I’m not an expert in Enonic XP and secondly because I read a post of others having issues with this and apparently gave it up.

Actually it was really simple, when you know of the possibillities in XP.

As this was a Enonic CMS application Requirejs requires files from _public. So what I did was to create a _public mapping service in XP (site.xml) - easy:

    <mappings>
        <mapping controller="/services/_public/_public.js">
             <pattern>/_public/.*</pattern>
        </mapping>
    </mappings>

The service (/services/_public/_public.js) was implemented as follows (ES6 code):

var lib = {
    io: require('/lib/xp/io'),
    util: require('/lib/enonic/util'),
};

import { toStr } from '/lib/posten/util';
import { respondWithErrorPage } from '/lib/posten/controller';

exports.get = function (req) {
    const filename = req.path.replace(/^.*?_public/, '/_public');
    const extension = filename.replace(/^.*\./, '.');
    const contentType = lib.io.getMimeType(extension);
    const file = lib.io.getResource(filename);
    log.info(`${filename} (${contentType})`);
    const body = file.exists() && lib.io.readText(file.getStream());

    if(body === false) {
        return respondWithErrorPage(404);
    }

    return { body, contentType };
};

So XP still delivers, a special thanks to Christian and Bjørnar at Enonic who pointed out the natural solution for this challenge. Oh and you might want to add some nocache headers. The code is ES6 (will not work in ES5) so it is transpiled with babel.

Best regards

Preben, EVRY, working for Konsernportalen Posten Norway.


#2

Great post Preben, thanks for sharing this solution!


#3

Looks to me like you have made a controller to serve files? How does this relate to requireJS?
Also - for the error handling you should simply return status: 404, then the error.js file will automatically be triggered.


#4

It is what it looks like - the controller delivers files that are requested by the Requirejs application (map application for Posten/Bring http://www.bring.se/karta). The application is quite big so it would be a challenge to rewrite it to not be a Requirejs app, and some of the code was external (it’s the arcGIS lib), so it would be very hard to rewrite it to something else. Simply returning 404 is sufficient in this case I agree (a bit lazy and used what we had).

Check the loading of single files delivered by Enonic CMS (_public at bring.se/karta) if you are interested. If there is an even easier way to do this I whould really appreciate this.

Also check out the post I read on the problem:

The resolution was as far as I can understand not to use Requirejs. This wasn’t really an option in this case. This fix seemed like the cheapest option to me - turning this to a project which could be solved in a week and not three or four.

As a result of this implementation there was zero rewrite of the initial application in Enonic CMS, which was the goal I had for the port to XP.