1
1
import React , { useEffect } from 'react'
2
- import { AppProps } from 'next/app'
2
+ import App from 'next/app'
3
+ import type { AppProps , AppContext } from 'next/app'
3
4
import Head from 'next/head'
4
5
import { useTheme , ThemeProvider } from '@primer/components'
6
+ import { getThemeProps } from 'components/lib/getThemeProps'
5
7
6
8
import '@primer/css/index.scss'
7
9
8
10
import { defaultThemeProps } from 'components/lib/getThemeProps'
9
11
10
- const App : React . FC < AppProps > = ( { Component, pageProps } ) => {
12
+ type MyAppProps = AppProps & { csrfToken : string , themeProps : typeof defaultThemeProps }
13
+ const MyApp = ( { Component, pageProps, csrfToken, themeProps } : MyAppProps ) => {
11
14
return (
12
15
< >
13
16
< Head >
@@ -27,16 +30,24 @@ const App: React.FC<AppProps> = ({ Component, pageProps }) => {
27
30
content = "c1kuD-K2HIVF635lypcsWPoD4kilo5-jA_wBFyT4uMY"
28
31
/>
29
32
30
- < meta name = "csrf-token" content = "$CSRFTOKEN$" />
33
+ < meta name = "csrf-token" content = { csrfToken } />
31
34
</ Head >
32
35
< ThemeProvider >
33
- < SetTheme themeProps = { pageProps . themeProps } />
36
+ < SetTheme themeProps = { themeProps } />
34
37
< Component { ...pageProps } />
35
38
</ ThemeProvider >
36
39
</ >
37
40
)
38
41
}
39
42
43
+ MyApp . getInitialProps = async ( appContext : AppContext ) => {
44
+ const { ctx } = appContext
45
+ // calls page's `getInitialProps` and fills `appProps.pageProps`
46
+ const appProps = await App . getInitialProps ( appContext ) ;
47
+
48
+ return { ...appProps , themeProps : getThemeProps ( ctx . req ) , csrfToken : ( ctx . req as any ) . csrfToken ( ) }
49
+ }
50
+
40
51
const SetTheme = ( { themeProps } : { themeProps : typeof defaultThemeProps } ) => {
41
52
// Cause primer/components to re-evaluate the 'auto' color mode on client side render
42
53
const { setColorMode } = useTheme ( )
@@ -48,4 +59,4 @@ const SetTheme = ({ themeProps }: { themeProps: typeof defaultThemeProps }) => {
48
59
return null
49
60
}
50
61
51
- export default App
62
+ export default MyApp
0 commit comments