Skip to content

Commit

Permalink
Allow already injected to be reset (#380)
Browse files Browse the repository at this point in the history
* Add resetInjected.

* Update types.

* Fix tests.

* Rename to resetInjectedStyle

* Update docs.

* Revert dist changes

* Add tests.

* Change word.
  • Loading branch information
milesj authored and lencioni committed Aug 28, 2019
1 parent bf95d9f commit 06a881b
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 4 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,24 @@ const styles = StyleSheet.create({
});
```

## Resetting Style Cache

The `reset` function can be used to reset the HTML style tag, injection buffer, and injected cache. Useful when Aphrodite needs to be torn down and set back up.

```js
import { reset } from 'aphrodite';

reset();
```

While the `resetInjectedStyle` function can be used to reset the injected cache for a single key (usually the class name).

```js
import { resetInjectedStyle } from 'aphrodite';

resetInjectedStyle('class_1sAs8jg');
```

## Server-side rendering

To perform server-side rendering, make a call to `StyleSheetServer.renderStatic`, which takes a callback. Do your rendering inside of the callback and return the generated HTML. All of the calls to `css()` inside of the callback will be collected and the generated css as well as the generated HTML will be returned.
Expand Down
3 changes: 3 additions & 0 deletions src/exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {hashString} from './util';
import {
injectAndGetClassName,
reset,
resetInjectedStyle,
startBuffering,
flushToString,
flushToStyleTag,
Expand Down Expand Up @@ -188,5 +189,7 @@ export default function makeExports(
flushToStyleTag,
injectAndGetClassName,
defaultSelectorHandlers,
reset,
resetInjectedStyle,
};
}
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const {
flushToStyleTag,
injectAndGetClassName,
defaultSelectorHandlers,
reset,
resetInjectedStyle,
} = Aphrodite;

export {
Expand All @@ -24,4 +26,6 @@ export {
flushToStyleTag,
injectAndGetClassName,
defaultSelectorHandlers,
reset,
resetInjectedStyle,
};
8 changes: 6 additions & 2 deletions src/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,13 @@ export const reset = () => {
styleTag = null;
};

export const resetInjectedStyle = (key /* : string */) => {
delete alreadyInjected[key];
};

export const getBufferedStyles = () => {
return injectionBuffer;
}
};

export const startBuffering = () => {
if (isBuffering) {
Expand Down Expand Up @@ -244,7 +248,7 @@ export const addRenderedClassNames = (classNames /* : string[] */) => {
};

const isValidStyleDefinition = (def /* : Object */) =>
"_definition" in def && "_name" in def && "_len" in def;
"_definition" in def && "_name" in def && "_len" in def;

const processStyleDefinitions = (
styleDefinitions /* : any[] */,
Expand Down
4 changes: 4 additions & 0 deletions src/no-important.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const {
flushToStyleTag,
injectAndGetClassName,
defaultSelectorHandlers,
reset,
resetInjectedStyle,
} = Aphrodite;

export {
Expand All @@ -28,4 +30,6 @@ export {
flushToStyleTag,
injectAndGetClassName,
defaultSelectorHandlers,
reset,
resetInjectedStyle,
};
32 changes: 32 additions & 0 deletions tests/inject_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
injectAndGetClassName,
injectStyleOnce,
reset,
resetInjectedStyle,
startBuffering,
flushToString,
flushToStyleTag,
Expand Down Expand Up @@ -335,6 +336,37 @@ describe('injection', () => {
assert.notMatch(styles, /color: blue/);
});
});

describe('resetInjectedStyle()', () => {
it('injects styles again after being reset', () => {
startBuffering();

css(sheet.red);

flushToStyleTag();

const styleTags = global.document.getElementsByTagName("style");
const lastTag = styleTags[styleTags.length - 1];

assert.equal(getSheetText(lastTag.sheet), `.${sheet.red._name} {color: red !important;} `);

// Delete all rules
while (lastTag.sheet.cssRules.length > 0) {
lastTag.sheet.deleteRule(0);
}

resetInjectedStyle(sheet.red._name);

assert.equal(getSheetText(lastTag.sheet), '');

// Re-inject
css(sheet.red);

flushToStyleTag();

assert.equal(getSheetText(lastTag.sheet), `.${sheet.red._name} {color: red !important;} `);
});
});
});

describe('String handlers', () => {
Expand Down
10 changes: 8 additions & 2 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export interface StyleSheetStatic {
* Rehydrate class names from server renderer
*/
rehydrate(renderedClassNames: string[]): void;

extend(extensions: Extension[]): Exports;
}

Expand All @@ -55,6 +55,12 @@ export function css(...styles: CSSInputTypes[]): string;
*/
export function minify(shouldMinify: boolean): void;

export function flushToStyleTag(): void;

export function reset(): void;

export function resetInjectedStyle(key: string): void;

interface StaticRendererResult {
html: string;
css: {
Expand Down Expand Up @@ -118,4 +124,4 @@ interface Exports {
StyleSheet: StyleSheetStatic;
StyleSheetServer: StyleSheetServerStatic;
StyleSheetTestUtils: StyleSheetTestUtilsStatic;
}
}

0 comments on commit 06a881b

Please sign in to comment.