Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
4c8ff7c8
Unverified
Commit
4c8ff7c8
authored
Apr 30, 2020
by
Tim Neutkens
Committed by
GitHub
Apr 30, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Polyfill fetch by default (#12353)
parent
0710b6b8
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
260 additions
and
0 deletions
+260
-0
next-serverless-loader.ts
...ages/next/build/webpack/loaders/next-serverless-loader.ts
+10
-0
worker.js
packages/next/export/worker.js
+8
-0
next-server.ts
packages/next/next-server/server/next-server.ts
+8
-0
static-paths-worker.ts
packages/next/server/static-paths-worker.ts
+8
-0
api-server.js
test/integration/fetch-polyfill/api-server.js
+14
-0
getinitialprops.js
test/integration/fetch-polyfill/pages/getinitialprops.js
+12
-0
ssr.js
test/integration/fetch-polyfill/pages/ssr.js
+14
-0
static.js
test/integration/fetch-polyfill/pages/static.js
+14
-0
serverless-server.js
test/integration/fetch-polyfill/serverless-server.js
+34
-0
index.test.js
test/integration/fetch-polyfill/test/index.test.js
+138
-0
No files found.
packages/next/build/webpack/loaders/next-serverless-loader.ts
View file @
4c8ff7c8
...
...
@@ -139,6 +139,11 @@ const nextServerlessLoader: loader.Loader = function() {
return
`
import initServer from 'next-plugin-loader?middleware=on-init-server!'
import onError from 'next-plugin-loader?middleware=on-error-server!'
import fetch from 'next/dist/compiled/node-fetch'
if(!global.fetch) {
global.fetch = fetch
}
${
runtimeConfigImports
}
${
/*
...
...
@@ -203,6 +208,11 @@ const nextServerlessLoader: loader.Loader = function() {
return
`
import initServer from 'next-plugin-loader?middleware=on-init-server!'
import onError from 'next-plugin-loader?middleware=on-error-server!'
import fetch from 'next/dist/compiled/node-fetch'
if(!global.fetch) {
global.fetch = fetch
}
${
runtimeConfigImports
}
${
// this needs to be called first so its available for any other imports
...
...
packages/next/export/worker.js
View file @
4c8ff7c8
...
...
@@ -10,6 +10,14 @@ import { getRouteMatcher } from '../next-server/lib/router/utils/route-matcher'
import
{
getRouteRegex
}
from
'../next-server/lib/router/utils/route-regex'
import
{
normalizePagePath
}
from
'../next-server/server/normalize-page-path'
import
{
SERVER_PROPS_EXPORT_ERROR
}
from
'../lib/constants'
import
fetch
from
'next/dist/compiled/node-fetch'
// @ts-ignore fetch exists globally
if
(
!
global
.
fetch
)
{
// Polyfill fetch() in the Node.js environment
// @ts-ignore fetch exists globally
global
.
fetch
=
fetch
}
const
envConfig
=
require
(
'../next-server/lib/runtime-config'
)
const
writeFileP
=
promisify
(
writeFile
)
...
...
packages/next/next-server/server/next-server.ts
View file @
4c8ff7c8
...
...
@@ -65,6 +65,14 @@ import { execOnce } from '../lib/utils'
import
{
isBlockedPage
}
from
'./utils'
import
{
compile
as
compilePathToRegex
}
from
'next/dist/compiled/path-to-regexp'
import
{
loadEnvConfig
}
from
'../../lib/load-env-config'
import
fetch
from
'next/dist/compiled/node-fetch'
// @ts-ignore fetch exists globally
if
(
!
global
.
fetch
)
{
// Polyfill fetch() in the Node.js environment
// @ts-ignore fetch exists globally
global
.
fetch
=
fetch
}
const
getCustomRouteMatcher
=
pathMatch
(
true
)
...
...
packages/next/server/static-paths-worker.ts
View file @
4c8ff7c8
import
{
buildStaticPaths
}
from
'../build/utils'
import
{
loadComponents
}
from
'../next-server/server/load-components'
import
fetch
from
'next/dist/compiled/node-fetch'
// @ts-ignore fetch exists globally
if
(
!
global
.
fetch
)
{
// Polyfill fetch() in the Node.js environment
// @ts-ignore fetch exists globally
global
.
fetch
=
fetch
}
let
workerWasUsed
=
false
...
...
test/integration/fetch-polyfill/api-server.js
0 → 100644
View file @
4c8ff7c8
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
}
`
)
})
test/integration/fetch-polyfill/pages/getinitialprops.js
0 → 100644
View file @
4c8ff7c8
export
default
function
SSRPageWithGetInitialProps
({
data
})
{
return
<
div
>
{
data
.
foo
}
<
/div
>
}
SSRPageWithGetInitialProps
.
getInitialProps
=
async
()
=>
{
const
port
=
process
.
env
.
NEXT_PUBLIC_API_PORT
const
res
=
await
fetch
(
`http://localhost:
${
port
}
/`
)
const
json
=
await
res
.
json
()
return
{
data
:
json
,
}
}
test/integration/fetch-polyfill/pages/ssr.js
0 → 100644
View file @
4c8ff7c8
export
default
function
SSRPage
({
data
})
{
return
<
div
>
{
data
.
foo
}
<
/div
>
}
export
async
function
getServerSideProps
()
{
const
port
=
process
.
env
.
NEXT_PUBLIC_API_PORT
const
res
=
await
fetch
(
`http://localhost:
${
port
}
/`
)
const
json
=
await
res
.
json
()
return
{
props
:
{
data
:
json
,
},
}
}
test/integration/fetch-polyfill/pages/static.js
0 → 100644
View file @
4c8ff7c8
export
default
function
StaticPage
({
data
})
{
return
<
div
>
{
data
.
foo
}
<
/div
>
}
export
async
function
getStaticProps
()
{
const
port
=
process
.
env
.
NEXT_PUBLIC_API_PORT
const
res
=
await
fetch
(
`http://localhost:
${
port
}
/`
)
const
json
=
await
res
.
json
()
return
{
props
:
{
data
:
json
,
},
}
}
test/integration/fetch-polyfill/serverless-server.js
0 → 100644
View file @
4c8ff7c8
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
===
'/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
)
})
test/integration/fetch-polyfill/test/index.test.js
0 → 100644
View file @
4c8ff7c8
/* 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
(
'API routes'
,
()
=>
{
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
()
})
})
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment