Skip to content

Commit 6649528

Browse files
tnienhouse-atlJakeLane
andauthoredDec 13, 2021
Fix: Perform Node/SSR check without process (#918)
* Check for `document` instead of `process` to check for SSR * Changeset * Fixup tests - JSDOM now looks like a browser, not SSR * Allow tests to disable caching * Fix jsdoc typo * Rename cache helper file * Move replace plugin to react package only and use window instead of document * Use a common config for the transform in React package Co-authored-by: Jake Lane <jlane2@atlassian.com>
1 parent 0ea403a commit 6649528

15 files changed

+89
-25
lines changed
 

‎.changeset/seven-walls-scream.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@compiled/react': patch
3+
---
4+
5+
Changed the SSR check to be based on the presence of `document` instead of looking for Node processes.

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@
143143
},
144144
{
145145
"path": "./packages/react/dist/browser/runtime.js",
146-
"limit": "435B",
146+
"limit": "436B",
147147
"import": "{ CC, CS }",
148148
"ignore": [
149149
"react"

‎packages/react/src/__tests__/browser.test.tsx

-6
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@ import { styled } from '@compiled/react';
22
import { render } from '@testing-library/react';
33
import React from 'react';
44

5-
// TODO: When we move back to browser checking (window vs. process) we can remove
6-
// this and replace with the "jest-environment node" pragma.
7-
jest.mock('../../src/runtime/is-node', () => ({
8-
isNodeEnvironment: () => false,
9-
}));
10-
115
describe('browser', () => {
126
beforeEach(() => {
137
// Reset style tags in head before each test so that it will remove styles

‎packages/react/src/keyframes/__tests__/index.test.tsx

+17-5
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,27 @@ import defaultFadeOut, { namedFadeOut, fadeOut as shadowedFadeOut } from '../__f
77
const getOpacity = (str: string | number) => str;
88

99
const getKeyframe = (name: string) => {
10-
const styles = Array.from(
11-
document.body.querySelectorAll('style'),
12-
(style) => style.innerHTML
13-
).join('\n');
10+
const searchStr = `@keyframes ${name}`;
1411

15-
return styles.substring(styles.indexOf(`@keyframes ${name}`));
12+
return Array.from(document.head.querySelectorAll('style'), (style) => style.innerHTML)
13+
.filter((style) => style.indexOf(searchStr) >= 0)
14+
.map((style) => style.substring(style.indexOf(searchStr)))
15+
.join('\n');
1616
};
1717

1818
describe('keyframes', () => {
19+
beforeAll(() => {
20+
process.env.CACHE = 'false';
21+
});
22+
23+
afterAll(() => {
24+
delete process.env.CACHE;
25+
});
26+
27+
afterEach(() => {
28+
document.head.innerHTML = '';
29+
});
30+
1931
describe('referenced through a css prop', () => {
2032
describe('render an animation', () => {
2133
it('given an object call expression argument', () => {

‎packages/react/src/runtime/__tests__/style-ssr.test.tsx

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/**
2+
* @jest-environment node
3+
*/
4+
15
import React from 'react';
26
import { renderToStaticMarkup, renderToString } from 'react-dom/server';
37

‎packages/react/src/runtime/__tests__/style.test.tsx

-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@ import { render } from '@testing-library/react';
22
import React from 'react';
33
import type { ComponentType } from 'react';
44

5-
jest.mock('../is-node', () => ({
6-
isNodeEnvironment: () => false,
7-
}));
8-
95
describe('<Style />', () => {
106
let consoleErrorSpy: jest.SpyInstance;
117

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Flowtype definitions for cache
3+
* Generated by Flowgen from a Typescript Definition
4+
* Flowgen v1.15.0
5+
* @flow
6+
*/
7+
/**
8+
* Returns `true` when caching should be disabled, else `false`.
9+
*
10+
* Any code within this check will be removed in the output bundles:
11+
*
12+
* ```js
13+
* if (isCacheDisabled()) {
14+
* // This code will be removed from the output bundles
15+
* }
16+
* ```
17+
*/
18+
declare export var isCacheDisabled: () => boolean;

‎packages/react/src/runtime/cache.tsx

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Returns `true` when caching should be disabled, else `false`.
3+
*
4+
* Any code within this check will be removed in the output bundles:
5+
*
6+
* ```js
7+
* if (isCacheDisabled()) {
8+
* // This code will be removed from the output bundles
9+
* }
10+
* ```
11+
*/
12+
export const isCacheDisabled = (): boolean => {
13+
return process.env.NODE_ENV === 'test' && process.env.CACHE === 'false';
14+
};

‎packages/react/src/runtime/is-node.tsx

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,5 @@
1111
* ```
1212
*/
1313
export const isNodeEnvironment = (): boolean => {
14-
// https://nodejs.org/api/process.html#process_process_release
15-
return typeof process !== 'undefined' && process?.release?.name === 'node';
14+
return typeof window === 'undefined';
1615
};

‎packages/react/src/runtime/sheet.tsx

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { isCacheDisabled } from './cache';
12
import type { Bucket, StyleSheetOpts } from './types';
23

34
/**
@@ -77,8 +78,13 @@ function lazyAddStyleBucketToHead(bucketName: Bucket, opts: StyleSheetOpts): HTM
7778
const tag = document.createElement('style');
7879
opts.nonce && tag.setAttribute('nonce', opts.nonce);
7980
tag.appendChild(document.createTextNode(''));
80-
styleBucketsInHead[bucketName] = tag;
8181
document.head.insertBefore(tag, nextBucketFromCache);
82+
83+
if (isCacheDisabled()) {
84+
return tag;
85+
}
86+
87+
styleBucketsInHead[bucketName] = tag;
8288
}
8389

8490
return styleBucketsInHead[bucketName]!;

‎packages/react/src/runtime/style-cache.tsx

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as React from 'react';
22
import { createContext, useContext } from 'react';
33

4+
import { isCacheDisabled } from './cache';
45
import { isNodeEnvironment } from './is-node';
56
import type { ProviderComponent, UseCacheHook } from './types';
67

@@ -27,6 +28,10 @@ if (!isNodeEnvironment()) {
2728
* Hook using the cache created on the server or client.
2829
*/
2930
export const useCache: UseCacheHook = () => {
31+
if (isCacheDisabled()) {
32+
return {};
33+
}
34+
3035
if (isNodeEnvironment()) {
3136
// On the server we use React Context to we don't leak the cache between SSR calls.
3237
// During runtime this hook isn't conditionally called - it is at build time that the flow gets decided.

‎packages/react/tsconfig.browser.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"extends": "../tsconfig.options.json",
2+
"extends": "./tsconfig.options.json",
33
"compilerOptions": {
44
"outDir": "dist/browser",
55
"plugins": [
@@ -10,7 +10,6 @@
1010
}
1111
}
1212
],
13-
"rootDir": "src",
1413
"tsBuildInfoFile": "tsconfig.browser.tsbuildinfo"
1514
},
1615
"references": [{ "path": "../benchmark" }, { "path": "../jest" }]

‎packages/react/tsconfig.cjs.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
{
2-
"extends": "../tsconfig.options.json",
2+
"extends": "./tsconfig.options.json",
33
"compilerOptions": {
44
"module": "commonjs",
55
"outDir": "dist/cjs",
6-
"rootDir": "src",
76
"tsBuildInfoFile": "tsconfig.cjs.tsbuildinfo"
87
},
98
"references": [{ "path": "../benchmark" }, { "path": "../jest" }]

‎packages/react/tsconfig.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
{
2-
"extends": "../tsconfig.options.json",
2+
"extends": "./tsconfig.options.json",
33
"compilerOptions": {
44
"outDir": "dist/esm",
5-
"rootDir": "src",
65
"tsBuildInfoFile": "tsconfig.esm.tsbuildinfo"
76
},
87
"references": [{ "path": "../benchmark" }, { "path": "../jest" }]

‎packages/react/tsconfig.options.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"extends": "../tsconfig.options.json",
3+
"compilerOptions": {
4+
"rootDir": "src",
5+
"plugins": [
6+
{
7+
"transform": "ts-transform-define",
8+
"replace": {
9+
"isCacheDisabled()": "false"
10+
}
11+
}
12+
]
13+
}
14+
}

0 commit comments

Comments
 (0)
Please sign in to comment.