Commit 9d4db8b5 authored by Paul Slaughter's avatar Paul Slaughter

Merge branch '31734-responses-from-icon_utils-should-be-cached' into 'master'

Reduce svg icon requests during rendering

Closes #31734

See merge request gitlab-org/gitlab!26974
parents 1873a854 2b1cf5f5
/* eslint-disable import/prefer-default-export */
import { memoize } from 'lodash';
import axios from '~/lib/utils/axios_utils';
/**
* Retrieve SVG icon path content from gitlab/svg sprite icons
* @param {String} name
* Resolves to a DOM that contains GitLab icons
* in svg format. Memoized to avoid duplicate requests
*/
export const getSvgIconPathContent = name =>
const getSvgDom = memoize(() =>
axios
.get(gon.sprite_icons)
.then(({ data: svgs }) =>
new DOMParser()
.parseFromString(svgs, 'text/xml')
.querySelector(`#${name} path`)
.getAttribute('d'),
)
.then(({ data: svgs }) => new DOMParser().parseFromString(svgs, 'text/xml'))
.catch(() => {
getSvgDom.cache.clear();
}),
);
/**
* Clears the memoized SVG content.
*
* You probably don't need to invoke this function unless
* sprite_icons are updated.
*/
export const clearSvgIconPathContentCache = () => {
getSvgDom.cache.clear();
};
/**
* Retrieve SVG icon path content from gitlab/svg sprite icons.
*
* Content loaded is cached.
*
* @param {String} name - Icon name
* @returns A promise that resolves to the svg path
*/
export const getSvgIconPathContent = name =>
getSvgDom()
.then(doc => {
return doc.querySelector(`#${name} path`).getAttribute('d');
})
.catch(() => null);
import MockAdapter from 'axios-mock-adapter';
import axios from '~/lib/utils/axios_utils';
import * as iconUtils from '~/lib/utils/icon_utils';
import { clearSvgIconPathContentCache, getSvgIconPathContent } from '~/lib/utils/icon_utils';
describe('Icon utils', () => {
describe('getSvgIconPathContent', () => {
let spriteIcons;
let axiosMock;
const mockName = 'mockIconName';
const mockPath = 'mockPath';
const mockIcons = `<svg><symbol id="${mockName}"><path d="${mockPath}"/></symbol></svg>`;
beforeAll(() => {
spriteIcons = gon.sprite_icons;
......@@ -15,45 +19,63 @@ describe('Icon utils', () => {
gon.sprite_icons = spriteIcons;
});
let axiosMock;
let mockEndpoint;
const mockName = 'mockIconName';
const mockPath = 'mockPath';
const getIcon = () => iconUtils.getSvgIconPathContent(mockName);
beforeEach(() => {
axiosMock = new MockAdapter(axios);
mockEndpoint = axiosMock.onGet(gon.sprite_icons);
});
afterEach(() => {
axiosMock.restore();
clearSvgIconPathContentCache();
});
it('extracts svg icon path content from sprite icons', () => {
mockEndpoint.replyOnce(
200,
`<svg><symbol id="${mockName}"><path d="${mockPath}"/></symbol></svg>`,
);
return getIcon().then(path => {
expect(path).toBe(mockPath);
describe('when the icons can be loaded', () => {
beforeEach(() => {
axiosMock.onGet(gon.sprite_icons).reply(200, mockIcons);
});
});
it('returns null if icon path content does not exist', () => {
mockEndpoint.replyOnce(200, ``);
it('extracts svg icon path content from sprite icons', () => {
return getSvgIconPathContent(mockName).then(path => {
expect(path).toBe(mockPath);
});
});
return getIcon().then(path => {
expect(path).toBe(null);
it('returns null if icon path content does not exist', () => {
return getSvgIconPathContent('missing-icon').then(path => {
expect(path).toBe(null);
});
});
});
it('returns null if an http error occurs', () => {
mockEndpoint.replyOnce(500);
describe('when the icons cannot be loaded on the first 2 tries', () => {
beforeEach(() => {
axiosMock
.onGet(gon.sprite_icons)
.replyOnce(500)
.onGet(gon.sprite_icons)
.replyOnce(500)
.onGet(gon.sprite_icons)
.reply(200, mockIcons);
});
it('returns null', () => {
return getSvgIconPathContent(mockName).then(path => {
expect(path).toBe(null);
});
});
return getIcon().then(path => {
expect(path).toBe(null);
it('extracts svg icon path content, after 2 attempts', () => {
return getSvgIconPathContent(mockName)
.then(path1 => {
expect(path1).toBe(null);
return getSvgIconPathContent(mockName);
})
.then(path2 => {
expect(path2).toBe(null);
return getSvgIconPathContent(mockName);
})
.then(path3 => {
expect(path3).toBe(mockPath);
});
});
});
});
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment