Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
garronej committed Jul 2, 2023
1 parent 70548c5 commit a87c39c
Showing 1 changed file with 45 additions and 14 deletions.
59 changes: 45 additions & 14 deletions src/next/appDir.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@ export function NextAppDirEmotionCacheProvider(
const cache = createCache(options);
cache.compat = true;
const prevInsert = cache.insert;
let inserted: string[] = [];
let inserted: { name: string; isGlobal: boolean }[] = [];
cache.insert = (...args) => {
const serialized = args[1];
const [selector, serialized] = args;
if (cache.inserted[serialized.name] === undefined) {
inserted.push(serialized.name);
inserted.push({
"name": serialized.name,
"isGlobal": selector === ""
});
}
return prevInsert(...args);
};
Expand All @@ -46,20 +49,48 @@ export function NextAppDirEmotionCacheProvider(
});

useServerInsertedHTML(() => {
const names = flush();
if (names.length === 0) return null;
const inserted = flush();
if (inserted.length === 0) {
return null;
}
let styles = "";
for (const name of names) {
styles += cache.inserted[name];
let dataEmotionAttribute = `${cache.key}`;
let globalStyles = "";
let globalDataEmotionAttribute = `${cache.key}-global`;

for (const { name, isGlobal } of inserted) {
const style = cache.inserted[name];

if (isGlobal) {
globalStyles += style;
globalDataEmotionAttribute += ` ${name}`;
} else {
styles += style;
dataEmotionAttribute += ` ${name}`;
}
}

return (
<style
key={cache.key}
data-emotion={`${cache.key} ${names.join(" ")}`}
dangerouslySetInnerHTML={{
"__html": styles
}}
/>
<>
{styles !== "" && (
<style
key={cache.key}
data-emotion={dataEmotionAttribute}
dangerouslySetInnerHTML={{
"__html": styles
}}
/>
)}
{globalStyles !== "" && (
<style
key={`${cache.key}-global`}
data-emotion={globalDataEmotionAttribute}
dangerouslySetInnerHTML={{
"__html": globalStyles
}}
/>
)}
</>
);
});

Expand Down

0 comments on commit a87c39c

Please sign in to comment.