Skip to content

Commit

Permalink
fix: document in ssr env (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ authored Jul 20, 2023
1 parent 15fd790 commit 18b305c
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions src/hooks/useStyleRegister/cacheMapUtil.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import canUseDom from 'rc-util/lib/Dom/canUseDom';
import { ATTR_MARK } from '../../StyleContext';

export const ATTR_CACHE_MAP = 'data-ant-cssinjs-cache-path';
Expand Down Expand Up @@ -30,28 +31,31 @@ export function reset(mockCache?: Record<string, string>, from = true) {

export function prepare() {
if (!cachePathMap) {
const div = document.createElement('div');
div.className = ATTR_CACHE_MAP;
document.body.appendChild(div);
cachePathMap = {};

let content = getComputedStyle(div).content || '';
content = content.replace(/^"/, '').replace(/"$/, '');
if (canUseDom()) {
const div = document.createElement('div');
div.className = ATTR_CACHE_MAP;
document.body.appendChild(div);

// Fill data
cachePathMap = {};
content.split(';').forEach((item) => {
const [path, hash] = item.split(':');
cachePathMap[path] = hash;
});

// Remove inline record style
const inlineMapStyle = document.querySelector(`style[${ATTR_CACHE_MAP}]`);
if (inlineMapStyle) {
fromCSSFile = false;
inlineMapStyle.parentNode?.removeChild(inlineMapStyle);
}
let content = getComputedStyle(div).content || '';
content = content.replace(/^"/, '').replace(/"$/, '');

document.body.removeChild(div);
// Fill data
content.split(';').forEach((item) => {
const [path, hash] = item.split(':');
cachePathMap[path] = hash;
});

// Remove inline record style
const inlineMapStyle = document.querySelector(`style[${ATTR_CACHE_MAP}]`);
if (inlineMapStyle) {
fromCSSFile = false;
inlineMapStyle.parentNode?.removeChild(inlineMapStyle);
}

document.body.removeChild(div);
}
}
}

Expand All @@ -67,7 +71,7 @@ export function getStyleAndHash(
const hash = cachePathMap[path];
let styleStr: string | null = CSS_FILE_STYLE;

if (!fromCSSFile) {
if (!fromCSSFile && hash && canUseDom()) {
const style = document.querySelector(
`style[${ATTR_MARK}="${cachePathMap[path]}"]`,
);
Expand Down

0 comments on commit 18b305c

Please sign in to comment.