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
927bd163
Unverified
Commit
927bd163
authored
Sep 28, 2022
by
JJ Kasper
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add experimental skipTrailingSlashRedirect
parent
cb1da96e
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
215 additions
and
39 deletions
+215
-39
load-custom-routes.ts
packages/next/lib/load-custom-routes.ts
+41
-39
config-schema.ts
packages/next/server/config-schema.ts
+3
-0
config-shared.ts
packages/next/server/config-shared.ts
+1
-0
another.js
test/e2e/skip-trailing-slash-redirect/app/pages/another.js
+13
-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
+127
-0
No files found.
packages/next/lib/load-custom-routes.ts
View file @
927bd163
...
@@ -609,50 +609,52 @@ export default async function loadCustomRoutes(
...
@@ -609,50 +609,52 @@ export default async function loadCustomRoutes(
)
)
}
}
if
(
config
.
trailingSlash
)
{
if
(
!
config
.
experimental
?.
skipTrailingSlashRedirect
)
{
redirects
.
unshift
(
if
(
config
.
trailingSlash
)
{
{
redirects
.
unshift
(
source
:
'/:file((?!
\\
.well-known(?:/.*)?)(?:[^/]+/)*[^/]+
\\
.
\\
w+)/'
,
{
destination
:
'/:file'
,
source
:
'/:file((?!
\\
.well-known(?:/.*)?)(?:[^/]+/)*[^/]+
\\
.
\\
w+)/'
,
permanent
:
true
,
destination
:
'/:file'
,
locale
:
config
.
i18n
?
false
:
undefined
,
permanent
:
true
,
internal
:
true
,
locale
:
config
.
i18n
?
false
:
undefined
,
}
as
Redirect
,
internal
:
true
,
{
}
as
Redirect
,
source
:
'/:notfile((?!
\\
.well-known(?:/.*)?)(?:[^/]+/)*[^/
\\
.]+)'
,
{
destination
:
'/:notfile/'
,
source
:
'/:notfile((?!
\\
.well-known(?:/.*)?)(?:[^/]+/)*[^/
\\
.]+)'
,
permanent
:
true
,
destination
:
'/:notfile/'
,
locale
:
config
.
i18n
?
false
:
undefined
,
permanent
:
true
,
internal
:
true
,
locale
:
config
.
i18n
?
false
:
undefined
,
}
as
Redirect
internal
:
true
,
)
}
as
Redirect
if
(
config
.
basePath
)
{
)
redirects
.
unshift
({
if
(
config
.
basePath
)
{
source
:
config
.
basePath
,
redirects
.
unshift
({
destination
:
config
.
basePath
+
'/'
,
source
:
config
.
basePath
,
permanent
:
true
,
destination
:
config
.
basePath
+
'/'
,
basePath
:
false
,
permanent
:
true
,
locale
:
config
.
i18n
?
false
:
undefined
,
basePath
:
false
,
internal
:
true
,
locale
:
config
.
i18n
?
false
:
undefined
,
}
as
Redirect
)
internal
:
true
,
}
}
as
Redirect
)
}
else
{
}
redirects
.
unshift
({
}
else
{
source
:
'/:path+/'
,
destination
:
'/:path+'
,
permanent
:
true
,
locale
:
config
.
i18n
?
false
:
undefined
,
internal
:
true
,
}
as
Redirect
)
if
(
config
.
basePath
)
{
redirects
.
unshift
({
redirects
.
unshift
({
source
:
config
.
basePath
+
'
/'
,
source
:
'/:path+
/'
,
destination
:
config
.
basePath
,
destination
:
'/:path+'
,
permanent
:
true
,
permanent
:
true
,
basePath
:
false
,
locale
:
config
.
i18n
?
false
:
undefined
,
locale
:
config
.
i18n
?
false
:
undefined
,
internal
:
true
,
internal
:
true
,
}
as
Redirect
)
}
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/config-schema.ts
View file @
927bd163
...
@@ -355,6 +355,9 @@ const configSchema = {
...
@@ -355,6 +355,9 @@ const configSchema = {
sharedPool
:
{
sharedPool
:
{
type
:
'boolean'
,
type
:
'boolean'
,
},
},
skipTrailingSlashRedirect
:
{
type
:
'boolean'
,
},
swcFileReading
:
{
swcFileReading
:
{
type
:
'boolean'
,
type
:
'boolean'
,
},
},
...
...
packages/next/server/config-shared.ts
View file @
927bd163
...
@@ -78,6 +78,7 @@ export interface NextJsWebpackConfig {
...
@@ -78,6 +78,7 @@ export interface NextJsWebpackConfig {
}
}
export
interface
ExperimentalConfig
{
export
interface
ExperimentalConfig
{
skipTrailingSlashRedirect
?:
boolean
optimisticClientCache
?:
boolean
optimisticClientCache
?:
boolean
legacyBrowsers
?:
boolean
legacyBrowsers
?:
boolean
browsersListForSwc
?:
boolean
browsersListForSwc
?:
boolean
...
...
test/e2e/skip-trailing-slash-redirect/app/pages/another.js
0 → 100644
View file @
927bd163
import
Link
from
'next/link'
export
default
function
Page
(
props
)
{
return
(
<>
<
p
id
=
"another"
>
another
page
<
/p
>
<
Link
href
=
"/"
>
<
a
id
=
"to-index"
>
to
index
<
/a
>
<
/Link
>
<
br
/>
<
/
>
)
}
test/e2e/skip-trailing-slash-redirect/app/pages/blog/[slug].js
0 → 100644
View file @
927bd163
import
Link
from
'next/link'
export
default
function
Page
(
props
)
{
return
(
<>
<
p
id
=
"blog"
>
blog
page
<
/p
>
<
Link
href
=
"/"
>
<
a
id
=
"to-index"
>
to
index
<
/a
>
<
/Link
>
<
br
/>
<
/
>
)
}
test/e2e/skip-trailing-slash-redirect/app/pages/index.js
0 → 100644
View file @
927bd163
import
Link
from
'next/link'
export
default
function
Page
(
props
)
{
return
(
<>
<
p
id
=
"index"
>
index
page
<
/p
>
<
Link
href
=
"/another"
>
<
a
id
=
"to-another"
>
to
another
<
/a
>
<
/Link
>
<
br
/>
<
Link
href
=
"/blog/first"
>
<
a
id
=
"to-blog-first"
>
to
/
blog
/
first
<
/a
>
<
/Link
>
<
br
/>
<
/
>
)
}
test/e2e/skip-trailing-slash-redirect/index.test.ts
0 → 100644
View file @
927bd163
import
{
createNext
,
FileRef
}
from
'e2e-utils'
import
{
NextInstance
}
from
'test/lib/next-modes/base'
import
{
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
:
{},
nextConfig
:
{
experimental
:
{
skipTrailingSlashRedirect
:
true
,
},
async
redirects
()
{
return
[
{
source
:
'/redirect-me'
,
destination
:
'/another'
,
permanent
:
false
,
},
]
},
async
rewrites
()
{
return
[
{
source
:
'/rewrite-me'
,
destination
:
'/another'
,
},
]
},
},
})
})
afterAll
(()
=>
next
.
destroy
())
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 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