Skip to main content

Sitecore with VueJS: Reserved Attributes and Ignored Elements

VueJS Strips Reserved Attributes on Ignored Elements

Experience Editor is what makes Sitecore EXTRA amazing as a CMS. Don't get me wrong it has alot to offer but the "oohhh ahhh WWoooWws" happen when it starts showing off what it can do through the experience editor.

One of the gotchas I ran into when connecting Sitecore and VueJS (and I'm sure its the same for any framework that crawls the DOM to figure out what it should be doing) is that sometimes attributes can conflict with each other. In VueJS world it thinks its a vuejs attribute and will remove it from the dom and try to use it. Unfortunately even on ignored elements it will still strip its reserved attributes...

Which breaks the experience editor with sitecore...

The Problem

VueJS removes Sitecore's attributes like "key" from the DOM even on elements that are marked to be ignored. There could be other conflicts but this is the one that got me.

The Solution

To make these conflicting elements not conflict, DUH!

What we know:
  • There is no configuration to change sitecore to use different attributes and sitecore doesnt use scoped attributes e.g: namespace-attrname
  • It would be a huge amount of effort and might ruin the upgrade path to try and alter such an integrated keyword in Sitecore's experience editor
  • There is no configuration to change VueJS to use scoped keywords eg: vue-reservedkeywordname
  • Vue has events that fire at the beginning of processing and just at the end of processing
  • Vue has an ignoredElements configuration but some special attributes still get parsed out (like "key")

Approach:

  • Javascript module that alters reserved attributes on vue component
  • It should not crawl over every single element...it needs to be focused and specialized for performance reasons
    • We'll only look at elements that have been marked to be ignored use the 
  • Because we don't really know all the conflicts we'll make the list of attributes expandable
  • It will change the attribute names before VueJS processes its components and put them back to their original name's when its finished (this is only for elements vuejs shouldnt have been messing with in the first place)

Implementation:

The Module:

/**
 * Author: Robert Guyett
 * Purpose: Resolves conflict with the "key" attribute between Sitecore's Experience Editor and VueJS
 * for Elements that VueJS should be ignoring
 */
import Vue from 'vue';

const reservedAttributes = ['key'];
const attrPrefix = 'data-';
export default {
    /**
     * Usage: Used just before vue is initialized in the root vueJS beforeCreate() event
     * This ads "data-" in front of any attribute marked as reserved by VueJS
     * NOTE: the reserved attributes doesnt tie into vuejs. its just an array of reserved attributes "reservedAttributes"
     */
    protectAttributes: () => {
        let reservedElements = [...Vue.config.ignoredElements];
        reservedElements.forEach(reservedEl => {
            [...document.querySelectorAll(reservedEl)].forEach(element => {
                if(element.hasAttributes() === false) {
                    return;
                }

                let elAttrNames = element.getAttributeNames();
                reservedAttributes.forEach(attrName => {
                    if(elAttrNames.includes(attrName)) {
                        // give the attribute a prefix
                        element.setAttribute(attrPrefix + attrName, element.getAttribute(attrName));
                    }
                });
            });
        });
    },
    /**
     * Usage: Used right after everything is mounted in the root VueJS mounted() event
     * This puts everything back the way it was
     */
    persistAttributes: () => {
        let reservedElements = [...Vue.config.ignoredElements];
        reservedElements.forEach(reservedEl => {
            [...document.querySelectorAll(reservedEl)].forEach(element => {
                if(element.hasAttributes() === false) {
                    return;
                }

                let elAttrNames = element.getAttributeNames();
                reservedAttributes.forEach(attrName => {
                    if(elAttrNames.includes(attrPrefix + attrName)) {
                        // set the attribute name back to the original value without the prefix
                        element.setAttribute(attrName, element.getAttribute(attrPrefix + attrName));
                    }
                });
            });
        });
    }
};

Usage:

import ignoredElementFix from './ignoredElementsFix'

export default new Vue({
    el: '#app',
    beforeCreate() {
        ignoredElementFix.protectAttributes();
    },
    mounted() {
        ignoredElementFix.persistAttributes();
    }
});

Comments

  1. Casinos Near Foxwoods - MapYRO
    The map showing casinos and 목포 출장샵 other gaming facilities 사천 출장안마 located near Foxwoods 통영 출장마사지 in Connecticut, including 청주 출장샵 Foxwoods Resort Casino, 대전광역 출장마사지 Mohegan Sun Pocono,

    ReplyDelete
  2. There are approximately 10,four hundred free STL recordsdata for face shields, prostheses, neuroscience, a “heart library,” and a “molecule of the month” among the collections. It additionally has a section for 3D printable low-cost prosthetics and high precision machining 3D fashions related to Covid. STL stands for Standard Triangle Language and is a normal file format in 3D printing.

    ReplyDelete

Post a Comment

Featured

Sitecore with VueJS: Reserved Attributes and Ignored Elements

VueJS Strips Reserved Attributes on Ignored Elements Experience Editor is what makes Sitecore EXTRA amazing as a CMS. Don't get me wrong it has alot to offer but the "oohhh ahhh WWoooWws" happen when it starts showing off what it can do through the experience editor. One of the gotchas I ran into when connecting Sitecore and VueJS (and I'm sure its the same for any framework that crawls the DOM to figure out what it should be doing) is that sometimes attributes can conflict with each other. In VueJS world it thinks its a vuejs attribute and will remove it from the dom and try to use it. Unfortunately even on ignored elements it will still strip its reserved attributes... Which breaks the experience editor with sitecore... The Problem VueJS removes Sitecore's attributes like "key" from the DOM even on elements that are marked to be ignored. There could be other conflicts but this is the one that got me. The Solution To make these conflict...

Other Articles

Soft Skills: Communicate on Another Level

Communicate on Another Level Over the years I've dedicated a lot of energy into being able to communicate and understand Project Managers, Product Owners, Programmers with various skill levels and specializations, and APIs. However, that list doesn't include anyone with a broader level of concerns. The past couple years I've noticed I've had to communicate more and more with Executives, Business Owners, and Managers of various levels more and more as my responsibilities increase and my impact on projects become greater. So the goal is to learn to communicate with people who care less about the inside workings of the project at hand and more about funding, timeline, overall health, and impact of multiple projects. Ground Level I am a software engineer. I understand how code works and how to take an end goal and break it down into smaller tasks for success including the fine intricate details. I can organize code and explain how code works with other sets of code or...

Sitecore with VueJS: Html Fragment Module

Sitecore Module for VueJS I recently worked on a Sitecore 9.1 project where we got the chance to use a front end framework called VueJS. If you haven't had the chance to work with VueJS you should check them out  https://vuejs.org/ . It was very nice to work with once we got through some of the kinks. One of those issues was that VueJS is driven by javascript (duh!)...not HTML....You know what likes to spit out HTML though? Sitecore! The Challenge How do we make it so we can use Sitecore's placeholders and Sitecore's render methods with all of the nesting glory that can be done with them into a VueJS template? The Solution To make a VueJS component that accepts the same formats Sitecore uses of course! Under the hood, Sitecore renders a begin tag element and an end tag element. What we know: The most basic render method just takes those two properties and slaps them together to form a whole html element. VueJS utilizes a simple tree structure for components ...