Skip to content

Commit

Permalink
feat: add client mark
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ committed Jul 25, 2023
1 parent 55d5716 commit 1945a0e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/hooks/useStyleRegister/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -366,10 +366,11 @@ export default function useStyleRegister(
hashId?: string;
layer?: string;
nonce?: string | (() => string);
clientOnly?: boolean;
},
styleFn: () => CSSInterpolation,
) {
const { token, path, hashId, layer, nonce } = info;
const { token, path, hashId, layer, nonce, clientOnly } = info;
const {
autoClear,
mock,
Expand Down Expand Up @@ -397,6 +398,7 @@ export default function useStyleRegister(
tokenKey: string,
styleId: string,
effectStyle: Record<string, string>,
clientOnly: boolean | undefined,
]
>(
'style',
Expand All @@ -409,7 +411,7 @@ export default function useStyleRegister(
if (existPath(cachePath)) {
const [inlineCacheStyleStr, styleHash] = getStyleAndHash(cachePath);
if (inlineCacheStyleStr) {
return [inlineCacheStyleStr, tokenKey, styleHash, {}];
return [inlineCacheStyleStr, tokenKey, styleHash, {}, clientOnly];
}
}

Expand All @@ -427,7 +429,7 @@ export default function useStyleRegister(
const styleStr = normalizeStyle(parsedStyle);
const styleId = uniqueHash(fullPath, styleStr);

return [styleStr, tokenKey, styleId, effectStyle];
return [styleStr, tokenKey, styleId, effectStyle, clientOnly];
},

// Remove cache if no need
Expand Down Expand Up @@ -548,13 +550,19 @@ export function extractStyle(cache: Cache, plain = false) {
styleKeys.forEach((key) => {
const cachePath = key.slice(matchPrefix.length).replace(/%/g, '|');

const [styleStr, tokenKey, styleId, effectStyle]: [
const [styleStr, tokenKey, styleId, effectStyle, clientOnly]: [
string,
string,
string,
Record<string, string>,
boolean,
] = cache.cache.get(key)![1];

// Skip client only style
if (clientOnly) {
return;
}

// ====================== Style ======================
styleText += toStyleStr(styleStr, tokenKey, styleId);

Expand Down
28 changes: 28 additions & 0 deletions tests/server.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,34 @@ describe('SSR', () => {
getStyleAndHash.mockRestore();
});

it('not extract clientOnly style', () => {
const Client = ({ children }: { children?: React.ReactNode }) => {
const [token] = useCacheToken<DerivativeToken>(theme, [baseToken]);

const wrapSSR = useStyleRegister(
{ theme, token, path: ['.client'], clientOnly: true },
() => ({
'.client': {
backgroundColor: token.primaryColor,
},
}),
);

return wrapSSR(<div className="box">{children}</div>);
};

const cache = createCache();

renderToString(
<StyleProvider cache={cache}>
<Client />
</StyleProvider>,
);

const plainStyle = extractStyle(cache, true);
expect(plainStyle).not.toContain('client');
});

it('default hashPriority', () => {
// >>> SSR
const cache = createCache();
Expand Down

0 comments on commit 1945a0e

Please sign in to comment.