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

fix: ssr cssinjs instance #32

Merged
merged 3 commits into from
Jul 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/StyleContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export function createCache() {
const styles = document.body.querySelectorAll(`style[${ATTR_MARK}]`);

Array.from(styles).forEach((style) => {
(style as any)[CSS_IN_JS_INSTANCE] ??= CSS_IN_JS_INSTANCE_ID;
document.head.appendChild(style);
});

Expand All @@ -23,7 +24,9 @@ export function createCache() {
(style) => {
const hash = style.getAttribute(ATTR_MARK)!;
if (styleHash[hash]) {
style.parentNode?.removeChild(style);
if ((style as any)[CSS_IN_JS_INSTANCE] === CSS_IN_JS_INSTANCE_ID) {
style.parentNode?.removeChild(style);
}
} else {
styleHash[hash] = true;
}
Expand Down
10 changes: 10 additions & 0 deletions tests/server.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import {
import type { CSSInterpolation } from '../src';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import canUseDom from 'rc-util/lib/Dom/canUseDom';
import {
CSS_IN_JS_INSTANCE,
CSS_IN_JS_INSTANCE_ID,
ATTR_MARK,
} from '../src/StyleContext';

interface DesignToken {
primaryColor: string;
Expand Down Expand Up @@ -192,6 +197,11 @@ describe('SSR', () => {

// Patch to header
expect(document.head.querySelectorAll('style')).toHaveLength(1);
expect(
(document.head.querySelector(`style[${ATTR_MARK}]`) as any)[
CSS_IN_JS_INSTANCE
],
).toBe(CSS_IN_JS_INSTANCE_ID);

expect(errorSpy).not.toHaveBeenCalled();
});
Expand Down