Skip to content

Commit

Permalink
fix: significantly speed up styled-components in dev mode (#7440)
Browse files Browse the repository at this point in the history
  • Loading branch information
stipsan committed Aug 30, 2024
1 parent f4e414c commit c259119
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions dev/starter-next-studio/next.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
module.exports = {
env: {
// 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.
SC_DISABLE_SPEEDY: 'false',
},
async redirects() {
return [
{
Expand Down
2 changes: 2 additions & 0 deletions dev/test-next-studio/next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const config = {
env: {
// Support the ability to debug log the studio, for example `DEBUG="sanity:pte:* pnpm dev:next-studio"`
DEBUG: process.env.DEBUG,
// 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.
SC_DISABLE_SPEEDY: 'false',
},
transpilePackages: [
'@sanity/block-tools',
Expand Down
10 changes: 10 additions & 0 deletions packages/sanity/src/_internal/cli/server/getViteConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,16 @@ export async function getViteConfig(options: ViteOptions): Promise<InlineConfig>
// eslint-disable-next-line no-process-env
'__SANITY_STAGING__': process.env.SANITY_INTERNAL_ENV === 'staging',
'process.env.MODE': JSON.stringify(mode),
/**
* Yes, double negatives are confusing.
* 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
* 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)
* 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
* 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.
* 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,
* and there is no longer any reason to use the much slower method in dev mode.
*/
'process.env.SC_DISABLE_SPEEDY': JSON.stringify('false'),
...getStudioEnvironmentVariables({prefix: 'process.env.', jsonEncode: true}),
},
}
Expand Down

0 comments on commit c259119

Please sign in to comment.