forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make fetch polyfill work with ky-universal (vercel#12804)
- Loading branch information
1 parent
9cb75d5
commit 93c8149
Showing
15 changed files
with
286 additions
and
36 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import fetch, { | ||
Headers, | ||
Request, | ||
Response, | ||
} from 'next/dist/compiled/node-fetch' | ||
|
||
// Polyfill fetch() in the Node.js environment | ||
if (!global.fetch) { | ||
global.fetch = fetch | ||
global.Headers = Headers | ||
global.Request = Request | ||
global.Response = Response | ||
} |
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
14 changes: 14 additions & 0 deletions
14
test/integration/fetch-polyfill-ky-universal/api-server.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,14 @@ | ||
const http = require('http') | ||
const port = process.env.PORT || 3000 | ||
|
||
const server = new http.Server(async (req, res) => { | ||
res.end(JSON.stringify({ foo: 'bar' })) | ||
}) | ||
|
||
server.listen(port, err => { | ||
if (err) { | ||
throw err | ||
} | ||
|
||
console.log(`> Ready on http://localhost:${port}`) | ||
}) |
7 changes: 7 additions & 0 deletions
7
test/integration/fetch-polyfill-ky-universal/api/api-route.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,7 @@ | ||
import ky from 'ky-universal' | ||
|
||
export default async function ApiRoute(_req, res) { | ||
const port = process.env.NEXT_PUBLIC_API_PORT | ||
const json = await ky.get(`http://localhost:${port}/`).json() | ||
res.json(json) | ||
} |
13 changes: 13 additions & 0 deletions
13
test/integration/fetch-polyfill-ky-universal/pages/getinitialprops.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,13 @@ | ||
import ky from 'ky-universal' | ||
|
||
export default function SSRPageWithGetInitialProps({ data }) { | ||
return <div>{data.foo}</div> | ||
} | ||
|
||
SSRPageWithGetInitialProps.getInitialProps = async () => { | ||
const port = process.env.NEXT_PUBLIC_API_PORT | ||
const json = await ky.get(`http://localhost:${port}/`).json() | ||
return { | ||
data: json, | ||
} | ||
} |
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 @@ | ||
import ky from 'ky-universal' | ||
|
||
export default function SSRPage({ data }) { | ||
return <div>{data.foo}</div> | ||
} | ||
|
||
export async function getServerSideProps() { | ||
const port = process.env.NEXT_PUBLIC_API_PORT | ||
const json = await ky.get(`http://localhost:${port}/`).json() | ||
return { | ||
props: { | ||
data: json, | ||
}, | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
test/integration/fetch-polyfill-ky-universal/pages/static.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 @@ | ||
import ky from 'ky-universal' | ||
|
||
export default function StaticPage({ data }) { | ||
return <div>{data.foo}</div> | ||
} | ||
|
||
export async function getStaticProps() { | ||
const port = process.env.NEXT_PUBLIC_API_PORT | ||
const json = await ky.get(`http://localhost:${port}/`).json() | ||
return { | ||
props: { | ||
data: json, | ||
}, | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
test/integration/fetch-polyfill-ky-universal/serverless-server.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,44 @@ | ||
// This is not the correct way to implement the Next.js serverless target for production traffic | ||
// It is only used for testing cases of rendering specific pages in the integration test suite | ||
|
||
const path = require('path') | ||
const http = require('http') | ||
const send = require('send') | ||
|
||
const server = http.createServer((req, res) => { | ||
if (req.url === '/ssr') { | ||
return require('./.next/serverless/pages/ssr.js').render(req, res) | ||
} | ||
|
||
if (req.url === '/getinitialprops') { | ||
return require('./.next/serverless/pages/getinitialprops.js').render( | ||
req, | ||
res | ||
) | ||
} | ||
|
||
if (req.url === '/api/api-route') { | ||
return require('./.next/serverless/pages/api/api-route.js').default( | ||
req, | ||
res | ||
) | ||
} | ||
|
||
if (req.url === '/static') { | ||
return send( | ||
req, | ||
path.join(__dirname, '.next/serverless/pages/static.html') | ||
).pipe(res) | ||
} | ||
|
||
if (req.url.startsWith('/_next')) { | ||
send( | ||
req, | ||
path.join(__dirname, '.next', req.url.split('/_next').pop()) | ||
).pipe(res) | ||
} | ||
}) | ||
|
||
server.listen(process.env.PORT, () => { | ||
console.log('ready on', process.env.PORT) | ||
}) |
138 changes: 138 additions & 0 deletions
138
test/integration/fetch-polyfill-ky-universal/test/index.test.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,138 @@ | ||
/* eslint-env jest */ | ||
/* global jasmine */ | ||
import fs from 'fs-extra' | ||
import { join } from 'path' | ||
import { | ||
killApp, | ||
findPort, | ||
launchApp, | ||
initNextServerScript, | ||
renderViaHTTP, | ||
nextBuild, | ||
nextStart, | ||
} from 'next-test-utils' | ||
import clone from 'clone' | ||
|
||
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2 | ||
const appDir = join(__dirname, '../') | ||
const nextConfig = join(appDir, 'next.config.js') | ||
let appPort | ||
let app | ||
let apiServerPort | ||
let apiServer | ||
|
||
const startApiServer = async (optEnv = {}, opts) => { | ||
const scriptPath = join(appDir, 'api-server.js') | ||
apiServerPort = await findPort() | ||
const env = Object.assign( | ||
{}, | ||
clone(process.env), | ||
{ PORT: `${apiServerPort}` }, | ||
optEnv | ||
) | ||
|
||
apiServer = await initNextServerScript( | ||
scriptPath, | ||
/ready on/i, | ||
env, | ||
/ReferenceError: options is not defined/, | ||
opts | ||
) | ||
} | ||
|
||
const startServerlessServer = async (optEnv = {}, opts) => { | ||
const scriptPath = join(appDir, 'serverless-server.js') | ||
appPort = await findPort() | ||
const env = Object.assign( | ||
{}, | ||
clone(process.env), | ||
{ PORT: `${appPort}` }, | ||
optEnv | ||
) | ||
|
||
return await initNextServerScript( | ||
scriptPath, | ||
/ready on/i, | ||
env, | ||
/ReferenceError: options is not defined/, | ||
opts | ||
) | ||
} | ||
|
||
function runTests() { | ||
it('includes polyfilled fetch when using getStaticProps', async () => { | ||
const html = await renderViaHTTP(appPort, '/static') | ||
expect(html).toMatch(/bar/) | ||
}) | ||
it('includes polyfilled fetch when using getServerSideProps', async () => { | ||
const html = await renderViaHTTP(appPort, '/ssr') | ||
expect(html).toMatch(/bar/) | ||
}) | ||
it('includes polyfilled fetch when using getInitialProps', async () => { | ||
const html = await renderViaHTTP(appPort, '/getinitialprops') | ||
expect(html).toMatch(/bar/) | ||
}) | ||
} | ||
|
||
describe('Fetch polyfill with ky-universal', () => { | ||
describe('dev support', () => { | ||
beforeAll(async () => { | ||
appPort = await findPort() | ||
await startApiServer() | ||
app = await launchApp(appDir, appPort, { | ||
env: { | ||
NEXT_PUBLIC_API_PORT: apiServerPort, | ||
}, | ||
}) | ||
}) | ||
afterAll(async () => { | ||
await killApp(app) | ||
await killApp(apiServer) | ||
}) | ||
|
||
runTests() | ||
}) | ||
|
||
describe('Server support', () => { | ||
beforeAll(async () => { | ||
await startApiServer() | ||
await nextBuild(appDir, [], { | ||
env: { | ||
NEXT_PUBLIC_API_PORT: apiServerPort, | ||
}, | ||
}) | ||
appPort = await findPort() | ||
app = await nextStart(appDir, appPort) | ||
}) | ||
afterAll(async () => { | ||
await killApp(app) | ||
await killApp(apiServer) | ||
}) | ||
|
||
runTests() | ||
}) | ||
|
||
describe('Serverless support', () => { | ||
beforeAll(async () => { | ||
await fs.writeFile( | ||
nextConfig, | ||
`module.exports = { target: 'serverless' }` | ||
) | ||
await startApiServer() | ||
await nextBuild(appDir, [], { | ||
env: { | ||
NEXT_PUBLIC_API_PORT: apiServerPort, | ||
}, | ||
}) | ||
appPort = await findPort() | ||
app = await startServerlessServer() | ||
}) | ||
afterAll(async () => { | ||
await killApp(app) | ||
await fs.remove(nextConfig) | ||
await killApp(apiServer) | ||
}) | ||
|
||
runTests() | ||
}) | ||
}) |
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