Unverified Commit 64c7571e authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Fix edge case in fetch polyfill (#12832)

* Add tests for getStaticPaths

* Fix edge case in fetch polyfill

Fixes #12794

* Correct typo in tests
parent 59ea81ad
import '../next-server/server/node-polyfill-fetch'
import crypto from 'crypto'
import { promises, writeFileSync } from 'fs'
import Worker from 'jest-worker'
......
import '../next-server/server/node-polyfill-fetch'
import chalk from 'next/dist/compiled/chalk'
import gzipSize from 'next/dist/compiled/gzip-size'
import textTable from 'next/dist/compiled/text-table'
......
......@@ -2,6 +2,30 @@ const http = require('http')
const port = process.env.PORT || 3000
const server = new http.Server(async (req, res) => {
if (req.url === '/usernames') {
return res.end(
JSON.stringify({
usernames: ['a', 'b'],
})
)
}
if (req.url === '/usernames/a') {
return res.end(
JSON.stringify({
from: 'a',
})
)
}
if (req.url === '/usernames/b') {
return res.end(
JSON.stringify({
from: 'b',
})
)
}
res.end(JSON.stringify({ foo: 'bar' }))
})
......
export default function StaticPage({ data }) {
return <div id="username">{data.from}</div>
}
const port = process.env.NEXT_PUBLIC_API_PORT
export async function getStaticPaths() {
const res = await fetch(`http://localhost:${port}/usernames`)
const { usernames } = await res.json()
return {
fallback: false,
paths: usernames.map(username => {
return {
params: {
username,
},
}
}),
}
}
export async function getStaticProps({ params }) {
const res = await fetch(
`http://localhost:${port}/usernames/${params.username}`
)
const json = await res.json()
return {
props: {
data: json,
},
}
}
......@@ -24,6 +24,20 @@ const server = http.createServer((req, res) => {
)
}
if (req.url === '/user/a') {
return send(
req,
path.join(__dirname, '.next/serverless/pages/user/a.html')
).pipe(res)
}
if (req.url === '/user/b') {
return send(
req,
path.join(__dirname, '.next/serverless/pages/user/b.html')
).pipe(res)
}
if (req.url === '/static') {
return send(
req,
......
......@@ -13,6 +13,7 @@ import {
nextStart,
} from 'next-test-utils'
import clone from 'clone'
import cheerio from 'cheerio'
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2
const appDir = join(__dirname, '../')
......@@ -80,6 +81,16 @@ function runTests() {
expect(json.foo).toBe('bar')
})
it('includes polyfilled fetch when using getStaticPaths', async () => {
const htmlA = await renderViaHTTP(appPort, '/user/a')
const $a = cheerio.load(htmlA)
expect($a('#username').text()).toBe('a')
const htmlB = await renderViaHTTP(appPort, '/user/b')
const $b = cheerio.load(htmlB)
expect($b('#username').text()).toBe('b')
})
}
describe('Fetch polyfill', () => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment