<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by João Paulo Vilaça on Medium]]></title>
        <description><![CDATA[Stories by João Paulo Vilaça on Medium]]></description>
        <link>https://medium.com/@jpggvilaca?source=rss-402490717e1f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*syvcvBl0xfisU11JTuSnBA.jpeg</url>
            <title>Stories by João Paulo Vilaça on Medium</title>
            <link>https://medium.com/@jpggvilaca?source=rss-402490717e1f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 18:50:55 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jpggvilaca/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[The quest for a headless eCommerce]]></title>
            <link>https://medium.com/@jpggvilaca/the-quest-for-a-headless-ecommerce-c62e9b84240?source=rss-402490717e1f------2</link>
            <guid isPermaLink="false">https://medium.com/p/c62e9b84240</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[João Paulo Vilaça]]></dc:creator>
            <pubDate>Mon, 08 Jul 2019 06:58:34 GMT</pubDate>
            <atom:updated>2019-07-08T06:58:34.431Z</atom:updated>
            <content:encoded><![CDATA[<p>Our goal was to turn a huge software platform framework built in 1996, with a huge monolithic codebase and millions of dependencies, <strong>decoupled</strong> from the frontend, and make it look like it’s 2019. The “beast” I’m talking about is <strong>IBM WebSphere Commerce</strong>, and the frontend solution was <strong>React</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*W1-iwyUPQ8AVGR-IX_05BQ.jpeg" /><figcaption>We are actually using a more recent version but still…</figcaption></figure><p>The team I belonged to was responsible for working in the eCommerce platform and the idea was to make the frontend part of it more modern, faster and UX friendly. But we couldn’t just build a single page application because NOT using WCS was not an option (legacy, dependencies, licenses, roadmap, etc…), and besides, it uses <strong>server side rendering</strong> so we needed a solution to overcome this problem.</p><p>Now, don’t get me wrong, WCS is really solid and does its job, but to make the frontend beautiful, shiny and fast, we could not rely on css files with !important‘s everywhere and javascript files with thousands of lines, along with 500 hundred line functions and almost 10 jQuery versions (and several plugins) in the mix as well.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/486/1*AUvENFH7ds-qYNnD-OZ_Eg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/660/1*qi8LvsMC0fVCSxTgwKb7vA.png" /><figcaption>lovely</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*1Os8lPso_Q2JSWH3uzy3rA.gif" /><figcaption>Actual footage of the frontend team gathering forces to accomplish this task.</figcaption></figure><p>I won’t go into much detail on how WCS works but in a nutshell it works like this:</p><ul><li>WCS gets all the data from database and external systems.</li><li>Template engines (JSP - JavaServer Pages) get that data, apply some business rules, inject styles and scripts, then the server processes all this information and serves the result as a response.</li></ul><p>Now for the React part.</p><p>The React application is just a bunch of javascript code injected into a DOM node so we need to add that node to the JSP of the page we’re trying to “reactify”. So, once WCS loads the JSP, our DOM node will be there, and, when our script executes, our app will be injected into that same node.</p><p>One tricky part is that the resulting page is not a single page application. We’re just “reactifying” parts of that page (e.g. header or footer). Apart from that, we’re not dealing with a single application but several ones, and the way we achieved that was by using a `switch` case, and for every DOM node responsible for hosting an app, we would render the specific code that should be injected in it, like so:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3rDffvAwGL7vFxEgv3lsFg.png" /><figcaption>Mapping nodes to React components</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xQJIDpBu2Wzmhc0cojGOHg.png" /><figcaption>Notice the `tree` variable</figcaption></figure><p>So now we have a few div’s with custom id’s being loaded by WCS through JSP’s and all our scripts being loaded accordingly, so far so good.</p><p>Setting aside the complexity of redux and caching logic we also implemented, the real problem now is: HOW THE HELL REACT INTERACTS WITH WCS?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*67ko1m2WrC_8Q8rB53e8Gg.jpeg" /><figcaption>We did…a little bit.</figcaption></figure><p>The way we achieved this was by injecting yet another tiny React app in the page, we called it… “ExternalSpy”. All this app does is getting the custom events and triggering a specific redux action, and it only returns aFragment, which means no nodes are added to the DOM, it’s literally invisible but quite powerful, this app is now the <strong>bridge between WCS and React</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-amhTg2IJqrNRiDNOJg1eQ.png" /><figcaption>Our sneaky “spy” listening to window events</figcaption></figure><p>Now, let’s imagine the scenario that the user wants to add an item to the shopping cart. The flow goes something like this:</p><ul><li>After the user clicks on the button, the normal flow of adding to cart from WCS starts.</li><li>WCS scripts responsible for adding to cart are executed, but before that, we edited that file to, on top of the core logic, also dispatch a custom event to the windowfor us:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1ooAZIc1R3TBX3qpyuy1nQ.png" /><figcaption>Inside WCS scripts</figcaption></figure><ul><li>Our “ExternalSpy” is ready for this because he’s listening for the custom window events we dispatched (code snippet above). It catches the ‘add_to_cart’ event, and now <strong>React is in control</strong>.</li><li>ExternalSpy gets the event and dispatches the correspondent action to start the redux flow which results in whatever it needs to do (update quantities, make api calls, hide/show some section, etc…).</li><li>Since all our apps are connected to the same redux store, <strong>they are all aware</strong> of what’s going on. So now the Shopping Cart (which is also a React app) gets the resulting state changes from redux and updates accordingly.</li><li>Don’t worry about page refreshes, because it’s all cached ;)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/430/1*-5Mj0Wz29MEHtn7t6nW84Q.jpeg" /><figcaption>Earth is like…the best planet in the world.</figcaption></figure><p>Of course this solution is a bit hacky and not ideal but it was <strong>necessary</strong>. The good news is that the amazing Backend team is building custom endpoints for us to use, which means that we won’t need to mess into WCS code to achieve our dreams.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*0SmeOUgR8hMJYNgLSAwSmg.png" /><figcaption>Don’t let your dreams be dreams. DO IT.</figcaption></figure><p>Yep, that’s all folks. While it wasn’t rocket science it was very fun to build. This was only a high overview on how we managed to connect these two technologies that are 17 years apart, and of course, like I said, with all the constraints we couldn’t just build everything from scratch so this was our solution.</p><p>If you’d like more insight about our approach or if you have any other questions feel free to contact me through email at jpggvilaca@gmail.com .</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c62e9b84240" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>