Skip to content

Commit

Permalink
Prevent _emitSheetCSS to generate empty style tags
Browse files Browse the repository at this point in the history
  • Loading branch information
hamidrezahanafi committed Jun 8, 2024
1 parent 770d1fa commit 92b2ad9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/styled-components/src/models/ServerStyleSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export default class ServerStyleSheet {

_emitSheetCSS = (): string => {
const css = this.instance.toString();
if (!css) return '';
const nonce = getNonce();
const attrs = [
nonce && `nonce="${nonce}"`,
Expand Down
2 changes: 1 addition & 1 deletion packages/styled-components/src/sheet/Rehydration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const outputSheet = (sheet: Sheet) => {

const names = sheet.names.get(id);
const rules = tag.getGroup(group);
if (names === undefined || rules.length === 0) continue;
if (names === undefined || !names.size || rules.length === 0) continue;

const selector = `${SC_ATTR}.g${group}[id="${id}"]`;

Expand Down
12 changes: 12 additions & 0 deletions packages/styled-components/src/test/ssr.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ describe('ssr', () => {
expect(css).toMatchSnapshot();
});

it('should emit nothing when no styles were generated', () => {
styled.h1`
color: red;
`;

const sheet = new ServerStyleSheet();
renderToString(sheet.collectStyles(<div />));

const css = sheet.getStyleTags();
expect(css).toBe('');
});

it('should not spill ServerStyleSheets into each other', () => {
const A = styled.h1`
color: red;
Expand Down

0 comments on commit 92b2ad9

Please sign in to comment.