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
1b8ab4e3
Unverified
Commit
1b8ab4e3
authored
Nov 08, 2022
by
JJ Kasper
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Apply middleware patches
parent
58dcf304
Hide whitespace changes
Inline
Side-by-side
Showing
19 changed files
with
494 additions
and
59 deletions
+494
-59
webpack-config.ts
packages/next/build/webpack-config.ts
+8
-0
middleware-plugin.ts
packages/next/build/webpack/plugins/middleware-plugin.ts
+18
-4
load-custom-routes.ts
packages/next/lib/load-custom-routes.ts
+41
-39
base-server.ts
packages/next/server/base-server.ts
+27
-0
config-schema.ts
packages/next/server/config-schema.ts
+9
-0
config-shared.ts
packages/next/server/config-shared.ts
+3
-0
next-server.ts
packages/next/server/next-server.ts
+24
-3
request-meta.ts
packages/next/server/request-meta.ts
+2
-0
router.ts
packages/next/server/router.ts
+18
-2
adapter.ts
packages/next/server/web/adapter.ts
+18
-10
next-url.ts
packages/next/server/web/next-url.ts
+1
-1
middleware.js
test/e2e/skip-trailing-slash-redirect/app/middleware.js
+40
-0
next.config.js
test/e2e/skip-trailing-slash-redirect/app/next.config.js
+28
-0
another.js
test/e2e/skip-trailing-slash-redirect/app/pages/another.js
+13
-0
test-cookie-edge.js
...trailing-slash-redirect/app/pages/api/test-cookie-edge.js
+12
-0
test-cookie.js
...skip-trailing-slash-redirect/app/pages/api/test-cookie.js
+5
-0
[slug].js
...e2e/skip-trailing-slash-redirect/app/pages/blog/[slug].js
+13
-0
index.js
test/e2e/skip-trailing-slash-redirect/app/pages/index.js
+17
-0
index.test.ts
test/e2e/skip-trailing-slash-redirect/index.test.ts
+197
-0
No files found.
packages/next/build/webpack-config.ts
View file @
1b8ab4e3
...
...
@@ -155,6 +155,12 @@ export function getDefineEnv({
'process.env.__NEXT_OPTIMISTIC_CLIENT_CACHE'
:
JSON
.
stringify
(
config
.
experimental
.
optimisticClientCache
),
'process.env.__NEXT_NO_MIDDLEWARE_URL_NORMALIZE'
:
JSON
.
stringify
(
config
.
experimental
.
skipMiddlewareUrlNormalize
),
'process.env.__NEXT_ALLOW_MIDDLEWARE_RESPONSE_BODY'
:
JSON
.
stringify
(
config
.
experimental
.
allowMiddlewareResponseBody
),
'process.env.__NEXT_CROSS_ORIGIN'
:
JSON
.
stringify
(
config
.
crossOrigin
),
'process.browser'
:
JSON
.
stringify
(
isClient
),
'process.env.__NEXT_TEST_MODE'
:
JSON
.
stringify
(
...
...
@@ -1779,6 +1785,8 @@ export default async function getBaseWebpackConfig(
new
MiddlewarePlugin
({
dev
,
sriEnabled
:
!
dev
&&
!!
config
.
experimental
.
sri
?.
algorithm
,
allowMiddlewareResponseBody
:
!!
config
.
experimental
.
allowMiddlewareResponseBody
,
}),
isClient
&&
new
BuildManifestPlugin
({
...
...
packages/next/build/webpack/plugins/middleware-plugin.ts
View file @
1b8ab4e3
...
...
@@ -335,12 +335,14 @@ function getCodeAnalyzer(params: {
dev
:
boolean
compiler
:
webpack
.
Compiler
compilation
:
webpack
.
Compilation
allowMiddlewareResponseBody
:
boolean
})
{
return
(
parser
:
webpack
.
javascript
.
JavascriptParser
)
=>
{
const
{
dev
,
compiler
:
{
webpack
:
wp
},
compilation
,
allowMiddlewareResponseBody
,
}
=
params
const
{
hooks
}
=
parser
...
...
@@ -557,8 +559,10 @@ Learn More: https://nextjs.org/docs/messages/node-module-in-edge-runtime`,
.
for
(
`
${
prefix
}
WebAssembly.instantiate`
)
.
tap
(
NAME
,
handleWrapWasmInstantiateExpression
)
}
hooks
.
new
.
for
(
'Response'
).
tap
(
NAME
,
handleNewResponseExpression
)
hooks
.
new
.
for
(
'NextResponse'
).
tap
(
NAME
,
handleNewResponseExpression
)
if
(
!
allowMiddlewareResponseBody
)
{
hooks
.
new
.
for
(
'Response'
).
tap
(
NAME
,
handleNewResponseExpression
)
hooks
.
new
.
for
(
'NextResponse'
).
tap
(
NAME
,
handleNewResponseExpression
)
}
hooks
.
callMemberChain
.
for
(
'process'
).
tap
(
NAME
,
handleCallMemberChain
)
hooks
.
expressionMemberChain
.
for
(
'process'
).
tap
(
NAME
,
handleCallMemberChain
)
hooks
.
importCall
.
tap
(
NAME
,
handleImport
)
...
...
@@ -797,10 +801,19 @@ function getExtractMetadata(params: {
export
default
class
MiddlewarePlugin
{
private
readonly
dev
:
boolean
private
readonly
sriEnabled
:
boolean
constructor
({
dev
,
sriEnabled
}:
{
dev
:
boolean
;
sriEnabled
:
boolean
})
{
private
readonly
allowMiddlewareResponseBody
:
boolean
constructor
({
dev
,
sriEnabled
,
allowMiddlewareResponseBody
,
}:
{
dev
:
boolean
sriEnabled
:
boolean
allowMiddlewareResponseBody
:
boolean
})
{
this
.
dev
=
dev
this
.
sriEnabled
=
sriEnabled
this
.
allowMiddlewareResponseBody
=
allowMiddlewareResponseBody
}
public
apply
(
compiler
:
webpack
.
Compiler
)
{
...
...
@@ -813,6 +826,7 @@ export default class MiddlewarePlugin {
dev
:
this
.
dev
,
compiler
,
compilation
,
allowMiddlewareResponseBody
:
this
.
allowMiddlewareResponseBody
,
})
hooks
.
parser
.
for
(
'javascript/auto'
).
tap
(
NAME
,
codeAnalyzer
)
hooks
.
parser
.
for
(
'javascript/dynamic'
).
tap
(
NAME
,
codeAnalyzer
)
...
...
packages/next/lib/load-custom-routes.ts
View file @
1b8ab4e3
...
...
@@ -624,50 +624,52 @@ export default async function loadCustomRoutes(
)
}
if
(
config
.
trailingSlash
)
{
redirects
.
unshift
(
{
source
:
'/:file((?!
\\
.well-known(?:/.*)?)(?:[^/]+/)*[^/]+
\\
.
\\
w+)/'
,
destination
:
'/:file'
,
permanent
:
true
,
locale
:
config
.
i18n
?
false
:
undefined
,
internal
:
true
,
}
as
Redirect
,
{
source
:
'/:notfile((?!
\\
.well-known(?:/.*)?)(?:[^/]+/)*[^/
\\
.]+)'
,
destination
:
'/:notfile/'
,
permanent
:
true
,
locale
:
config
.
i18n
?
false
:
undefined
,
internal
:
true
,
}
as
Redirect
)
if
(
config
.
basePath
)
{
redirects
.
unshift
({
source
:
config
.
basePath
,
destination
:
config
.
basePath
+
'/'
,
permanent
:
true
,
basePath
:
false
,
locale
:
config
.
i18n
?
false
:
undefined
,
internal
:
true
,
}
as
Redirect
)
}
}
else
{
redirects
.
unshift
({
source
:
'/:path+/'
,
destination
:
'/:path+'
,
permanent
:
true
,
locale
:
config
.
i18n
?
false
:
undefined
,
internal
:
true
,
}
as
Redirect
)
if
(
config
.
basePath
)
{
if
(
!
config
.
experimental
?.
skipTrailingSlashRedirect
)
{
if
(
config
.
trailingSlash
)
{
redirects
.
unshift
(
{
source
:
'/:file((?!
\\
.well-known(?:/.*)?)(?:[^/]+/)*[^/]+
\\
.
\\
w+)/'
,
destination
:
'/:file'
,
permanent
:
true
,
locale
:
config
.
i18n
?
false
:
undefined
,
internal
:
true
,
}
as
Redirect
,
{
source
:
'/:notfile((?!
\\
.well-known(?:/.*)?)(?:[^/]+/)*[^/
\\
.]+)'
,
destination
:
'/:notfile/'
,
permanent
:
true
,
locale
:
config
.
i18n
?
false
:
undefined
,
internal
:
true
,
}
as
Redirect
)
if
(
config
.
basePath
)
{
redirects
.
unshift
({
source
:
config
.
basePath
,
destination
:
config
.
basePath
+
'/'
,
permanent
:
true
,
basePath
:
false
,
locale
:
config
.
i18n
?
false
:
undefined
,
internal
:
true
,
}
as
Redirect
)
}
}
else
{
redirects
.
unshift
({
source
:
config
.
basePath
+
'
/'
,
destination
:
config
.
basePath
,
source
:
'/:path+
/'
,
destination
:
'/:path+'
,
permanent
:
true
,
basePath
:
false
,
locale
:
config
.
i18n
?
false
:
undefined
,
internal
:
true
,
}
as
Redirect
)
if
(
config
.
basePath
)
{
redirects
.
unshift
({
source
:
config
.
basePath
+
'/'
,
destination
:
config
.
basePath
,
permanent
:
true
,
basePath
:
false
,
locale
:
config
.
i18n
?
false
:
undefined
,
internal
:
true
,
}
as
Redirect
)
}
}
}
...
...
packages/next/server/base-server.ts
View file @
1b8ab4e3
...
...
@@ -434,6 +434,33 @@ export default abstract class Server<ServerOptions extends Options = Options> {
parsedUrl
?:
NextUrlWithParsedQuery
):
Promise
<
void
>
{
try
{
// ensure cookies set in middleware are merged and
// not overridden by API routes/getServerSideProps
const
_res
=
(
res
as
any
).
originalResponse
||
res
const
origSetHeader
=
_res
.
setHeader
.
bind
(
_res
)
_res
.
setHeader
=
(
name
:
string
,
val
:
string
|
string
[])
=>
{
if
(
name
.
toLowerCase
()
===
'set-cookie'
)
{
const
middlewareValue
=
getRequestMeta
(
req
,
'_nextMiddlewareCookie'
)
if
(
!
middlewareValue
||
!
Array
.
isArray
(
val
)
||
!
val
.
every
((
item
,
idx
)
=>
item
===
middlewareValue
[
idx
])
)
{
val
=
[
...(
middlewareValue
||
[]),
...(
typeof
val
===
'string'
?
[
val
]
:
Array
.
isArray
(
val
)
?
val
:
[]),
]
}
}
return
origSetHeader
(
name
,
val
)
}
const
urlParts
=
(
req
.
url
||
''
).
split
(
'?'
)
const
urlNoQuery
=
urlParts
[
0
]
...
...
packages/next/server/config-schema.ts
View file @
1b8ab4e3
...
...
@@ -222,6 +222,9 @@ const configSchema = {
adjustFontFallbacks
:
{
type
:
'boolean'
,
},
allowMiddlewareResponseBody
:
{
type
:
'boolean'
,
},
amp
:
{
additionalProperties
:
false
,
properties
:
{
...
...
@@ -348,6 +351,12 @@ const configSchema = {
sharedPool
:
{
type
:
'boolean'
,
},
skipMiddlewareUrlNormalize
:
{
type
:
'boolean'
,
},
skipTrailingSlashRedirect
:
{
type
:
'boolean'
,
},
sri
:
{
properties
:
{
algorithm
:
{
...
...
packages/next/server/config-shared.ts
View file @
1b8ab4e3
...
...
@@ -78,6 +78,9 @@ export interface NextJsWebpackConfig {
}
export
interface
ExperimentalConfig
{
allowMiddlewareResponseBody
?:
boolean
skipMiddlewareUrlNormalize
?:
boolean
skipTrailingSlashRedirect
?:
boolean
optimisticClientCache
?:
boolean
legacyBrowsers
?:
boolean
browsersListForSwc
?:
boolean
...
...
packages/next/server/next-server.ts
View file @
1b8ab4e3
...
...
@@ -83,7 +83,7 @@ import { normalizePagePath } from '../shared/lib/page-path/normalize-page-path'
import
{
loadComponents
}
from
'./load-components'
import
isError
,
{
getProperError
}
from
'../lib/is-error'
import
{
FontManifest
}
from
'./font-utils'
import
{
toNodeHeaders
}
from
'./web/utils'
import
{
splitCookiesString
,
toNodeHeaders
}
from
'./web/utils'
import
{
relativizeURL
}
from
'../shared/lib/router/utils/relativize-url'
import
{
prepareDestination
}
from
'../shared/lib/router/utils/prepare-destination'
import
{
normalizeLocalePath
}
from
'../shared/lib/i18n/normalize-locale-path'
...
...
@@ -1051,9 +1051,17 @@ export default class NextNodeServer extends BaseServer {
name
:
'_next/data catchall'
,
check
:
true
,
fn
:
async
(
req
,
res
,
params
,
_parsedUrl
)
=>
{
const
isNextDataNormalizing
=
getRequestMeta
(
req
,
'_nextDataNormalizing'
)
// Make sure to 404 for /_next/data/ itself and
// we also want to 404 if the buildId isn't correct
if
(
!
params
.
path
||
params
.
path
[
0
]
!==
this
.
buildId
)
{
if
(
isNextDataNormalizing
)
{
return
{
finished
:
false
}
}
await
this
.
render404
(
req
,
res
,
_parsedUrl
)
return
{
finished
:
true
,
...
...
@@ -1797,6 +1805,14 @@ export default class NextNodeServer extends BaseServer {
}
else
{
for
(
let
[
key
,
value
]
of
allHeaders
)
{
result
.
response
.
headers
.
set
(
key
,
value
)
if
(
key
.
toLowerCase
()
===
'set-cookie'
)
{
addRequestMeta
(
params
.
request
,
'_nextMiddlewareCookie'
,
splitCookiesString
(
value
)
)
}
}
}
...
...
@@ -2105,8 +2121,13 @@ export default class NextNodeServer extends BaseServer {
params
.
res
.
statusCode
=
result
.
response
.
status
params
.
res
.
statusMessage
=
result
.
response
.
statusText
result
.
response
.
headers
.
forEach
((
value
,
key
)
=>
{
params
.
res
.
appendHeader
(
key
,
value
)
result
.
response
.
headers
.
forEach
((
value
:
string
,
key
)
=>
{
// the append handling is special cased for `set-cookie`
if
(
key
.
toLowerCase
()
===
'set-cookie'
)
{
params
.
res
.
setHeader
(
key
,
value
)
}
else
{
params
.
res
.
appendHeader
(
key
,
value
)
}
})
if
(
result
.
response
.
body
)
{
...
...
packages/next/server/request-meta.ts
View file @
1b8ab4e3
...
...
@@ -22,6 +22,8 @@ export interface RequestMeta {
_nextHadBasePath
?:
boolean
_nextRewroteUrl
?:
string
_protocol
?:
string
_nextMiddlewareCookie
?:
string
[]
_nextDataNormalizing
?:
boolean
}
export
function
getRequestMeta
(
...
...
packages/next/server/router.ts
View file @
1b8ab4e3
...
...
@@ -7,7 +7,11 @@ import type {
}
from
'../shared/lib/router/utils/route-matcher'
import
type
{
RouteHas
}
from
'../lib/load-custom-routes'
import
{
getNextInternalQuery
,
NextUrlWithParsedQuery
}
from
'./request-meta'
import
{
addRequestMeta
,
getNextInternalQuery
,
NextUrlWithParsedQuery
,
}
from
'./request-meta'
import
{
getPathMatch
}
from
'../shared/lib/router/utils/path-match'
import
{
removeTrailingSlash
}
from
'../shared/lib/router/utils/remove-trailing-slash'
import
{
normalizeLocalePath
}
from
'../shared/lib/i18n/normalize-locale-path'
...
...
@@ -189,7 +193,11 @@ export default class Router {
...(
middlewareCatchAllRoute
?
this
.
fsRoutes
.
filter
((
route
)
=>
route
.
name
===
'_next/data catchall'
)
.
map
((
route
)
=>
({
...
route
,
check
:
false
}))
.
map
((
route
)
=>
({
...
route
,
name
:
'_next/data normalizing'
,
check
:
false
,
}))
:
[]),
...
this
.
headers
,
...
this
.
redirects
,
...
...
@@ -433,6 +441,11 @@ export default class Router {
}
if
(
params
)
{
const
isNextDataNormalizing
=
route
.
name
===
'_next/data normalizing'
if
(
isNextDataNormalizing
)
{
addRequestMeta
(
req
,
'_nextDataNormalizing'
,
true
)
}
parsedUrlUpdated
.
pathname
=
matchPathname
const
result
=
await
route
.
fn
(
req
,
...
...
@@ -441,6 +454,9 @@ export default class Router {
parsedUrlUpdated
,
upgradeHead
)
if
(
isNextDataNormalizing
)
{
addRequestMeta
(
req
,
'_nextDataNormalizing'
,
false
)
}
if
(
result
.
finished
)
{
return
true
}
...
...
packages/next/server/web/adapter.ts
View file @
1b8ab4e3
...
...
@@ -110,11 +110,13 @@ export async function adapter(params: {
nextConfig
:
params
.
request
.
nextConfig
,
})
if
(
rewriteUrl
.
host
===
request
.
nextUrl
.
host
)
{
rewriteUrl
.
buildId
=
buildId
||
rewriteUrl
.
buildId
rewriteUrl
.
flightSearchParameters
=
flightSearchParameters
||
rewriteUrl
.
flightSearchParameters
response
.
headers
.
set
(
'x-middleware-rewrite'
,
String
(
rewriteUrl
))
if
(
!
process
.
env
.
__NEXT_NO_MIDDLEWARE_URL_NORMALIZE
)
{
if
(
rewriteUrl
.
host
===
request
.
nextUrl
.
host
)
{
rewriteUrl
.
buildId
=
buildId
||
rewriteUrl
.
buildId
rewriteUrl
.
flightSearchParameters
=
flightSearchParameters
||
rewriteUrl
.
flightSearchParameters
response
.
headers
.
set
(
'x-middleware-rewrite'
,
String
(
rewriteUrl
))
}
}
/**
...
...
@@ -149,11 +151,13 @@ export async function adapter(params: {
*/
response
=
new
Response
(
response
.
body
,
response
)
if
(
redirectURL
.
host
===
request
.
nextUrl
.
host
)
{
redirectURL
.
buildId
=
buildId
||
redirectURL
.
buildId
redirectURL
.
flightSearchParameters
=
flightSearchParameters
||
redirectURL
.
flightSearchParameters
response
.
headers
.
set
(
'Location'
,
String
(
redirectURL
))
if
(
!
process
.
env
.
__NEXT_NO_MIDDLEWARE_URL_NORMALIZE
)
{
if
(
redirectURL
.
host
===
request
.
nextUrl
.
host
)
{
redirectURL
.
buildId
=
buildId
||
redirectURL
.
buildId
redirectURL
.
flightSearchParameters
=
flightSearchParameters
||
redirectURL
.
flightSearchParameters
response
.
headers
.
set
(
'Location'
,
String
(
redirectURL
))
}
}
/**
...
...
@@ -179,6 +183,10 @@ export async function adapter(params: {
export
function
blockUnallowedResponse
(
promise
:
Promise
<
FetchEventResult
>
):
Promise
<
FetchEventResult
>
{
if
(
process
.
env
.
__NEXT_ALLOW_MIDDLEWARE_RESPONSE_BODY
)
{
return
promise
}
return
promise
.
then
((
result
)
=>
{
if
(
result
.
response
?.
body
)
{
console
.
error
(
...
...
packages/next/server/web/next-url.ts
View file @
1b8ab4e3
...
...
@@ -100,7 +100,7 @@ export class NextURL {
private
analyzeUrl
()
{
const
pathnameInfo
=
getNextPathnameInfo
(
this
[
Internal
].
url
.
pathname
,
{
nextConfig
:
this
[
Internal
].
options
.
nextConfig
,
parseData
:
true
,
parseData
:
!
process
.
env
.
__NEXT_NO_MIDDLEWARE_URL_NORMALIZE
,
})
this
[
Internal
].
domainLocale
=
detectDomainLocale
(
...
...
test/e2e/skip-trailing-slash-redirect/app/middleware.js
0 → 100644
View file @
1b8ab4e3
import
{
NextResponse
}
from
'next/server'
export
default
function
handler
(
req
)
{
if
(
req
.
nextUrl
.
pathname
.
startsWith
(
'/_next/data/missing-id'
))
{
console
.
log
(
`missing-id rewrite:
${
req
.
nextUrl
.
toString
()}
`
)
return
NextResponse
.
rewrite
(
'https://example.vercel.sh'
)
}
if
(
req
.
nextUrl
.
pathname
===
'/middleware-rewrite-with-slash'
)
{
return
NextResponse
.
rewrite
(
new
URL
(
'/another/'
,
req
.
nextUrl
))
}
if
(
req
.
nextUrl
.
pathname
===
'/middleware-rewrite-without-slash'
)
{
return
NextResponse
.
rewrite
(
new
URL
(
'/another'
,
req
.
nextUrl
))
}
if
(
req
.
nextUrl
.
pathname
===
'/middleware-redirect-external-with'
)
{
return
NextResponse
.
redirect
(
'https://example.vercel.sh/somewhere/'
,
307
)
}
if
(
req
.
nextUrl
.
pathname
===
'/middleware-redirect-external-without'
)
{
return
NextResponse
.
redirect
(
'https://example.vercel.sh/somewhere'
,
307
)
}
if
(
req
.
nextUrl
.
pathname
.
startsWith
(
'/api/test-cookie'
))
{
const
res
=
NextResponse
.
next
()
res
.
cookies
.
set
(
'from-middleware'
,
1
)
return
res
}
if
(
req
.
nextUrl
.
pathname
===
'/middleware-response-body'
)
{
return
new
Response
(
'hello from middleware'
,
{
headers
:
{
'x-from-middleware'
:
'true'
,
},
})
}
return
NextResponse
.
next
()
}
test/e2e/skip-trailing-slash-redirect/app/next.config.js
0 → 100644
View file @
1b8ab4e3
/** @type {import('next').NextConfig} */
const
nextConfig
=
{
experimental
:
{
skipTrailingSlashRedirect
:
true
,
skipMiddlewareUrlNormalize
:
true
,
allowMiddlewareResponseBody
:
true
,
newNextLinkBehavior
:
true
,
},
async
redirects
()
{
return
[
{
source
:
'/redirect-me'
,
destination
:
'/another'
,
permanent
:
false
,
},
]
},
async
rewrites
()
{
return
[
{
source
:
'/rewrite-me'
,
destination
:
'/another'
,
},
]
},
}
module
.
exports
=
nextConfig
test/e2e/skip-trailing-slash-redirect/app/pages/another.js
0 → 100644
View file @
1b8ab4e3
import
Link
from
'next/link'
export
default
function
Page
(
props
)
{
return
(
<>
<
p
id
=
"another"
>
another
page
<
/p
>
<
Link
href
=
"/"
id
=
"to-index"
>
to
index
<
/Link
>
<
br
/>
<
/
>
)
}
test/e2e/skip-trailing-slash-redirect/app/pages/api/test-cookie-edge.js
0 → 100644
View file @
1b8ab4e3
import
{
NextResponse
}
from
'next/server'
export
const
config
=
{
runtime
:
'experimental-edge'
,
}
export
default
function
handler
(
req
)
{
console
.
log
(
'setting cookie in api route'
)
const
res
=
NextResponse
.
json
({
name
:
'API'
})
res
.
cookies
.
set
(
'hello'
,
'From API'
)
return
res
}
test/e2e/skip-trailing-slash-redirect/app/pages/api/test-cookie.js
0 → 100644
View file @
1b8ab4e3
export
default
function
handler
(
req
,
res
)
{
console
.
log
(
'setting cookie in api route'
)
res
.
setHeader
(
'Set-Cookie'
,
'hello=From API'
)
res
.
status
(
200
).
json
({
name
:
'API'
})
}
test/e2e/skip-trailing-slash-redirect/app/pages/blog/[slug].js
0 → 100644
View file @
1b8ab4e3
import
Link
from
'next/link'
export
default
function
Page
(
props
)
{
return
(
<>
<
p
id
=
"blog"
>
blog
page
<
/p
>
<
Link
href
=
"/"
id
=
"to-index"
>
to
index
<
/Link
>
<
br
/>
<
/
>
)
}
test/e2e/skip-trailing-slash-redirect/app/pages/index.js
0 → 100644
View file @
1b8ab4e3
import
Link
from
'next/link'
export
default
function
Page
(
props
)
{
return
(
<>
<
p
id
=
"index"
>
index
page
<
/p
>
<
Link
href
=
"/another"
id
=
"to-another"
>
to
another
<
/Link
>
<
br
/>
<
Link
href
=
"/blog/first"
id
=
"to-blog-first"
>
to
/
blog
/
first
<
/Link
>
<
br
/>
<
/
>
)
}
test/e2e/skip-trailing-slash-redirect/index.test.ts
0 → 100644
View file @
1b8ab4e3
import
{
createNext
,
FileRef
}
from
'e2e-utils'
import
{
NextInstance
}
from
'test/lib/next-modes/base'
import
{
check
,
fetchViaHTTP
}
from
'next-test-utils'
import
{
join
}
from
'path'
import
webdriver
from
'next-webdriver'
describe
(
'skip-trailing-slash-redirect'
,
()
=>
{
let
next
:
NextInstance
beforeAll
(
async
()
=>
{
next
=
await
createNext
({
files
:
new
FileRef
(
join
(
__dirname
,
'app'
)),
dependencies
:
{},
})
})
afterAll
(()
=>
next
.
destroy
())
it
(
'should allow rewriting invalid buildId correctly'
,
async
()
=>
{
const
res
=
await
fetchViaHTTP
(
next
.
url
,
'/_next/data/missing-id/hello.json'
,
undefined
,
{
headers
:
{
'x-nextjs-data'
:
'1'
,
},
}
)
expect
(
res
.
status
).
toBe
(
200
)
expect
(
await
res
.
text
()).
toContain
(
'Example Domain'
)
if
(
!
(
global
as
any
).
isNextDeploy
)
{
await
check
(()
=>
next
.
cliOutput
,
/missing-id rewrite/
)
expect
(
next
.
cliOutput
).
toContain
(
'/_next/data/missing-id/hello.json'
)
}
})
it
(
'should allow response body from middleware with flag'
,
async
()
=>
{
const
res
=
await
fetchViaHTTP
(
next
.
url
,
'/middleware-response-body'
)
expect
(
res
.
status
).
toBe
(
200
)
expect
(
res
.
headers
.
get
(
'x-from-middleware'
)).
toBe
(
'true'
)
expect
(
await
res
.
text
()).
toBe
(
'hello from middleware'
)
})
it
(
'should merge cookies from middleware and API routes correctly'
,
async
()
=>
{
const
res
=
await
fetchViaHTTP
(
next
.
url
,
'/api/test-cookie'
,
undefined
,
{
redirect
:
'manual'
,
})
expect
(
res
.
status
).
toBe
(
200
)
expect
(
res
.
headers
.
get
(
'set-cookie'
)).
toEqual
(
'from-middleware=1; Path=/, hello=From API'
)
})
it
(
'should merge cookies from middleware and edge API routes correctly'
,
async
()
=>
{
const
res
=
await
fetchViaHTTP
(
next
.
url
,
'/api/test-cookie-edge'
,
undefined
,
{
redirect
:
'manual'
,
}
)
expect
(
res
.
status
).
toBe
(
200
)
expect
(
res
.
headers
.
get
(
'set-cookie'
)).
toEqual
(
'from-middleware=1; Path=/, hello=From%20API; Path=/'
)
})
if
((
global
as
any
).
isNextStart
)
{
it
(
'should not have trailing slash redirects in manifest'
,
async
()
=>
{
const
routesManifest
=
JSON
.
parse
(
await
next
.
readFile
(
'.next/routes-manifest.json'
)
)
expect
(
routesManifest
.
redirects
.
some
((
redirect
)
=>
{
return
(
redirect
.
statusCode
===
308
&&
(
redirect
.
destination
===
'/:path+'
||
redirect
.
destination
===
'/:path+/'
)
)
})
).
toBe
(
false
)
})
}
it
(
'should correct skip URL normalizing in middleware'
,
async
()
=>
{
let
res
=
await
fetchViaHTTP
(
next
.
url
,
'/middleware-rewrite-with-slash'
,
undefined
,
{
redirect
:
'manual'
,
headers
:
{
'x-nextjs-data'
:
'1'
}
}
)
expect
(
res
.
headers
.
get
(
'x-nextjs-rewrite'
).
endsWith
(
'/another/'
)).
toBe
(
true
)
res
=
await
fetchViaHTTP
(
next
.
url
,
'/middleware-rewrite-without-slash'
,
undefined
,
{
redirect
:
'manual'
,
headers
:
{
'x-nextjs-data'
:
'1'
}
}
)
expect
(
res
.
headers
.
get
(
'x-nextjs-rewrite'
).
endsWith
(
'/another'
)).
toBe
(
true
)
res
=
await
fetchViaHTTP
(
next
.
url
,
'/middleware-redirect-external-with'
,
undefined
,
{
redirect
:
'manual'
}
)
expect
(
res
.
status
).
toBe
(
307
)
expect
(
res
.
headers
.
get
(
'Location'
)).
toBe
(
'https://example.vercel.sh/somewhere/'
)
res
=
await
fetchViaHTTP
(
next
.
url
,
'/middleware-redirect-external-without'
,
undefined
,
{
redirect
:
'manual'
}
)
expect
(
res
.
status
).
toBe
(
307
)
expect
(
res
.
headers
.
get
(
'Location'
)).
toBe
(
'https://example.vercel.sh/somewhere'
)
})
it
(
'should apply config redirect correctly'
,
async
()
=>
{
const
res
=
await
fetchViaHTTP
(
next
.
url
,
'/redirect-me'
,
undefined
,
{
redirect
:
'manual'
,
})
expect
(
res
.
status
).
toBe
(
307
)
expect
(
new
URL
(
res
.
headers
.
get
(
'location'
),
'http://n'
).
pathname
).
toBe
(
'/another'
)
})
it
(
'should apply config rewrites correctly'
,
async
()
=>
{
const
res
=
await
fetchViaHTTP
(
next
.
url
,
'/rewrite-me'
,
undefined
,
{
redirect
:
'manual'
,
})
expect
(
res
.
status
).
toBe
(
200
)
expect
(
await
res
.
text
()).
toContain
(
'another page'
)
})
it
(
'should not apply trailing slash redirect (with slash)'
,
async
()
=>
{
const
res
=
await
fetchViaHTTP
(
next
.
url
,
'/another/'
,
undefined
,
{
redirect
:
'manual'
,
})
expect
(
res
.
status
).
toBe
(
200
)
expect
(
await
res
.
text
()).
toContain
(
'another page'
)
})
it
(
'should not apply trailing slash redirect (without slash)'
,
async
()
=>
{
const
res
=
await
fetchViaHTTP
(
next
.
url
,
'/another'
,
undefined
,
{
redirect
:
'manual'
,
})
expect
(
res
.
status
).
toBe
(
200
)
expect
(
await
res
.
text
()).
toContain
(
'another page'
)
})
it
(
'should respond to index correctly'
,
async
()
=>
{
const
res
=
await
fetchViaHTTP
(
next
.
url
,
'/'
,
undefined
,
{
redirect
:
'manual'
,
})
expect
(
res
.
status
).
toBe
(
200
)
expect
(
await
res
.
text
()).
toContain
(
'index page'
)
})
it
(
'should respond to dynamic route correctly'
,
async
()
=>
{
const
res
=
await
fetchViaHTTP
(
next
.
url
,
'/blog/first'
,
undefined
,
{
redirect
:
'manual'
,
})
expect
(
res
.
status
).
toBe
(
200
)
expect
(
await
res
.
text
()).
toContain
(
'blog page'
)
})
it
(
'should navigate client side correctly'
,
async
()
=>
{
const
browser
=
await
webdriver
(
next
.
url
,
'/'
)
expect
(
await
browser
.
eval
(
'location.pathname'
)).
toBe
(
'/'
)
await
browser
.
elementByCss
(
'#to-another'
).
click
()
await
browser
.
waitForElementByCss
(
'#another'
)
expect
(
await
browser
.
eval
(
'location.pathname'
)).
toBe
(
'/another'
)
await
browser
.
back
()
await
browser
.
waitForElementByCss
(
'#index'
)
expect
(
await
browser
.
eval
(
'location.pathname'
)).
toBe
(
'/'
)
await
browser
.
elementByCss
(
'#to-blog-first'
).
click
()
await
browser
.
waitForElementByCss
(
'#blog'
)
expect
(
await
browser
.
eval
(
'location.pathname'
)).
toBe
(
'/blog/first'
)
})
})
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