Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: store cache in template tag #7

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 1 addition & 24 deletions packages/ngx-fast-lib/src/lib/icon-registry.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,14 @@ function createDomParser(document: Document): (s: string) => HTMLElement {
};
}

function styleDomCacheForPerformance(el: HTMLElement): HTMLElement {
/**
* reduce paint area with with/height 0 and overflow hidden
* fixed position of -2000px to always have it offscreen and outside of any native trigger (viewport observer in content visibilits)
* contain:content to leverage css perf features for older browsers not supporting content-visibility
* content-visibility: auto to exclude it completely from recalc styles
*/
el.setAttribute(
'style',
`
overflow: hidden;
width: 0px;
height: 0px;
position: fixed;
bottom: -2000px;
contain: content;
content-visibility: auto;
`
);
return el;
}

@Injectable()
export class IconRegistry {
private readonly domParser = createDomParser(this.document);
private readonly svgDomCache: HTMLElement = (() => {
// The DOM cache could be already created on SSR or due to multiple instances of the registry
const domCache =
this.document.getElementById('svg-cache') ||
this.domParser(`<div id="svg-cache"></div>`);
styleDomCacheForPerformance(domCache);
this.domParser(`<template id="svg-cache"></template>`);
this.document.body.appendChild(domCache);
return domCache;
})();
Expand Down