generated from garronej/ts-ci
-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathnextJs.tsx
83 lines (75 loc) · 3.25 KB
/
nextJs.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import * as React from "react";
import type { ReactNode } from "react";
import createEmotionServer from "@emotion/server/create-instance";
import type NextDocument from "next/document";
import type { DocumentContext } from "next/document";
import type { EmotionCache } from "@emotion/cache";
import { CacheProvider } from "@emotion/react";
import type { Options as CreateCacheOption } from "@emotion/cache";
import createCache from "@emotion/cache";
/**
* @deprecated Use tss-react/next instead.
* @see <https://docs.tss-react.dev/ssr/next> */
export function createEmotionSsrAdvancedApproach(options: CreateCacheOption) {
let muiCache: EmotionCache | undefined = undefined;
const createMuiCache = () => (muiCache = createCache(options));
function EmotionCacheProvider(props: { children: ReactNode }) {
const { children } = props;
return (
<CacheProvider value={muiCache ?? createMuiCache()}>
{children}
</CacheProvider>
);
}
function withEmotionCache(
Document: typeof NextDocument,
params?: {
getExtraCaches: () => EmotionCache[];
}
) {
const { getExtraCaches = () => [] } = params ?? {};
return class DocumentWithEmotionCache extends Document {
static async getInitialProps(ctx: DocumentContext) {
const emotionServers = [createMuiCache(), ...getExtraCaches()]
.sort((a, b) =>
getPrepend(a) === getPrepend(b)
? 0
: getPrepend(a)
? -1
: 1
)
.map(createEmotionServer);
const initialProps = await Document.getInitialProps(ctx);
return {
...initialProps,
"styles": [
...React.Children.toArray(initialProps.styles),
...emotionServers
.map(({ extractCriticalToChunks }) =>
extractCriticalToChunks(initialProps.html)
.styles.filter(({ css }) => css !== "")
.map(style => (
<style
nonce={options.nonce}
data-emotion={`${
style.key
} ${style.ids.join(" ")}`}
key={style.key}
dangerouslySetInnerHTML={{
"__html": style.css
}}
/>
))
)
.reduce((prev, curr) => [...prev, ...curr], [])
]
};
}
};
}
return { EmotionCacheProvider, withEmotionCache };
}
function getPrepend(cache: EmotionCache): boolean {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return !!(cache.sheet as any).prepend;
}