Skip to content

Commit be2e5a8

Browse files
committed
Use the realPathname for script tags in document.js
This will help us to fetch these scripts directly from a static server since these paths are resolved.
1 parent f602f6d commit be2e5a8

2 files changed

Lines changed: 13 additions & 4 deletions

File tree

server/document.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ export class Head extends Component {
6767

6868
render () {
6969
const { head, styles, __NEXT_DATA__ } = this.context._documentProps
70-
const { pathname, buildId, assetPrefix } = __NEXT_DATA__
70+
const { realPathname, buildId, assetPrefix } = __NEXT_DATA__
7171

7272
return <head>
73-
<link rel='preload' href={`${assetPrefix}/_next/${buildId}/page${pathname}`} as='script' />
73+
<link rel='preload' href={`${assetPrefix}/_next/${buildId}/page${realPathname}`} as='script' />
7474
<link rel='preload' href={`${assetPrefix}/_next/${buildId}/page/_error`} as='script' />
7575
{this.getPreloadMainLinks()}
7676
{(head || []).map((h, i) => React.cloneElement(h, { key: i }))}
@@ -133,7 +133,7 @@ export class NextScript extends Component {
133133

134134
render () {
135135
const { staticMarkup, __NEXT_DATA__ } = this.context._documentProps
136-
const { pathname, buildId, assetPrefix } = __NEXT_DATA__
136+
const { realPathname, buildId, assetPrefix } = __NEXT_DATA__
137137

138138
return <div>
139139
{staticMarkup ? null : <script dangerouslySetInnerHTML={{
@@ -147,7 +147,7 @@ export class NextScript extends Component {
147147
}
148148
`
149149
}} />}
150-
<script async type='text/javascript' src={`${assetPrefix}/_next/${buildId}/page${pathname}`} />
150+
<script async type='text/javascript' src={`${assetPrefix}/_next/${buildId}/page${realPathname}`} />
151151
<script async type='text/javascript' src={`${assetPrefix}/_next/${buildId}/page/_error`} />
152152
{staticMarkup ? null : this.getScripts()}
153153
</div>

server/render.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,26 @@ async function doRender (req, res, pathname, query, {
9090

9191
if (res.finished) return
9292

93+
// realPathname is pretty useful in the document.js
94+
// In there, we include a proper valid resolved path.
95+
// That helps us to server that JSON page directly from a static server.
96+
// Basically, this is a requirment for next-export
97+
const pageRealPath = await resolvePath(join(dir, 'pages', pathname))
98+
const realPathname = pageRealPath.replace(join(dir, 'pages'), '')
99+
93100
const doc = createElement(Document, {
94101
__NEXT_DATA__: {
95102
props,
96103
pathname,
104+
realPathname,
97105
query,
98106
buildId,
99107
buildStats,
100108
assetPrefix,
101109
err: (err) ? serializeError(dev, err) : null
102110
},
103111
dev,
112+
dir,
104113
staticMarkup,
105114
...docProps
106115
})

0 commit comments

Comments
 (0)