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'; import axios from '~/lib/utils/axios_utils';
/** /**
* Retrieve SVG icon path content from gitlab/svg sprite icons * Resolves to a DOM that contains GitLab icons
* @param {String} name * in svg format. Memoized to avoid duplicate requests
*/ */
export const getSvgIconPathContent = name => const getSvgDom = memoize(() =>
axios axios
.get(gon.sprite_icons) .get(gon.sprite_icons)
.then(({ data: svgs }) => .then(({ data: svgs }) => new DOMParser().parseFromString(svgs, 'text/xml'))
new DOMParser() .catch(() => {
.parseFromString(svgs, 'text/xml') getSvgDom.cache.clear();
.querySelector(`#${name} path`) }),
.getAttribute('d'), );
)
/**
* 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); .catch(() => null);
import MockAdapter from 'axios-mock-adapter'; import MockAdapter from 'axios-mock-adapter';
import axios from '~/lib/utils/axios_utils'; 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('Icon utils', () => {
describe('getSvgIconPathContent', () => { describe('getSvgIconPathContent', () => {
let spriteIcons; let spriteIcons;
let axiosMock;
const mockName = 'mockIconName';
const mockPath = 'mockPath';
const mockIcons = `<svg><symbol id="${mockName}"><path d="${mockPath}"/></symbol></svg>`;
beforeAll(() => { beforeAll(() => {
spriteIcons = gon.sprite_icons; spriteIcons = gon.sprite_icons;
...@@ -15,45 +19,63 @@ describe('Icon utils', () => { ...@@ -15,45 +19,63 @@ describe('Icon utils', () => {
gon.sprite_icons = spriteIcons; gon.sprite_icons = spriteIcons;
}); });
let axiosMock;
let mockEndpoint;
const mockName = 'mockIconName';
const mockPath = 'mockPath';
const getIcon = () => iconUtils.getSvgIconPathContent(mockName);
beforeEach(() => { beforeEach(() => {
axiosMock = new MockAdapter(axios); axiosMock = new MockAdapter(axios);
mockEndpoint = axiosMock.onGet(gon.sprite_icons);
}); });
afterEach(() => { afterEach(() => {
axiosMock.restore(); axiosMock.restore();
clearSvgIconPathContentCache();
}); });
it('extracts svg icon path content from sprite icons', () => { describe('when the icons can be loaded', () => {
mockEndpoint.replyOnce( beforeEach(() => {
200, axiosMock.onGet(gon.sprite_icons).reply(200, mockIcons);
`<svg><symbol id="${mockName}"><path d="${mockPath}"/></symbol></svg>`,
);
return getIcon().then(path => {
expect(path).toBe(mockPath);
}); });
});
it('returns null if icon path content does not exist', () => { it('extracts svg icon path content from sprite icons', () => {
mockEndpoint.replyOnce(200, ``); return getSvgIconPathContent(mockName).then(path => {
expect(path).toBe(mockPath);
});
});
return getIcon().then(path => { it('returns null if icon path content does not exist', () => {
expect(path).toBe(null); return getSvgIconPathContent('missing-icon').then(path => {
expect(path).toBe(null);
});
}); });
}); });
it('returns null if an http error occurs', () => { describe('when the icons cannot be loaded on the first 2 tries', () => {
mockEndpoint.replyOnce(500); 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 => { it('extracts svg icon path content, after 2 attempts', () => {
expect(path).toBe(null); 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