Skip to content

Commit c259119

Browse files
authored
fix: significantly speed up styled-components in dev mode (#7440)
1 parent f4e414c commit c259119

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

dev/starter-next-studio/next.config.js

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
module.exports = {
2+
env: {
3+
// Matches the behavior of `sanity dev` which sets styled-components to use the fastest way of inserting CSS rules in both dev and production. It's default behavior is to disable it in dev mode.
4+
SC_DISABLE_SPEEDY: 'false',
5+
},
26
async redirects() {
37
return [
48
{

dev/test-next-studio/next.config.mjs

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ const config = {
2121
env: {
2222
// Support the ability to debug log the studio, for example `DEBUG="sanity:pte:* pnpm dev:next-studio"`
2323
DEBUG: process.env.DEBUG,
24+
// Matches the behavior of `sanity dev` which sets styled-components to use the fastest way of inserting CSS rules in both dev and production. It's default behavior is to disable it in dev mode.
25+
SC_DISABLE_SPEEDY: 'false',
2426
},
2527
transpilePackages: [
2628
'@sanity/block-tools',

packages/sanity/src/_internal/cli/server/getViteConfig.ts

+10
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,16 @@ export async function getViteConfig(options: ViteOptions): Promise<InlineConfig>
120120
// eslint-disable-next-line no-process-env
121121
'__SANITY_STAGING__': process.env.SANITY_INTERNAL_ENV === 'staging',
122122
'process.env.MODE': JSON.stringify(mode),
123+
/**
124+
* Yes, double negatives are confusing.
125+
* The default value of `SC_DISABLE_SPEEDY` is `process.env.NODE_ENV === 'production'`: https://github.com/styled-components/styled-components/blob/99c02f52d69e8e509c0bf012cadee7f8e819a6dd/packages/styled-components/src/constants.ts#L34
126+
* Which means that in production, use the much faster way of inserting CSS rules, based on the CSSStyleSheet API (https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleSheet/insertRule)
127+
* while in dev mode, use the slower way of inserting CSS rules, which appends text nodes to the `<style>` tag: https://github.com/styled-components/styled-components/blob/99c02f52d69e8e509c0bf012cadee7f8e819a6dd/packages/styled-components/src/sheet/Tag.ts#L74-L76
128+
* There are historical reasons for this, primarily that browsers initially did not support editing CSS rules in the DevTools inspector if `CSSStyleSheet.insetRule` were used.
129+
* However, that's no longer the case (since Chrome 81 back in April 2020: https://developer.chrome.com/docs/css-ui/css-in-js), the latest version of FireFox also supports it,
130+
* and there is no longer any reason to use the much slower method in dev mode.
131+
*/
132+
'process.env.SC_DISABLE_SPEEDY': JSON.stringify('false'),
123133
...getStudioEnvironmentVariables({prefix: 'process.env.', jsonEncode: true}),
124134
},
125135
}

0 commit comments

Comments
 (0)