-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(server): add extractCriticalToChunks #2334
Merged
+840
−20
Merged
Changes from 23 commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
d98b963
init
mnajdova f30982d
add some resets
mnajdova bba6038
updates
mnajdova 55b8c0e
fixes
mnajdova aca34c9
cleanup
mnajdova afdcbe5
Update packages/server/types/create-instance.d.ts
mnajdova 52521b4
Update packages/server/src/create-instance/extract-critical2.js
mnajdova c7ac36c
adress comments
mnajdova d7e7f8c
tests
mnajdova c39f42a
flow fixes
mnajdova ee704e1
Improve auto-rehydration for global styles when using extractCritical2
Andarist 104d539
Fixed constructStyleTags to include all ids correctly
Andarist 6b5489e
Merge branch 'main' into feat/extract-critical-2
mnajdova cdd74fd
Update packages/server/types/create-instance.d.ts
mnajdova d5dad23
Update packages/server/src/create-instance/construct-style-tags.js
mnajdova de5acc6
fix flow issues & update tests
mnajdova da4d7d9
Update packages/react/__tests__/rehydration.js
mnajdova f3b63f4
tweak test title
Andarist 8345672
Merge branch 'main' into feat/extract-critical-2
mnajdova e69351c
renamed to extractCriticalToChunks
mnajdova 70301b5
spacings
mnajdova 584216a
spacing
mnajdova 00ce87b
Fixed an issue with Global styles being reinjected when rehydrating
Andarist 124cf3f
resolve comments
mnajdova 88de62d
test updates
mnajdova 26b6254
Fixed issue with global styles being sometimes owned by multiple shee…
Andarist c550bc1
Add tests for global rehydration
Andarist 9596d32
add comment about why the data-attribute is reset in <Global/>
Andarist b51b8d1
fix flow errors
Andarist 1ba5ab8
Revert merging layout effects in <Global/>
Andarist 8d6fc69
Update packages/server/src/create-instance/extract-critical-to-chunks.js
emmatown 634c863
Merge branch 'main' into feat/extract-critical-2
emmatown 2025657
Merge branch 'main' into feat/extract-critical-2
Andarist eb70989
Merge branch 'main' into feat/extract-critical-2
emmatown 9537994
Create tidy-feet-buy.md
emmatown 2cc16da
Create chilly-clocks-jam.md
emmatown File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
packages/server/src/create-instance/construct-style-tags.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// @flow | ||
import type { EmotionCache } from '@emotion/utils' | ||
import { generateStyleTag } from './utils' | ||
|
||
const createConstructStyleTags = ( | ||
cache: EmotionCache, | ||
nonceString: string | ||
) => (criticalData: { | ||
html: string, | ||
styles: Array<{ key: string, ids: Array<string>, css: string }> | ||
}) => { | ||
let styleTagsString = '' | ||
|
||
criticalData.styles.forEach(item => { | ||
styleTagsString += generateStyleTag( | ||
item.key, | ||
item.ids.join(' '), | ||
item.css, | ||
nonceString | ||
) | ||
}) | ||
|
||
return styleTagsString | ||
} | ||
|
||
export default createConstructStyleTags |
51 changes: 51 additions & 0 deletions
51
packages/server/src/create-instance/extract-critical-to-chunks.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// @flow | ||
import type { EmotionCache } from '@emotion/utils' | ||
|
||
const extractCriticalToChunks = (cache: EmotionCache) => (html: string) => { | ||
// parse out ids from html | ||
// reconstruct css/rules/cache to pass | ||
let RGX = new RegExp(`${cache.key}-([a-zA-Z0-9-_]+)`, 'gm') | ||
|
||
let o = { html, styles: [] } | ||
let match | ||
let ids = {} | ||
while ((match = RGX.exec(html)) !== null) { | ||
// $FlowFixMe | ||
if (ids[match[1]] === undefined) { | ||
// $FlowFixMe | ||
ids[match[1]] = true | ||
} | ||
} | ||
|
||
const regularCssIds = [] | ||
let regularCss = '' | ||
|
||
Object.keys(cache.inserted).forEach(id => { | ||
if ( | ||
(ids[id] !== undefined || | ||
cache.registered[`${cache.key}-${id}`] === undefined) && | ||
cache.inserted[id] !== true | ||
) { | ||
if (cache.registered[`${cache.key}-${id}`]) { | ||
// regular css can be added in one style tag | ||
regularCssIds.push(id) | ||
// $FlowFixMe | ||
regularCss += cache.inserted[id].toString() | ||
emmatown marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} else { | ||
// each global styles require a new entry so it can be independently flushed | ||
o.styles.push({ | ||
key: `${cache.key}-global`, | ||
ids: [id], | ||
css: cache.inserted[id] | ||
}) | ||
} | ||
} | ||
}) | ||
|
||
// make sure that regular css is added after the global styles | ||
o.styles.push({ key: cache.key, ids: regularCssIds, css: regularCss }) | ||
|
||
return o | ||
} | ||
|
||
export default extractCriticalToChunks |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For now, this won't get deprecated - the recommendation would be that:
extractCriticalToChunks
should be used when using@emotion/react
extractCritical
should be used when using@emotion/css
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated, you can re-check the wording.