Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update to make sure preview mode works with getServerSideProps (#10813)
* Update to make sure preview mode works with getServerSideProps * Update to only parse previewData in GS(S)P mode
- Loading branch information
Showing
8 changed files
with
423 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
test/integration/getserversideprops-preview/pages/api/preview.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export default (req, res) => { | ||
res.setPreviewData(req.query) | ||
res.status(200).end() | ||
} |
4 changes: 4 additions & 0 deletions
4
test/integration/getserversideprops-preview/pages/api/reset.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export default (req, res) => { | ||
res.clearPreviewData() | ||
res.status(200).end() | ||
} |
15 changes: 15 additions & 0 deletions
15
test/integration/getserversideprops-preview/pages/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export function getServerSideProps({ preview, previewData }) { | ||
return { props: { hasProps: true, preview, previewData } } | ||
} | ||
|
||
export default function({ hasProps, preview, previewData }) { | ||
if (!hasProps) { | ||
return <pre id="props-pre">Has No Props</pre> | ||
} | ||
|
||
return ( | ||
<pre id="props-pre"> | ||
{JSON.stringify(preview) + ' and ' + JSON.stringify(previewData)} | ||
</pre> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const http = require('http') | ||
const url = require('url') | ||
const fs = require('fs') | ||
const path = require('path') | ||
const server = http.createServer((req, res) => { | ||
let { pathname } = url.parse(req.url) | ||
if (pathname.startsWith('/_next/data')) { | ||
pathname = pathname | ||
.replace(`/_next/data/${process.env.BUILD_ID}/`, '/') | ||
.replace(/\.json$/, '') | ||
} | ||
console.log('serving', pathname) | ||
|
||
if (pathname === '/favicon.ico') { | ||
res.statusCode = 404 | ||
return res.end() | ||
} | ||
|
||
if (pathname.startsWith('/_next/static/')) { | ||
res.write( | ||
fs.readFileSync( | ||
path.join( | ||
__dirname, | ||
'./.next/static/', | ||
pathname.slice('/_next/static/'.length) | ||
), | ||
'utf8' | ||
) | ||
) | ||
return res.end() | ||
} else { | ||
const re = require(`./.next/serverless/pages${pathname}`) | ||
return typeof re.render === 'function' | ||
? re.render(req, res) | ||
: re.default(req, res) | ||
} | ||
}) | ||
|
||
server.listen(process.env.PORT, () => { | ||
console.log('ready on', process.env.PORT) | ||
}) |
Oops, something went wrong.