-
-
Notifications
You must be signed in to change notification settings - Fork 797
/
index-browser.tsx
135 lines (118 loc) · 3.66 KB
/
index-browser.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import "./global"
import type {ClientPlugin, BlitzPluginWithProvider} from "blitz"
import {reduceBlitzClientPlugins, Ctx} from "blitz"
import React, {ReactNode} from "react"
import {withSuperJSONPage} from "./superjson"
import {UrlObject} from "url"
import {AppPropsType} from "next/dist/shared/lib/utils"
import type {Router} from "next/router"
import {BlitzProvider} from "./provider"
import dynamic from "next/dynamic"
export {Routes} from ".blitz"
const Head = dynamic(
() =>
import("next/head").then((mod) => ({
default: mod.default,
})),
{
ssr: false,
loading: () => null,
},
)
export {BlitzProvider} from "./provider"
const buildWithBlitz = (withPlugins: BlitzPluginWithProvider) => {
return function withBlitzAppRoot(UserAppRoot: React.ComponentType<AppProps>) {
const BlitzOuterRoot = (props: AppProps<{dehydratedState: unknown}>) => {
const component = React.useMemo(() => withPlugins(props.Component), [props.Component])
React.useEffect(() => {
// supress first render flicker
setTimeout(() => {
document.documentElement.classList.add("blitz-first-render-complete")
})
}, [])
const children = (
<>
{props.Component.suppressFirstRenderFlicker && <NoPageFlicker />}
<UserAppRoot {...props} Component={component} />
</>
)
return (
<BlitzProvider dehydratedState={props.pageProps?.dehydratedState}>{children}</BlitzProvider>
)
}
Object.assign(BlitzOuterRoot, UserAppRoot)
return withSuperJSONPage(BlitzOuterRoot)
}
}
interface RouteUrlObject extends Pick<UrlObject, "pathname" | "query" | "href"> {
pathname: string
}
type RedirectAuthenticatedTo = string | RouteUrlObject | false
type RedirectAuthenticatedToFnCtx = {
session: Ctx["session"]["$publicData"]
}
type RedirectAuthenticatedToFn = (args: RedirectAuthenticatedToFnCtx) => RedirectAuthenticatedTo
export type BlitzPage<P = {}> = React.ComponentType<P> & {
getLayout?: (component: React.JSX.Element) => React.JSX.Element
authenticate?: boolean | {redirectTo?: string | RouteUrlObject; role?: string | Array<string>}
suppressFirstRenderFlicker?: boolean
redirectAuthenticatedTo?: RedirectAuthenticatedTo | RedirectAuthenticatedToFn
}
export type BlitzLayout<P = {}> = React.ComponentType<P & {children: ReactNode}> & {
authenticate?: boolean | {redirectTo?: string | RouteUrlObject}
redirectAuthenticatedTo?: RedirectAuthenticatedTo | RedirectAuthenticatedToFn
}
export type AppProps<P = {}> = AppPropsType<Router, P> & {
Component: BlitzPage
}
const setupBlitzClient = <TPlugins extends readonly ClientPlugin<object>[]>({
plugins,
}: {
plugins: TPlugins
}) => {
const {exports, withPlugins} = reduceBlitzClientPlugins({plugins})
const withBlitz = buildWithBlitz(withPlugins)
// todo: finish this
// Used to build BlitzPage type
// const types = {} as {plugins: typeof plugins}
return {
withBlitz,
...exports,
}
}
export {setupBlitzClient}
const customCSS = `
body::before {
content: "";
display: block;
position: fixed;
width: 100%;
height: 100%;
top: 0;
left: 0;
z-index: 99999;
background-color: white;
}
.blitz-first-render-complete body::before {
display: none;
}
`
const noscriptCSS = `
body::before {
content: none
}
`
export const NoPageFlicker = () => {
return (
<Head>
<style dangerouslySetInnerHTML={{__html: customCSS}} />
<noscript>
<style dangerouslySetInnerHTML={{__html: noscriptCSS}} />
</noscript>
</Head>
)
}
export * from "./error-boundary"
export * from "./error-component"
export * from "./use-params"
export * from "./router-context"