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
f0fd4962
Unverified
Commit
f0fd4962
authored
Dec 03, 2021
by
JJ Kasper
Committed by
GitHub
Dec 03, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Revert "Fix running server with Polyfilled fetch (#31935)" (#32100)
This reverts commit
1c199a5e
.
parent
b194664d
Hide whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
163 additions
and
283 deletions
+163
-283
next-server.ts
packages/next/server/next-server.ts
+15
-35
adapter.ts
packages/next/server/web/adapter.ts
+6
-3
next-url.ts
packages/next/server/web/next-url.ts
+97
-109
sandbox.ts
packages/next/server/web/sandbox/sandbox.ts
+0
-13
relativize-url.ts
packages/next/shared/lib/router/utils/relativize-url.ts
+0
-13
server.js
test/integration/custom-server/server.js
+0
-4
index.test.js
test/integration/custom-server/test/index.test.js
+0
-10
_middleware.js
...ation/middleware/core/pages/interface/[id]/_middleware.js
+0
-7
_middleware.js
...ntegration/middleware/core/pages/interface/_middleware.js
+3
-1
_middleware.js
...ntegration/middleware/core/pages/responses/_middleware.js
+3
-3
index.test.js
test/integration/middleware/core/test/index.test.js
+0
-8
next-url.test.ts
test/unit/web-runtime/next-url.test.ts
+39
-77
No files found.
packages/next/server/next-server.ts
View file @
f0fd4962
...
@@ -95,10 +95,10 @@ import { parseNextUrl } from '../shared/lib/router/utils/parse-next-url'
...
@@ -95,10 +95,10 @@ import { parseNextUrl } from '../shared/lib/router/utils/parse-next-url'
import
isError
from
'../lib/is-error'
import
isError
from
'../lib/is-error'
import
{
getMiddlewareInfo
}
from
'./require'
import
{
getMiddlewareInfo
}
from
'./require'
import
{
MIDDLEWARE_ROUTE
}
from
'../lib/constants'
import
{
MIDDLEWARE_ROUTE
}
from
'../lib/constants'
import
{
NextResponse
}
from
'./web/spec-extension/response'
import
{
run
}
from
'./web/sandbox'
import
{
run
}
from
'./web/sandbox'
import
{
addRequestMeta
,
getRequestMeta
}
from
'./request-meta'
import
{
addRequestMeta
,
getRequestMeta
}
from
'./request-meta'
import
{
toNodeHeaders
}
from
'./web/utils'
import
{
toNodeHeaders
}
from
'./web/utils'
import
{
relativizeURL
}
from
'../shared/lib/router/utils/relativize-url'
const
getCustomRouteMatcher
=
pathMatch
(
true
)
const
getCustomRouteMatcher
=
pathMatch
(
true
)
...
@@ -384,13 +384,7 @@ export default class Server {
...
@@ -384,13 +384,7 @@ export default class Server {
parsedUrl
.
query
=
parseQs
(
parsedUrl
.
query
)
parsedUrl
.
query
=
parseQs
(
parsedUrl
.
query
)
}
}
// When there are hostname and port we build an absolute URL
addRequestMeta
(
req
,
'__NEXT_INIT_URL'
,
req
.
url
)
const
initUrl
=
this
.
hostname
&&
this
.
port
?
`http://
${
this
.
hostname
}
:
${
this
.
port
}${
req
.
url
}
`
:
req
.
url
addRequestMeta
(
req
,
'__NEXT_INIT_URL'
,
initUrl
)
addRequestMeta
(
req
,
'__NEXT_INIT_QUERY'
,
{
...
parsedUrl
.
query
})
addRequestMeta
(
req
,
'__NEXT_INIT_QUERY'
,
{
...
parsedUrl
.
query
})
const
url
=
parseNextUrl
({
const
url
=
parseNextUrl
({
...
@@ -673,14 +667,6 @@ export default class Server {
...
@@ -673,14 +667,6 @@ export default class Server {
}): Promise<FetchEventResult | null> {
}): Promise<FetchEventResult | null> {
this.middlewareBetaWarning()
this.middlewareBetaWarning()
// For middleware to "fetch" we must always provide an absolute URL
const url = getRequestMeta(params.request, '__NEXT_INIT_URL')!
if (!url.startsWith('http')) {
throw new Error(
'To use middleware you must provide a `
hostname
` and `
port
` to the Next.js Server'
)
}
const page: { name?: string; params?: { [key: string]: string } } = {}
const page: { name?: string; params?: { [key: string]: string } } = {}
if (await this.hasPage(params.parsedUrl.pathname)) {
if (await this.hasPage(params.parsedUrl.pathname)) {
page.name = params.parsedUrl.pathname
page.name = params.parsedUrl.pathname
...
@@ -695,6 +681,8 @@ export default class Server {
...
@@ -695,6 +681,8 @@ export default class Server {
}
}
}
}
const subreq = params.request.headers[`
x
-
middleware
-
subrequest
`]
const subrequests = typeof subreq === 'string' ? subreq.split(':') : []
const allHeaders = new Headers()
const allHeaders = new Headers()
let result: FetchEventResult | null = null
let result: FetchEventResult | null = null
...
@@ -714,6 +702,14 @@ export default class Server {
...
@@ -714,6 +702,14 @@ export default class Server {
serverless: this._isLikeServerless,
serverless: this._isLikeServerless,
})
})
if (subrequests.includes(middlewareInfo.name)) {
result = {
response: NextResponse.next(),
waitUntil: Promise.resolve(),
}
continue
}
result = await run({
result = await run({
name: middlewareInfo.name,
name: middlewareInfo.name,
paths: middlewareInfo.paths,
paths: middlewareInfo.paths,
...
@@ -725,7 +721,7 @@ export default class Server {
...
@@ -725,7 +721,7 @@ export default class Server {
i18n: this.nextConfig.i18n,
i18n: this.nextConfig.i18n,
trailingSlash: this.nextConfig.trailingSlash,
trailingSlash: this.nextConfig.trailingSlash,
},
},
url:
url
,
url:
getRequestMeta(params.request, '__NEXT_INIT_URL')!
,
page: page,
page: page,
},
},
useCache: !this.nextConfig.experimental.concurrentFeatures,
useCache: !this.nextConfig.experimental.concurrentFeatures,
...
@@ -1185,13 +1181,9 @@ export default class Server {
...
@@ -1185,13 +1181,9 @@ export default class Server {
type: 'route',
type: 'route',
name: 'middleware catchall',
name: 'middleware catchall',
fn: async (req, res, _params, parsed) => {
fn: async (req, res, _params, parsed) => {
if (!this.middleware?.length) {
const fullUrl = getRequestMeta(req, '__NEXT_INIT_URL')
return { finished: false }
}
const initUrl = getRequestMeta(req, '__NEXT_INIT_URL')!
const parsedUrl = parseNextUrl({
const parsedUrl = parseNextUrl({
url:
init
Url,
url:
full
Url,
headers: req.headers,
headers: req.headers,
nextConfig: {
nextConfig: {
basePath: this.nextConfig.basePath,
basePath: this.nextConfig.basePath,
...
@@ -1230,18 +1222,6 @@ export default class Server {
...
@@ -1230,18 +1222,6 @@ export default class Server {
return { finished: true }
return { finished: true }
}
}
if (result.response.headers.has('x-middleware-rewrite')) {
const value = result.response.headers.get('x-middleware-rewrite')!
const rel = relativizeURL(value, initUrl)
result.response.headers.set('x-middleware-rewrite', rel)
}
if (result.response.headers.has('Location')) {
const value = result.response.headers.get('Location')!
const rel = relativizeURL(value, initUrl)
result.response.headers.set('Location', rel)
}
if (
if (
!result.response.headers.has('x-middleware-rewrite') &&
!result.response.headers.has('x-middleware-rewrite') &&
!result.response.headers.has('x-middleware-next') &&
!result.response.headers.has('x-middleware-next') &&
...
...
packages/next/server/web/adapter.ts
View file @
f0fd4962
import
type
{
NextMiddleware
,
RequestData
,
FetchEventResult
}
from
'./types'
import
type
{
NextMiddleware
,
RequestData
,
FetchEventResult
}
from
'./types'
import
type
{
RequestInit
}
from
'./spec-extension/request'
import
{
DeprecationError
}
from
'./error'
import
{
DeprecationError
}
from
'./error'
import
{
fromNodeHeaders
}
from
'./utils'
import
{
fromNodeHeaders
}
from
'./utils'
import
{
NextFetchEvent
}
from
'./spec-extension/fetch-event'
import
{
NextFetchEvent
}
from
'./spec-extension/fetch-event'
import
{
NextRequest
}
from
'./spec-extension/request'
import
{
NextRequest
,
RequestInit
}
from
'./spec-extension/request'
import
{
NextResponse
}
from
'./spec-extension/response'
import
{
NextResponse
}
from
'./spec-extension/response'
import
{
waitUntilSymbol
}
from
'./spec-compliant/fetch-event'
import
{
waitUntilSymbol
}
from
'./spec-compliant/fetch-event'
...
@@ -12,9 +11,13 @@ export async function adapter(params: {
...
@@ -12,9 +11,13 @@ export async function adapter(params: {
page
:
string
page
:
string
request
:
RequestData
request
:
RequestData
}):
Promise
<
FetchEventResult
>
{
}):
Promise
<
FetchEventResult
>
{
const
url
=
params
.
request
.
url
.
startsWith
(
'/'
)
?
`https://
${
params
.
request
.
headers
.
host
}${
params
.
request
.
url
}
`
:
params
.
request
.
url
const
request
=
new
NextRequestHint
({
const
request
=
new
NextRequestHint
({
page
:
params
.
page
,
page
:
params
.
page
,
input
:
params
.
request
.
url
,
input
:
url
,
init
:
{
init
:
{
geo
:
params
.
request
.
geo
,
geo
:
params
.
request
.
geo
,
headers
:
fromNodeHeaders
(
params
.
request
.
headers
),
headers
:
fromNodeHeaders
(
params
.
request
.
headers
),
...
...
packages/next/server/web/next-url.ts
View file @
f0fd4962
import
type
{
PathLocale
}
from
'../../shared/lib/i18n/normalize-locale-path'
import
type
{
PathLocale
}
from
'../../shared/lib/i18n/normalize-locale-path'
import
type
{
DomainLocale
,
I18NConfig
}
from
'../config-shared'
import
type
{
DomainLocale
,
I18NConfig
}
from
'../config-shared'
import
{
getLocaleMetadata
}
from
'../../shared/lib/i18n/get-locale-metadata'
import
{
getLocaleMetadata
}
from
'../../shared/lib/i18n/get-locale-metadata'
import
{
replaceBasePath
}
from
'../router'
import
cookie
from
'next/dist/compiled/cookie'
import
cookie
from
'next/dist/compiled/cookie'
import
{
replaceBasePath
}
from
'../router'
/**
* TODO
*
* - Add comments to the URLNext API.
* - Move internals to be using symbols for its shape.
* - Make sure logging does not show any implementation details.
* - Include in the event payload the nextJS configuration
*/
interface
Options
{
interface
Options
{
base
?:
string
|
URL
basePath
?:
string
basePath
?:
string
headers
?:
{
[
key
:
string
]:
string
|
string
[]
|
undefined
}
headers
?:
{
[
key
:
string
]:
string
|
string
[]
|
undefined
}
i18n
?:
I18NConfig
|
null
i18n
?:
I18NConfig
|
null
trailingSlash
?:
boolean
trailingSlash
?:
boolean
}
}
const
Internal
=
Symbol
(
'NextURLInternal'
)
const
REGEX_LOCALHOST_HOSTNAME
=
/
(?!
^https
?
:
\/\/)(
127
(?:\.(?:
25
[
0-5
]
|2
[
0-4
][
0-9
]
|
[
01
]?[
0-9
][
0-9
]?)){3}
|::1
)
/
export
class
NextURL
extends
URL
{
export
class
NextURL
extends
URL
{
[
Internal
]:
{
private
_basePath
:
string
url
:
URL
private
_locale
?:
{
options
:
Options
defaultLocale
:
string
basePath
:
string
domain
?:
DomainLocale
locale
?:
{
locale
:
string
defaultLocale
:
string
path
:
PathLocale
domain
?:
DomainLocale
redirect
?:
string
locale
:
string
trailingSlash
?:
boolean
path
:
PathLocale
}
redirect
?:
string
private
_options
:
Options
trailingSlash
?:
boolean
private
_url
:
URL
}
constructor
(
input
:
string
,
options
:
Options
=
{})
{
const
url
=
createWHATWGURL
(
input
)
super
(
url
)
this
.
_options
=
options
this
.
_basePath
=
''
this
.
_url
=
url
this
.
analyzeUrl
()
}
}
constructor
(
input
:
string
|
URL
,
base
?:
string
|
URL
,
opts
?:
Options
)
get
absolute
()
{
constructor
(
input
:
string
|
URL
,
opts
?:
Options
)
return
this
.
_url
.
hostname
!==
'localhost'
constructor
(
input
:
string
|
URL
,
baseOrOpts
?:
string
|
URL
|
Options
,
opts
?:
Options
)
{
super
(
'http://127.0.0.1'
)
// This works as a placeholder
let
base
:
undefined
|
string
|
URL
let
options
:
Options
if
(
baseOrOpts
instanceof
URL
||
typeof
baseOrOpts
===
'string'
)
{
base
=
baseOrOpts
options
=
opts
||
{}
}
else
{
options
=
opts
||
baseOrOpts
||
{}
}
this
[
Internal
]
=
{
url
:
parseURL
(
input
,
base
??
options
.
base
),
options
:
options
,
basePath
:
''
,
}
this
.
analyzeUrl
()
}
}
private
analyzeUrl
()
{
analyzeUrl
()
{
const
{
headers
=
{},
basePath
,
i18n
}
=
this
[
Internal
].
options
const
{
headers
=
{},
basePath
,
i18n
}
=
this
.
_
options
if
(
basePath
&&
this
[
Internal
].
url
.
pathname
.
startsWith
(
basePath
))
{
if
(
basePath
&&
this
.
_url
.
pathname
.
startsWith
(
basePath
))
{
this
[
Internal
].
url
.
pathname
=
replaceBasePath
(
this
.
_url
.
pathname
=
replaceBasePath
(
this
.
_url
.
pathname
,
basePath
)
this
[
Internal
].
url
.
pathname
,
this
.
_basePath
=
basePath
basePath
)
this
[
Internal
].
basePath
=
basePath
}
else
{
}
else
{
this
[
Internal
].
basePath
=
''
this
.
_
basePath
=
''
}
}
if
(
i18n
)
{
if
(
i18n
)
{
this
[
Internal
].
locale
=
getLocaleMetadata
({
this
.
_
locale
=
getLocaleMetadata
({
cookies
:
()
=>
{
cookies
:
()
=>
{
const
value
=
headers
[
'cookie'
]
const
value
=
headers
[
'cookie'
]
return
value
return
value
...
@@ -84,156 +73,154 @@ export class NextURL extends URL {
...
@@ -84,156 +73,154 @@ export class NextURL extends URL {
i18n
:
i18n
,
i18n
:
i18n
,
},
},
url
:
{
url
:
{
hostname
:
this
[
Internal
].
url
.
hostname
||
null
,
hostname
:
this
.
_
url
.
hostname
||
null
,
pathname
:
this
[
Internal
].
url
.
pathname
,
pathname
:
this
.
_
url
.
pathname
,
},
},
})
})
if
(
this
[
Internal
].
locale
?.
path
.
detectedLocale
)
{
if
(
this
.
_
locale
?.
path
.
detectedLocale
)
{
this
[
Internal
].
url
.
pathname
=
this
[
Internal
].
locale
!
.
path
.
pathname
this
.
_url
.
pathname
=
this
.
_locale
.
path
.
pathname
}
}
}
}
}
}
private
formatPathname
()
{
formatPathname
()
{
const
{
i18n
}
=
this
[
Internal
].
options
const
{
i18n
}
=
this
.
_
options
let
pathname
=
this
[
Internal
].
url
.
pathname
let
pathname
=
this
.
_
url
.
pathname
if
(
if
(
this
.
_locale
?.
locale
&&
i18n
?.
defaultLocale
!==
this
.
_locale
?.
locale
)
{
this
[
Internal
].
locale
?.
locale
&&
pathname
=
`/
${
this
.
_locale
?.
locale
}
$
{
pathname
}
`
i18n
?.
defaultLocale
!==
this
[
Internal
].
locale
?.
locale
)
{
pathname
=
`/
${
this
[
Internal
].
locale
?.
locale
}
$
{
pathname
}
`
}
}
if (this
[Internal].
basePath) {
if (this
._
basePath) {
pathname = `
$
{
this
[
Internal
].
basePath
}
$
{
pathname
}
`
pathname = `
$
{
this
.
_
basePath
}
$
{
pathname
}
`
}
}
return pathname
return pathname
}
}
public get locale() {
get locale() {
return this[Internal].locale?.locale ?? ''
if (!this._locale) {
throw new TypeError(`
The
URL
is
not
configured
with
i18n
`)
}
return this._locale.locale
}
}
public set locale(locale: string) {
set locale(locale: string) {
if (
if (!this._locale) {
!this[Internal].locale ||
throw new TypeError(`
The
URL
is
not
configured
with
i18n
`)
!this[Internal].options.i18n?.locales.includes(locale)
) {
throw new TypeError(
`
The
NextURL
configuration
includes
no
locale
"${locale}"`
)
}
}
this
[Internal].locale!
.locale = locale
this
._locale
.locale = locale
}
}
get defaultLocale() {
get defaultLocale() {
return this
[Internal].
locale?.defaultLocale
return this
._
locale?.defaultLocale
}
}
get domainLocale() {
get domainLocale() {
return this
[Internal].
locale?.domain
return this
._
locale?.domain
}
}
get searchParams() {
get searchParams() {
return this
[Internal].
url.searchParams
return this
._
url.searchParams
}
}
get host() {
get host() {
return this
[Internal].url.host
return this
.absolute ? this._url.host : ''
}
}
set host(value: string) {
set host(value: string) {
this
[Internal].
url.host = value
this
._
url.host = value
}
}
get hostname() {
get hostname() {
return this
[Internal].url.hostname
return this
.absolute ? this._url.hostname : ''
}
}
set hostname(value: string) {
set hostname(value: string) {
this
[Internal].url.hostname = value
this
._url.hostname = value || 'localhost'
}
}
get port() {
get port() {
return this
[Internal].url.port
return this
.absolute ? this._url.port : ''
}
}
set port(value: string) {
set port(value: string) {
this
[Internal].
url.port = value
this
._
url.port = value
}
}
get protocol() {
get protocol() {
return this
[Internal].url.protocol
return this
.absolute ? this._url.protocol : ''
}
}
set protocol(value: string) {
set protocol(value: string) {
this
[Internal].
url.protocol = value
this
._
url.protocol = value
}
}
get href() {
get href() {
const pathname = this.formatPathname()
const pathname = this.formatPathname()
return `
$
{
this
.
protocol
}
//${this.host}${pathname}${this[Internal].url.search}`
return this.absolute
? `
$
{
this
.
protocol
}
//${this.host}${pathname}${this._url.search}`
:
`
${
pathname
}${
this
.
_url
.
search
}
`
}
}
set href(url: string) {
set href(url: string) {
this
[
Internal
].
url
=
parse
URL
(
url
)
this
._url = createWHATWG
URL(url)
this.analyzeUrl()
this.analyzeUrl()
}
}
get origin() {
get origin() {
return this
[Internal].url.origin
return this
.absolute ? this._url.origin : ''
}
}
get pathname() {
get pathname() {
return this
[Internal].
url.pathname
return this
._
url.pathname
}
}
set pathname(value: string) {
set pathname(value: string) {
this
[Internal].
url.pathname = value
this
._
url.pathname = value
}
}
get hash() {
get hash() {
return this
[Internal].
url.hash
return this
._
url.hash
}
}
set hash(value: string) {
set hash(value: string) {
this
[Internal].
url.hash = value
this
._
url.hash = value
}
}
get search() {
get search() {
return this
[Internal].
url.search
return this
._
url.search
}
}
set search(value: string) {
set search(value: string) {
this
[Internal].
url.search = value
this
._
url.search = value
}
}
get password() {
get password() {
return this
[Internal].
url.password
return this
._
url.password
}
}
set password(value: string) {
set password(value: string) {
this
[Internal].
url.password = value
this
._
url.password = value
}
}
get username() {
get username() {
return this
[Internal].
url.username
return this
._
url.username
}
}
set username(value: string) {
set username(value: string) {
this
[Internal].
url.username = value
this
._
url.username = value
}
}
get basePath() {
get basePath() {
return this
[Internal].
basePath
return this
._
basePath
}
}
set basePath(value: string) {
set basePath(value: string) {
this
[Internal].
basePath = value.startsWith('/') ? value : `
/
$
{
value
}
`
this
._
basePath = value.startsWith('/') ? value : `
/
$
{
value
}
`
}
}
toString() {
toString() {
...
@@ -245,12 +232,13 @@ export class NextURL extends URL {
...
@@ -245,12 +232,13 @@ export class NextURL extends URL {
}
}
}
}
const REGEX_LOCALHOST_HOSTNAME =
function createWHATWGURL(url: string) {
/(?!^https?:\/\/)(127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}|::1|localhost)/
url = url.replace(REGEX_LOCALHOST_HOSTNAME, 'localhost')
return isRelativeURL(url)
? new URL(url.replace(/^\/+/, '/'), new URL('https://localhost'))
: new URL(url)
}
function parseURL(url: string | URL, base?: string | URL) {
function isRelativeURL(url: string) {
return new URL(
return url.startsWith('/')
String(url).replace(REGEX_LOCALHOST_HOSTNAME, 'localhost'),
base && String(base).replace(REGEX_LOCALHOST_HOSTNAME, 'localhost')
)
}
}
packages/next/server/web/sandbox/sandbox.ts
View file @
f0fd4962
...
@@ -18,19 +18,6 @@ export async function run(params: {
...
@@ -18,19 +18,6 @@ export async function run(params: {
runInContext
(
paramPath
)
runInContext
(
paramPath
)
}
}
const
subreq
=
params
.
request
.
headers
[
`x-middleware-subrequest`
]
const
subrequests
=
typeof
subreq
===
'string'
?
subreq
.
split
(
':'
)
:
[]
if
(
subrequests
.
includes
(
params
.
name
))
{
return
{
waitUntil
:
Promise
.
resolve
(),
response
:
new
context
.
Response
(
null
,
{
headers
:
{
'x-middleware-next'
:
'1'
,
},
}),
}
}
return
context
.
_ENTRIES
[
`middleware_
${
params
.
name
}
`
].
default
({
return
context
.
_ENTRIES
[
`middleware_
${
params
.
name
}
`
].
default
({
request
:
params
.
request
,
request
:
params
.
request
,
})
})
...
...
packages/next/shared/lib/router/utils/relativize-url.ts
deleted
100644 → 0
View file @
b194664d
/**
* Given a URL as a string and a base URL it will make the URL relative
* if the parsed protocol and host is the same as the one in the base
* URL. Otherwise it returns the same URL string.
*/
export
function
relativizeURL
(
url
:
string
|
string
,
base
:
string
|
URL
)
{
const
baseURL
=
typeof
base
===
'string'
?
new
URL
(
base
)
:
base
const
relative
=
new
URL
(
url
,
base
)
const
origin
=
`
${
baseURL
.
protocol
}
//
${
baseURL
.
host
}
`
return
`
${
relative
.
protocol
}
//
${
relative
.
host
}
`
===
origin
?
relative
.
toString
().
replace
(
origin
,
''
)
:
relative
.
toString
()
}
test/integration/custom-server/server.js
View file @
f0fd4962
if
(
process
.
env
.
POLYFILL_FETCH
)
{
global
.
fetch
=
require
(
'node-fetch'
).
default
}
const
http
=
require
(
'http'
)
const
http
=
require
(
'http'
)
const
next
=
require
(
'next'
)
const
next
=
require
(
'next'
)
...
...
test/integration/custom-server/test/index.test.js
View file @
f0fd4962
...
@@ -195,14 +195,4 @@ describe('Custom Server', () => {
...
@@ -195,14 +195,4 @@ describe('Custom Server', () => {
}
}
)
)
})
})
describe
(
'with a custom fetch polyfill'
,
()
=>
{
beforeAll
(()
=>
startServer
({
POLYFILL_FETCH
:
'true'
}))
afterAll
(()
=>
killApp
(
server
))
it
(
'should serve internal file from render'
,
async
()
=>
{
const
data
=
await
renderViaHTTP
(
appPort
,
'/static/hello.txt'
)
expect
(
data
).
toMatch
(
/hello world/
)
})
})
})
})
test/integration/middleware/core/pages/interface/[id]/_middleware.js
deleted
100644 → 0
View file @
b194664d
import
{
NextResponse
}
from
'next/server'
export
function
middleware
()
{
const
response
=
NextResponse
.
next
()
response
.
headers
.
set
(
'x-dynamic-path'
,
'true'
)
return
response
}
test/integration/middleware/core/pages/interface/_middleware.js
View file @
f0fd4962
...
@@ -54,7 +54,9 @@ export async function middleware(request) {
...
@@ -54,7 +54,9 @@ export async function middleware(request) {
}
}
if
(
url
.
pathname
.
endsWith
(
'/root-subrequest'
))
{
if
(
url
.
pathname
.
endsWith
(
'/root-subrequest'
))
{
return
fetch
(
url
)
return
fetch
(
`http://
${
request
.
headers
.
get
(
'host'
)}
/interface/root-subrequest`
)
}
}
return
new
Response
(
null
,
{
return
new
Response
(
null
,
{
...
...
test/integration/middleware/core/pages/responses/_middleware.js
View file @
f0fd4962
...
@@ -57,11 +57,11 @@ export async function middleware(request, ev) {
...
@@ -57,11 +57,11 @@ export async function middleware(request, ev) {
ev
.
waitUntil
(
ev
.
waitUntil
(
(
async
()
=>
{
(
async
()
=>
{
writer
.
write
(
encoder
.
encode
(
'this is a streamed '
.
repeat
(
10
)))
writer
.
write
(
encoder
.
encode
(
'this is a streamed '
.
repeat
(
10
)))
await
sleep
(
200
)
await
sleep
(
200
0
)
writer
.
write
(
encoder
.
encode
(
'after 2 seconds '
.
repeat
(
10
)))
writer
.
write
(
encoder
.
encode
(
'after 2 seconds '
.
repeat
(
10
)))
await
sleep
(
200
)
await
sleep
(
200
0
)
writer
.
write
(
encoder
.
encode
(
'after 4 seconds '
.
repeat
(
10
)))
writer
.
write
(
encoder
.
encode
(
'after 4 seconds '
.
repeat
(
10
)))
await
sleep
(
200
)
await
sleep
(
200
0
)
writer
.
close
()
writer
.
close
()
})()
})()
)
)
...
...
test/integration/middleware/core/test/index.test.js
View file @
f0fd4962
...
@@ -447,14 +447,6 @@ function interfaceTests(locale = '') {
...
@@ -447,14 +447,6 @@ function interfaceTests(locale = '') {
const
element
=
await
browser
.
elementByCss
(
'.title'
)
const
element
=
await
browser
.
elementByCss
(
'.title'
)
expect
(
await
element
.
text
()).
toEqual
(
'Dynamic route'
)
expect
(
await
element
.
text
()).
toEqual
(
'Dynamic route'
)
})
})
it
(
`
${
locale
}
allows subrequests without infinite loops`
,
async
()
=>
{
const
res
=
await
fetchViaHTTP
(
context
.
appPort
,
`/interface/root-subrequest`
)
expect
(
res
.
headers
.
get
(
'x-dynamic-path'
)).
toBe
(
'true'
)
})
}
}
function
getCookieFromResponse
(
res
,
cookieName
)
{
function
getCookieFromResponse
(
res
,
cookieName
)
{
...
...
test/unit/web-runtime/next-url.test.ts
View file @
f0fd4962
/* eslint-env jest */
/* eslint-env jest */
import
{
NextURL
}
from
'next/dist/server/web/next-url'
import
{
NextURL
}
from
'next/dist/server/web/next-url'
it
(
'has the right shape
and prototype
'
,
()
=>
{
it
(
'has the right shape'
,
()
=>
{
const
parsed
=
new
NextURL
(
'/about?param1=value1'
,
'http://127.0.0.1'
)
const
parsed
=
new
NextURL
(
'/about?param1=value1'
)
expect
(
parsed
).
toBeInstanceOf
(
URL
)
expect
(
parsed
).
toBeInstanceOf
(
URL
)
})
})
it
(
'allows to
the pathname
'
,
async
()
=>
{
it
(
'allows to
format relative urls
'
,
async
()
=>
{
const
parsed
=
new
NextURL
(
'/about?param1=value1'
,
'http://127.0.0.1:3000'
)
const
parsed
=
new
NextURL
(
'/about?param1=value1'
)
expect
(
parsed
.
basePath
).
toEqual
(
''
)
expect
(
parsed
.
basePath
).
toEqual
(
''
)
expect
(
parsed
.
hostname
).
toEqual
(
'
localhost
'
)
expect
(
parsed
.
hostname
).
toEqual
(
''
)
expect
(
parsed
.
host
).
toEqual
(
'
localhost:3000
'
)
expect
(
parsed
.
host
).
toEqual
(
''
)
expect
(
parsed
.
href
).
toEqual
(
'
http://localhost:3000
/about?param1=value1'
)
expect
(
parsed
.
href
).
toEqual
(
'/about?param1=value1'
)
parsed
.
pathname
=
'/hihi'
parsed
.
pathname
=
'/hihi'
expect
(
parsed
.
href
).
toEqual
(
'
http://localhost:3000
/hihi?param1=value1'
)
expect
(
parsed
.
href
).
toEqual
(
'/hihi?param1=value1'
)
})
})
it
(
'allows to change the host'
,
()
=>
{
it
(
'allows to change the host
of a relative url
'
,
()
=>
{
const
parsed
=
new
NextURL
(
'/about?param1=value1'
,
'http://127.0.0.1'
)
const
parsed
=
new
NextURL
(
'/about?param1=value1'
)
expect
(
parsed
.
hostname
).
toEqual
(
'
localhost
'
)
expect
(
parsed
.
hostname
).
toEqual
(
''
)
expect
(
parsed
.
host
).
toEqual
(
'
localhost
'
)
expect
(
parsed
.
host
).
toEqual
(
''
)
expect
(
parsed
.
href
).
toEqual
(
'
http://localhost
/about?param1=value1'
)
expect
(
parsed
.
href
).
toEqual
(
'/about?param1=value1'
)
parsed
.
hostname
=
'foo.com'
parsed
.
hostname
=
'foo.com'
expect
(
parsed
.
hostname
).
toEqual
(
'foo.com'
)
expect
(
parsed
.
hostname
).
toEqual
(
'foo.com'
)
expect
(
parsed
.
host
).
toEqual
(
'foo.com'
)
expect
(
parsed
.
host
).
toEqual
(
'foo.com'
)
expect
(
parsed
.
href
).
toEqual
(
'http://foo.com/about?param1=value1'
)
expect
(
parsed
.
href
).
toEqual
(
'http
s
://foo.com/about?param1=value1'
)
expect
(
parsed
.
toString
()).
toEqual
(
'http://foo.com/about?param1=value1'
)
expect
(
parsed
.
toString
()).
toEqual
(
'http
s
://foo.com/about?param1=value1'
)
})
})
it
(
'does noop changing to an invalid hostname'
,
()
=>
{
it
(
'allows to change the hostname of a relative url'
,
()
=>
{
const
url
=
new
NextURL
(
'/example'
)
url
.
hostname
=
'foo.com'
expect
(
url
.
toString
()).
toEqual
(
'https://foo.com/example'
)
})
it
(
'allows to remove the hostname of an absolute url'
,
()
=>
{
const
url
=
new
NextURL
(
'https://foo.com/example'
)
const
url
=
new
NextURL
(
'https://foo.com/example'
)
url
.
hostname
=
''
url
.
hostname
=
''
expect
(
url
.
toString
()).
toEqual
(
'
https://foo.com
/example'
)
expect
(
url
.
toString
()).
toEqual
(
'/example'
)
})
})
it
(
'allows to change the whole href'
,
()
=>
{
it
(
'allows to change the whole href
of an absolute url
'
,
()
=>
{
const
url
=
new
NextURL
(
'https://localhost.com/foo'
)
const
url
=
new
NextURL
(
'https://localhost.com/foo'
)
expect
(
url
.
hostname
).
toEqual
(
'localhost.com'
)
expect
(
url
.
hostname
).
toEqual
(
'localhost.com'
)
expect
(
url
.
protocol
).
toEqual
(
'https:'
)
expect
(
url
.
protocol
).
toEqual
(
'https:'
)
expect
(
url
.
host
).
toEqual
(
'localhost.com'
)
expect
(
url
.
host
).
toEqual
(
'localhost.com'
)
url
.
href
=
'
http://foo.com/bar
'
url
.
href
=
'
/foo
'
expect
(
url
.
hostname
).
toEqual
(
'
foo.com
'
)
expect
(
url
.
hostname
).
toEqual
(
''
)
expect
(
url
.
protocol
).
toEqual
(
'
http:
'
)
expect
(
url
.
protocol
).
toEqual
(
''
)
expect
(
url
.
host
).
toEqual
(
'
foo.com
'
)
expect
(
url
.
host
).
toEqual
(
''
)
})
})
it
(
'allows to update search params'
,
()
=>
{
it
(
'allows to update search params'
,
()
=>
{
const
url
=
new
NextURL
(
'/example'
,
'http://localhost.com'
)
const
url
=
new
NextURL
(
'/example'
)
url
.
searchParams
.
set
(
'foo'
,
'bar'
)
url
.
searchParams
.
set
(
'foo'
,
'bar'
)
expect
(
url
.
search
).
toEqual
(
'?foo=bar'
)
expect
(
url
.
search
).
toEqual
(
'?foo=bar'
)
expect
(
url
.
toString
()).
toEqual
(
'
http://localhost.com
/example?foo=bar'
)
expect
(
url
.
toString
()).
toEqual
(
'/example?foo=bar'
)
})
})
it
(
'parses and formats the basePath'
,
()
=>
{
it
(
'parses and formats the basePath'
,
()
=>
{
const
url
=
new
NextURL
(
'/root/example'
,
{
const
url
=
new
NextURL
(
'/root/example'
,
{
base
:
'http://127.0.0.1'
,
basePath
:
'/root'
,
basePath
:
'/root'
,
})
})
expect
(
url
.
basePath
).
toEqual
(
'/root'
)
expect
(
url
.
basePath
).
toEqual
(
'/root'
)
expect
(
url
.
pathname
).
toEqual
(
'/example'
)
expect
(
url
.
pathname
).
toEqual
(
'/example'
)
expect
(
url
.
toString
()).
toEqual
(
'
http://localhost
/root/example'
)
expect
(
url
.
toString
()).
toEqual
(
'/root/example'
)
const
url2
=
new
NextURL
(
'https://foo.com/root/bar'
,
{
const
url2
=
new
NextURL
(
'https://foo.com/root/bar'
,
{
basePath
:
'/root'
,
basePath
:
'/root'
,
...
@@ -84,47 +89,14 @@ it('parses and formats the basePath', () => {
...
@@ -84,47 +89,14 @@ it('parses and formats the basePath', () => {
expect
(
url3
.
basePath
).
toEqual
(
''
)
expect
(
url3
.
basePath
).
toEqual
(
''
)
url3
.
href
=
'http://localhost.com/root/example'
url3
.
href
=
'/root/example'
expect
(
url3
.
basePath
).
toEqual
(
'/root'
)
expect
(
url
.
basePath
).
toEqual
(
'/root'
)
expect
(
url3
.
pathname
).
toEqual
(
'/example'
)
expect
(
url
.
pathname
).
toEqual
(
'/example'
)
expect
(
url3
.
toString
()).
toEqual
(
'http://localhost.com/root/example'
)
expect
(
url
.
toString
()).
toEqual
(
'/root/example'
)
})
it
(
'allows to get empty locale when there is no locale'
,
()
=>
{
const
url
=
new
NextURL
(
'https://localhost:3000/foo'
)
expect
(
url
.
locale
).
toEqual
(
''
)
})
it
(
'doesnt allow to set an unexisting locale'
,
()
=>
{
const
url
=
new
NextURL
(
'https://localhost:3000/foo'
)
let
error
:
Error
|
null
=
null
try
{
url
.
locale
=
'foo'
}
catch
(
err
)
{
error
=
err
}
expect
(
error
).
toBeInstanceOf
(
TypeError
)
expect
(
error
.
message
).
toEqual
(
'The NextURL configuration includes no locale "foo"'
)
})
it
(
'always get a default locale'
,
()
=>
{
const
url
=
new
NextURL
(
'/bar'
,
{
base
:
'http://127.0.0.1'
,
i18n
:
{
defaultLocale
:
'en'
,
locales
:
[
'en'
,
'es'
,
'fr'
],
},
})
expect
(
url
.
locale
).
toEqual
(
'en'
)
})
})
it
(
'parses and formats the default locale'
,
()
=>
{
it
(
'parses and formats the default locale'
,
()
=>
{
const
url
=
new
NextURL
(
'/es/bar'
,
{
const
url
=
new
NextURL
(
'/es/bar'
,
{
base
:
'http://127.0.0.1'
,
basePath
:
'/root'
,
basePath
:
'/root'
,
i18n
:
{
i18n
:
{
defaultLocale
:
'en'
,
defaultLocale
:
'en'
,
...
@@ -133,19 +105,19 @@ it('parses and formats the default locale', () => {
...
@@ -133,19 +105,19 @@ it('parses and formats the default locale', () => {
})
})
expect
(
url
.
locale
).
toEqual
(
'es'
)
expect
(
url
.
locale
).
toEqual
(
'es'
)
expect
(
url
.
toString
()).
toEqual
(
'
http://localhost
/es/bar'
)
expect
(
url
.
toString
()).
toEqual
(
'/es/bar'
)
url
.
basePath
=
'/root'
url
.
basePath
=
'/root'
expect
(
url
.
locale
).
toEqual
(
'es'
)
expect
(
url
.
locale
).
toEqual
(
'es'
)
expect
(
url
.
toString
()).
toEqual
(
'
http://localhost
/root/es/bar'
)
expect
(
url
.
toString
()).
toEqual
(
'/root/es/bar'
)
url
.
locale
=
'en'
url
.
locale
=
'en'
expect
(
url
.
locale
).
toEqual
(
'en'
)
expect
(
url
.
locale
).
toEqual
(
'en'
)
expect
(
url
.
toString
()).
toEqual
(
'
http://localhost
/root/bar'
)
expect
(
url
.
toString
()).
toEqual
(
'/root/bar'
)
url
.
locale
=
'fr'
url
.
locale
=
'fr'
expect
(
url
.
locale
).
toEqual
(
'fr'
)
expect
(
url
.
locale
).
toEqual
(
'fr'
)
expect
(
url
.
toString
()).
toEqual
(
'
http://localhost
/root/fr/bar'
)
expect
(
url
.
toString
()).
toEqual
(
'/root/fr/bar'
)
})
})
it
(
'consider 127.0.0.1 and variations as localhost'
,
()
=>
{
it
(
'consider 127.0.0.1 and variations as localhost'
,
()
=>
{
...
@@ -159,13 +131,3 @@ it('consider 127.0.0.1 and variations as localhost', () => {
...
@@ -159,13 +131,3 @@ it('consider 127.0.0.1 and variations as localhost', () => {
expect
(
new
NextURL
(
'https://127.0.1.0:3000/hello'
)).
toStrictEqual
(
httpsUrl
)
expect
(
new
NextURL
(
'https://127.0.1.0:3000/hello'
)).
toStrictEqual
(
httpsUrl
)
expect
(
new
NextURL
(
'https://::1:3000/hello'
)).
toStrictEqual
(
httpsUrl
)
expect
(
new
NextURL
(
'https://::1:3000/hello'
)).
toStrictEqual
(
httpsUrl
)
})
})
it
(
'allows to change the port'
,
()
=>
{
const
url
=
new
NextURL
(
'https://localhost:3000/foo'
)
url
.
port
=
'3001'
expect
(
url
.
href
).
toEqual
(
'https://localhost:3001/foo'
)
url
.
port
=
'80'
expect
(
url
.
href
).
toEqual
(
'https://localhost:80/foo'
)
url
.
port
=
''
expect
(
url
.
href
).
toEqual
(
'https://localhost/foo'
)
})
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