Skip to content

Commit

Permalink
fix: useId generic logic
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ committed Apr 15, 2022
1 parent acdc8f7 commit d7f5aaf
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 12 deletions.
20 changes: 15 additions & 5 deletions src/useStyleRegister.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ function uniqueHash(path: (string | number)[], styleStr: string) {
return hash(`${path.join('%')}${styleStr}`);
}

function Empty() {
return null;
}

/**
* Register a style to the global style sheet.
*/
Expand Down Expand Up @@ -230,19 +234,25 @@ export default function useStyleRegister(
);

return (node: React.ReactElement) => {
if (isMergedClientSide || !defaultCache) {
return node;
}
let styleNode: React.ReactElement;

return (
<>
if (isMergedClientSide || !defaultCache) {
styleNode = <Empty />;
} else {
styleNode = (
<style
{...{
[ATTR_TOKEN]: cachedTokenKey,
[ATTR_MARK]: cachedStyleId,
}}
dangerouslySetInnerHTML={{ __html: cachedStyleStr }}
/>
);
}

return (
<>
{styleNode}
{node}
</>
);
Expand Down
41 changes: 34 additions & 7 deletions tests/server.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,23 @@ describe('SSR', () => {
},
});

const Box = () => {
const Box = ({ children }: { children?: React.ReactNode }) => {
const [token] = useCacheToken<DerivativeToken>(theme, [baseToken]);

const wrapSSR = useStyleRegister({ theme, token, path: ['.box'] }, () => [
genStyle(token),
]);

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

const IdHolder = () => {
const id = React.useId();
return (
<div id={id} className="id">
{id}
</div>
);
};

it('should not use cache', () => {
Expand All @@ -84,13 +93,19 @@ describe('SSR', () => {

const html = renderToString(
<StyleProvider cache={cache}>
<Box />
<IdHolder />
<Box>
<IdHolder />
</Box>
<IdHolder />
</StyleProvider>,
);

const style = extractStyle(cache);

expect(html).toEqual('<div class="box"></div>');
expect(html).toEqual(
'<div id=":R1:" class="id">:R1:</div><div class="box"><div id=":Ra:" class="id">:Ra:</div></div><div id=":R3:" class="id">:R3:</div>',
);
expect(style).toEqual(
'<style data-token-hash="u4cay0" data-css-hash="gn1jfq">.box{background-color:#1890ff;}</style>',
);
Expand All @@ -116,7 +131,11 @@ describe('SSR', () => {
// Force insert style since we hack `canUseDom` to false
mock="client"
>
<Box />
<IdHolder />
<Box>
<IdHolder />
</Box>
<IdHolder />
</StyleProvider>,
{
hydrate: true,
Expand All @@ -133,7 +152,11 @@ describe('SSR', () => {
it('tricky ssr', () => {
const html = renderToString(
<StyleProvider>
<Box />
<IdHolder />
<Box>
<IdHolder />
</Box>
<IdHolder />
</StyleProvider>,
);

Expand All @@ -152,7 +175,11 @@ describe('SSR', () => {
// Force insert style since we hack `canUseDom` to false
mock="client"
>
<Box />
<IdHolder />
<Box>
<IdHolder />
</Box>
<IdHolder />
</StyleProvider>,
{
hydrate: true,
Expand Down

0 comments on commit d7f5aaf

Please sign in to comment.