Skip to content

Commit 5ac100a

Browse files
committed
fix: fixed jsonp rendering
1 parent b1234eb commit 5ac100a

File tree

4 files changed

+33
-13
lines changed

4 files changed

+33
-13
lines changed

src/server/events.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const EV_BEFORE_HYDRATION = '_next_static_before_hydration'
2+
export const EV_AFTER_HYDRATION = '_next_static_after_hydration'

src/server/jsonp.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { NextApiRequest, NextApiResponse } from 'next'
2+
import { EV_BEFORE_HYDRATION } from './events.js'
23

34
// see https://github.com/expressjs/express/blob/158a17031a2668269aedb31ea07b58d6b700272b/lib/response.js#L293
45
export const sendAsJsonP = (
@@ -51,12 +52,20 @@ export const sendAsJsonP = (
5152
console.warn('[next-static]: "rootElement" was provided but not of expected type HTMLElement, please provide a valid element.');
5253
return Promise.reject();
5354
}
54-
var thisElement = rootElement && rootElement instanceof HTMLElement ? rootElement : document.body;
55-
thisElement.insertAdjacentHTML('beforeend', manifest.content);
55+
const thisElement = rootElement && rootElement instanceof HTMLElement ? rootElement : document.body;
56+
const scriptNode = document.createRange().createContextualFragment(manifest.scripts);
57+
const applicationRoot = document.createElement('div');
58+
applicationRoot.setAttribute('data-next-static-outer-root', 'true')
59+
applicationRoot.style.cssText = 'visibility: hidden;';
60+
applicationRoot.insertAdjacentHTML('beforeend', manifest.content)
61+
thisElement.insertAdjacentHTML('afterbegin', manifest.styles)
62+
thisElement.appendChild(applicationRoot)
63+
thisElement.appendChild(scriptNode)
5664
return new Promise(function (resolve) {
57-
window.addEventListener('_next_static_hydration_complete', function eventCapture() {
65+
window.addEventListener('${EV_BEFORE_HYDRATION}', function eventCapture() {
5866
resolve();
59-
window.removeEventListener('_next_static_hydration_complete', eventCapture);
67+
applicationRoot.style.cssText = 'visibility: visible;';
68+
window.removeEventListener('${EV_BEFORE_HYDRATION}', eventCapture);
6069
})
6170
})
6271
}

src/shell/app-shell.client.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import ReactDOM from 'react-dom'
33
import React from 'react'
44
import { ApplicationRoot } from './components/ApplicationRoot.js'
55
import application from '@main'
6+
import { EV_AFTER_HYDRATION, EV_BEFORE_HYDRATION } from '../server/events.js'
67

78
async function init() {
89
if (typeof window.__NEXT_STATIC_DATA__ === 'undefined') {
@@ -20,9 +21,9 @@ async function init() {
2021
context,
2122
query,
2223
} = window.__NEXT_STATIC_DATA__
24+
window.dispatchEvent(new CustomEvent(EV_BEFORE_HYDRATION))
2325

2426
await loadableReady()
25-
2627
const { components, props } = await application(context)
2728

2829
for (const [index, Component] of components.entries()) {
@@ -48,7 +49,7 @@ async function init() {
4849
root
4950
)
5051
}
51-
window.dispatchEvent(new CustomEvent('_next_static_hydration_complete'))
52+
window.dispatchEvent(new CustomEvent(EV_AFTER_HYDRATION))
5253
}
5354

5455
;(async () => {

src/shell/app-shell.server.tsx

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,8 @@ export default async function (
9494

9595
const renderedApp = renderToStaticMarkup(Application)
9696

97-
const Body = () => (
97+
const Scripts = () => (
9898
<>
99-
{chunkExtractor.getStyleElements()}
100-
<div
101-
data-next-static-outer-root="true"
102-
dangerouslySetInnerHTML={{ __html: renderedApp }}
103-
/>
10499
{chunkExtractor.getScriptElements()}
105100
<script
106101
id="__NEXT_STATIC_DATA__"
@@ -113,13 +108,26 @@ export default async function (
113108
if (thisOutputMode === 'jsonp') {
114109
return sendAsJsonP(
115110
{
116-
content: renderToStaticMarkup(<Body />),
111+
styles: chunkExtractor.getStyleTags(),
112+
content: renderedApp,
113+
scripts: renderToStaticMarkup(<Scripts />),
117114
},
118115
res,
119116
req
120117
)
121118
}
122119

120+
const Body = () => (
121+
<>
122+
{chunkExtractor.getStyleElements()}
123+
<div
124+
data-next-static-outer-root="true"
125+
dangerouslySetInnerHTML={{ __html: renderedApp }}
126+
/>
127+
<Scripts />
128+
</>
129+
)
130+
123131
const Outer = () => (
124132
<html>
125133
<head>

0 commit comments

Comments
 (0)