Skip to content

Commit

Permalink
Minify styles during prerendering on the server side. (#302)
Browse files Browse the repository at this point in the history
Co-authored-by: Golo Roden <golo.roden@thenativeweb.io>
  • Loading branch information
Matthias Wagler and goloroden authored Feb 5, 2020
1 parent e1a49b3 commit b81fe79
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
7 changes: 6 additions & 1 deletion lib/styles/ServerSideStyles.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { GenerateId } from 'jss';
import { minifyCss } from './minifyCss';
import { createGenerateId, SheetsRegistry } from 'react-jss';
import React, { ReactElement } from 'react';

Expand All @@ -13,8 +14,12 @@ class ServerSideStyles {
}

public generateStyleTag (): ReactElement {
const styles = this.registry.toString();

const minifiedStyles = minifyCss(styles);

return (
<style id='server-side-styles' dangerouslySetInnerHTML={{ __html: this.registry.toString() }} />
<style id='server-side-styles' dangerouslySetInnerHTML={{ __html: minifiedStyles }} />
);
}
}
Expand Down
9 changes: 6 additions & 3 deletions lib/styles/StaticGlobalStyles.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { oneLine } from 'common-tags';
import { minifyCss } from './minifyCss';
import { Theme } from '..';
import React, { ReactElement } from 'react';

Expand All @@ -10,13 +10,16 @@ class StaticGlobalStyles {
}

public generateStyleTag (): ReactElement {
const styles = this.buildStyles();
const minfiedStyles = minifyCss(styles);

return (
<style id='static-global-styles' dangerouslySetInnerHTML={{ __html: this.buildStyles() }} />
<style id='static-global-styles' dangerouslySetInnerHTML={{ __html: minfiedStyles }} />
);
}

private buildStyles (): string {
return oneLine`
return `
@import url(${this.theme.font.import});
body, html {
Expand Down
7 changes: 7 additions & 0 deletions lib/styles/minifyCss.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { oneLine } from 'common-tags';

const minifyCss = function (css: string): string {
return oneLine(css);
};

export { minifyCss };

0 comments on commit b81fe79

Please sign in to comment.