-
Notifications
You must be signed in to change notification settings - Fork 28
/
render.tsx
154 lines (144 loc) · 4.38 KB
/
render.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import React from 'react';
import type { NextPage } from 'next';
import fetchDocumentData from './fetchDocumentData';
import type {
ExtendedOptions,
PageObject,
PageProps,
NextApp,
} from '../commonTypes';
import { APP_PATH, NEXT_ROOT_ELEMENT_ID } from '../constants';
import { renderToString } from 'react-dom/server';
import { HeadManagerContext } from 'next/dist/shared/lib/head-manager-context';
import type { DocumentProps } from 'next/document';
import { renderEnhancedApp } from '../_app';
import { executeAsIfOnServer } from '../server';
import {
RenderPage,
ComponentsEnhancer,
NextComponentType,
} from 'next/dist/shared/lib/utils';
// Copied from next.js
// https://github.com/vercel/next.js/blob/b944b06f30322076ceb9020c10cb9bf3448d2659/packages/next/next-server/server/render.tsx#L127
// Now in: https://github.com/vercel/next.js/blob/v11.1.0/packages/next/server/render.tsx#L138
function enhanceComponents(
options: ComponentsEnhancer,
App: NextApp,
Component: NextComponentType
): {
App: NextApp;
Component: NextComponentType;
} {
// For backwards compatibility
if (typeof options === 'function') {
return {
App,
Component: options(Component),
};
}
return {
App: options.enhanceApp ? (options.enhanceApp(App) as NextApp) : App,
Component: options.enhanceComponent
? options.enhanceComponent(Component)
: Component,
};
}
export default async function serverRenderDocument({
options,
appProps,
pageProps,
pageObject,
wrapWithRouter,
}: {
options: ExtendedOptions;
appProps: PageProps | undefined;
pageProps: PageProps | undefined;
pageObject: PageObject;
wrapWithRouter: (children: JSX.Element) => JSX.Element;
}): Promise<JSX.Element> {
return executeAsIfOnServer(async () => {
const { useDocument } = options;
const {
documentFile: { default: DocumentComponent },
appFile: { default: AppComponent },
pageFile: { default: PageComponent },
} = pageObject.files.server;
const render = (App: NextApp, Page: NextPage) => {
return renderEnhancedApp({ App, Page, options, appProps, pageProps });
};
// Return an empty dummy document if useDocument is not enabled
if (!useDocument) {
return (
<html>
<head></head>
<body>
<div id={NEXT_ROOT_ELEMENT_ID}>
{wrapWithRouter(render(AppComponent, PageComponent))}
</div>
</body>
</html>
);
}
const renderPage: RenderPage = (options = {}) => {
const {
App: EnhancedApp,
Component: EnhancedComponent,
} = enhanceComponents(options, AppComponent, PageComponent);
let head: JSX.Element[] = [];
const html = renderToString(
// @NOTE: implemented from:
// https://github.com/vercel/next.js/blob/v10.0.3/packages/next/next-server/server/render.tsx#L561
// Now in: https://github.com/vercel/next.js/blob/v11.1.0/packages/next/server/render.tsx#L639
<HeadManagerContext.Provider
value={{
updateHead: (state) => {
head = state;
},
mountedInstances: new Set(),
}}
>
{wrapWithRouter(render(EnhancedApp, EnhancedComponent))}
</HeadManagerContext.Provider>
);
return { html, head };
};
const initialProps = await fetchDocumentData({
Document: DocumentComponent,
renderPage,
pageObject,
});
const documentProps: DocumentProps = {
...initialProps,
buildManifest: {
ampDevFiles: [],
ampFirstPages: [],
devFiles: [],
lowPriorityFiles: [],
polyfillFiles: [],
pages: {
[APP_PATH]: [],
[pageObject.pagePath]: [],
},
},
__NEXT_DATA__: {
page: pageObject.pagePath,
query: pageObject.query,
buildId: 'next-page-tester',
props: { pageProps },
},
scriptLoader: {},
docComponentsRendered: {},
dangerousAsPath: '',
ampPath: '',
inAmpMode: false,
dynamicImports: [],
isDevelopment: false,
hybridAmp: false,
canonicalBase: '',
headTags: [],
devOnlyCacheBusterQueryString: '',
};
// @ts-expect-error this method doesn't exist since Next.js v11.1.2 and useDocument option is currently disabled
return DocumentComponent.renderDocument(DocumentComponent, documentProps);
});
}