1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00

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.
This commit is contained in:
Arunoda Susiripala 2017-05-07 19:36:11 -07:00
parent f602f6dc1e
commit be2e5a8c23
2 changed files with 13 additions and 4 deletions

View file

@ -67,10 +67,10 @@ export class Head extends Component {
render () { render () {
const { head, styles, __NEXT_DATA__ } = this.context._documentProps const { head, styles, __NEXT_DATA__ } = this.context._documentProps
const { pathname, buildId, assetPrefix } = __NEXT_DATA__ const { realPathname, buildId, assetPrefix } = __NEXT_DATA__
return <head> return <head>
<link rel='preload' href={`${assetPrefix}/_next/${buildId}/page${pathname}`} as='script' /> <link rel='preload' href={`${assetPrefix}/_next/${buildId}/page${realPathname}`} as='script' />
<link rel='preload' href={`${assetPrefix}/_next/${buildId}/page/_error`} as='script' /> <link rel='preload' href={`${assetPrefix}/_next/${buildId}/page/_error`} as='script' />
{this.getPreloadMainLinks()} {this.getPreloadMainLinks()}
{(head || []).map((h, i) => React.cloneElement(h, { key: i }))} {(head || []).map((h, i) => React.cloneElement(h, { key: i }))}
@ -133,7 +133,7 @@ export class NextScript extends Component {
render () { render () {
const { staticMarkup, __NEXT_DATA__ } = this.context._documentProps const { staticMarkup, __NEXT_DATA__ } = this.context._documentProps
const { pathname, buildId, assetPrefix } = __NEXT_DATA__ const { realPathname, buildId, assetPrefix } = __NEXT_DATA__
return <div> return <div>
{staticMarkup ? null : <script dangerouslySetInnerHTML={{ {staticMarkup ? null : <script dangerouslySetInnerHTML={{
@ -147,7 +147,7 @@ export class NextScript extends Component {
} }
` `
}} />} }} />}
<script async type='text/javascript' src={`${assetPrefix}/_next/${buildId}/page${pathname}`} /> <script async type='text/javascript' src={`${assetPrefix}/_next/${buildId}/page${realPathname}`} />
<script async type='text/javascript' src={`${assetPrefix}/_next/${buildId}/page/_error`} /> <script async type='text/javascript' src={`${assetPrefix}/_next/${buildId}/page/_error`} />
{staticMarkup ? null : this.getScripts()} {staticMarkup ? null : this.getScripts()}
</div> </div>

View file

@ -90,10 +90,18 @@ async function doRender (req, res, pathname, query, {
if (res.finished) return if (res.finished) return
// realPathname is pretty useful in the document.js
// In there, we include a proper valid resolved path.
// That helps us to server that JSON page directly from a static server.
// Basically, this is a requirment for next-export
const pageRealPath = await resolvePath(join(dir, 'pages', pathname))
const realPathname = pageRealPath.replace(join(dir, 'pages'), '')
const doc = createElement(Document, { const doc = createElement(Document, {
__NEXT_DATA__: { __NEXT_DATA__: {
props, props,
pathname, pathname,
realPathname,
query, query,
buildId, buildId,
buildStats, buildStats,
@ -101,6 +109,7 @@ async function doRender (req, res, pathname, query, {
err: (err) ? serializeError(dev, err) : null err: (err) ? serializeError(dev, err) : null
}, },
dev, dev,
dir,
staticMarkup, staticMarkup,
...docProps ...docProps
}) })