javascripthtmlsvgsvg-sprite

Why svg sprites are not visible on the server. In gulp everything is fine


There was such a problem that svg sprite'y not visible at all anywhere except the gulp itself. Here's what on the hosting ====> https://i.sstatic.net/N1Ko8.png

And now the included gulp and everything works fine ===>https://i.sstatic.net/MRQ0j.png

Script for adding sprites to the body

;( function( window, document ) {
  'use strict';

   var file     = 'img/symbols.svg',
    revision = 1;

    if( !document.createElementNS || !document.createElementNS( 'http://www.w3.org/2000/svg', 'svg' ).createSVGRect )
    return true;

    var isLocalStorage = 'localStorage' in window && window[ 'localStorage' ] !== null,
    request,
    data,
    insertIT = function()
    {
        document.body.insertAdjacentHTML( 'afterbegin', data );
    },
    insert = function()
    {
        if( document.body ) insertIT();
        else document.addEventListener( 'DOMContentLoaded', insertIT );
    };

    if( isLocalStorage && localStorage.getItem( 'inlineSVGrev' ) == revision )
    {
    data = localStorage.getItem( 'inlineSVGdata' );
    if( data )
    {
        insert();
        return true;
    }
}

try
{
    request = new XMLHttpRequest();
    request.open( 'GET', file, true );
    request.onload = function()
    {
        if( request.status >= 200 && request.status < 400 )
        {
            data = request.responseText;
            insert();
            if( isLocalStorage )
            {
                localStorage.setItem( 'inlineSVGdata',  data );
                localStorage.setItem( 'inlineSVGrev',   revision );
            }
        }
    }
    request.send();
}
catch( e ){}

}( window, document ) );

Solution

  • Have you used a debug statement to ensure you're getting the right data on the server-side?

    Then AFTER, you cna use a console.log in this js script to log data to the console (or use the chrome javascript debugger and breakpoints).

    I would ensure you're receiving the right data from the server. If the server gets a 500 error, JS usually just stops executing (sometimes it'll continue but he response is invalid).

    This page describes how to use the chrome debugger:

    Get Started with Debugging JavaScript in Chrome DevTools