You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As of today, popular runtime CSS-in-JS libraries like Emotion and Styled Components all emit static styles into JavaScript bundles. By this regard, otion is no exception, as seen in the resulting bundle generated by Next.js on the right:
Basic example
The styling code shown on the previous image is as follows:
It can be split into two parts, possibly with a Babel plugin:
Static:css({ display: "flex" })
Dynamic: All the remaining key–value pairs from the original css function call.
The static part may be replaced with its corresponding hashed class name string ahead of time.
Details
While replacing constant-parametered calls to css and keyframes works with server-side rendering and inlined <style> tags, some styles may become missing once client-side navigation happens.
To make the solution router-compatible, static styles of the entire website should be collected and then emitted into a single static CSS file (e.g. with webpack), possibly with file name hashing and infinite caching in place. By referring to the result through a <link>, every static styling rule gets loaded in the background, so routing can happen without missing any styles.
When opting for ahead of time static rule processing, filterOutUnusedRules should not be used, as state changes may result in referring to class names filtered out during SSR.
In conclusion, style loading should happen like:
User visits page with critical static CSS inlined into a <style> tag for no round trips over HTTP 1.
As the user navigates through a different page on the site, the rules loaded through <link> stay intact. No inlined styles should be loaded on client-side route changes, as all the static CSS should be available by the time a navigation happens.
The text was updated successfully, but these errors were encountered:
That’s an interesting edge-case, thank you! I’m not sure about the limits of static analysis, but if the value of rest can be resolved in the context (it isn’t a function parameter), then its static and dynamic blocks may be taken apart.
The rest variable shall be left intact. A minifier could eliminate it if not used elsewhere.
Motivation
As of today, popular runtime CSS-in-JS libraries like Emotion and Styled Components all emit static styles into JavaScript bundles. By this regard, otion is no exception, as seen in the resulting bundle generated by Next.js on the right:
Basic example
The styling code shown on the previous image is as follows:
It can be split into two parts, possibly with a Babel plugin:
css({ display: "flex" })
css
function call.The static part may be replaced with its corresponding hashed class name string ahead of time.
Details
While replacing constant-parametered calls to
css
andkeyframes
works with server-side rendering and inlined<style>
tags, some styles may become missing once client-side navigation happens.To make the solution router-compatible, static styles of the entire website should be collected and then emitted into a single static CSS file (e.g. with webpack), possibly with file name hashing and infinite caching in place. By referring to the result through a
<link>
, every static styling rule gets loaded in the background, so routing can happen without missing any styles.When opting for ahead of time static rule processing,
filterOutUnusedRules
should not be used, as state changes may result in referring to class names filtered out during SSR.In conclusion, style loading should happen like:
<style>
tag for no round trips over HTTP 1.<link>
stay intact. No inlined styles should be loaded on client-side route changes, as all the static CSS should be available by the time a navigation happens.The text was updated successfully, but these errors were encountered: