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
29d267a1
Unverified
Commit
29d267a1
authored
Nov 20, 2019
by
JJ Kasper
Committed by
GitHub
Nov 20, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add handling for rewriting to /_next with catchall (#9466)
* Add handling for rewriting to /_next with catchall * bump
parent
546bb68a
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
124 additions
and
23 deletions
+124
-23
next-server.ts
packages/next/next-server/server/next-server.ts
+23
-23
next.config.js
test/integration/custom-routes-catchall/next.config.js
+12
-0
hello.js
test/integration/custom-routes-catchall/pages/hello.js
+15
-0
another.txt
test/integration/custom-routes-catchall/public/another.txt
+1
-0
data.json
...ntegration/custom-routes-catchall/public/static/data.json
+1
-0
index.test.js
test/integration/custom-routes-catchall/test/index.test.js
+72
-0
No files found.
packages/next/next-server/server/next-server.ts
View file @
29d267a1
...
@@ -290,7 +290,7 @@ export default class Server {
...
@@ -290,7 +290,7 @@ export default class Server {
]
]
:
[]
:
[]
const
r
outes
:
Route
[]
=
[
const
topR
outes
:
Route
[]
=
[
{
{
match
:
route
(
'/_next/static/:path*'
),
match
:
route
(
'/_next/static/:path*'
),
type
:
'route'
,
type
:
'route'
,
...
@@ -381,7 +381,10 @@ export default class Server {
...
@@ -381,7 +381,10 @@ export default class Server {
}
}
},
},
},
},
...
publicRoutes
,
...
staticFilesRoute
,
]
]
const
routes
:
Route
[]
=
[...
topRoutes
]
if
(
this
.
customRoutes
)
{
if
(
this
.
customRoutes
)
{
const
{
redirects
,
rewrites
}
=
this
.
customRoutes
const
{
redirects
,
rewrites
}
=
this
.
customRoutes
...
@@ -428,30 +431,27 @@ export default class Server {
...
@@ -428,30 +431,27 @@ export default class Server {
}
as
Route
}
as
Route
})
})
)
)
// make sure previous routes can still be rewritten to by
// custom routes e.g. /docs/_next/static -> /_next/static
routes
.
push
(...
topRoutes
)
}
}
routes
.
push
(
routes
.
push
({
...([
match
:
route
(
'/api/:path*'
),
...
publicRoutes
,
type
:
'route'
,
...
staticFilesRoute
,
name
:
'API Route'
,
{
fn
:
async
(
req
,
res
,
params
,
parsedUrl
)
=>
{
match
:
route
(
'/api/:path*'
),
const
{
pathname
}
=
parsedUrl
type
:
'route'
,
await
this
.
handleApiRequest
(
name
:
'API Route'
,
req
as
NextApiRequest
,
fn
:
async
(
req
,
res
,
params
,
parsedUrl
)
=>
{
res
as
NextApiResponse
,
const
{
pathname
}
=
parsedUrl
pathname
!
await
this
.
handleApiRequest
(
)
req
as
NextApiRequest
,
return
{
res
as
NextApiResponse
,
finished
:
true
,
pathname
!
}
)
},
return
{
})
finished
:
true
,
}
},
},
]
as
Route
[])
)
if
(
this
.
nextConfig
.
useFileSystemPublicRoutes
)
{
if
(
this
.
nextConfig
.
useFileSystemPublicRoutes
)
{
this
.
dynamicRoutes
=
this
.
getDynamicRoutes
()
this
.
dynamicRoutes
=
this
.
getDynamicRoutes
()
...
...
test/integration/custom-routes-catchall/next.config.js
0 → 100644
View file @
29d267a1
module
.
exports
=
{
experimental
:
{
rewrites
()
{
return
[
{
source
:
'/docs/:path*'
,
destination
:
'/:path*'
,
},
]
},
},
}
test/integration/custom-routes-catchall/pages/hello.js
0 → 100644
View file @
29d267a1
import
{
useRouter
}
from
'next/router'
console
.
log
(
'hello from hello.js'
)
const
Page
=
()
=>
{
const
{
query
}
=
useRouter
()
return
(
<>
<
h3
>
hello
world
<
/h3
>
<
span
>
{
JSON
.
stringify
(
query
)}
<
/span
>
<
/
>
)
}
export
default
Page
test/integration/custom-routes-catchall/public/another.txt
0 → 100644
View file @
29d267a1
some text
\ No newline at end of file
test/integration/custom-routes-catchall/public/static/data.json
0 → 100644
View file @
29d267a1
{
"field"
:
"some data..."
}
test/integration/custom-routes-catchall/test/index.test.js
0 → 100644
View file @
29d267a1
/* eslint-env jest */
/* global jasmine */
import
fs
from
'fs-extra'
import
{
join
}
from
'path'
import
{
launchApp
,
killApp
,
findPort
,
nextBuild
,
nextStart
,
renderViaHTTP
,
}
from
'next-test-utils'
jasmine
.
DEFAULT_TIMEOUT_INTERVAL
=
1000
*
60
*
2
let
appDir
=
join
(
__dirname
,
'..'
)
let
buildId
let
appPort
let
app
const
runTests
=
()
=>
{
it
(
'should rewrite to /_next/static correctly'
,
async
()
=>
{
// ensure the bundle is built
await
renderViaHTTP
(
appPort
,
'/hello'
)
const
bundlePath
=
await
join
(
'/docs/_next/static/'
,
buildId
,
'pages/hello.js'
)
const
data
=
await
renderViaHTTP
(
appPort
,
bundlePath
)
expect
(
data
).
toContain
(
'hello from hello.js'
)
})
it
(
'should rewrite and render page correctly'
,
async
()
=>
{
const
html
=
await
renderViaHTTP
(
appPort
,
'/docs/hello'
)
expect
(
html
).
toMatch
(
/hello world/
)
})
it
(
'should rewrite to public/static correctly'
,
async
()
=>
{
const
data
=
await
renderViaHTTP
(
appPort
,
'/docs/static/data.json'
)
expect
(
data
).
toContain
(
'some data...'
)
})
it
(
'should rewrite to public file correctly'
,
async
()
=>
{
const
data
=
await
renderViaHTTP
(
appPort
,
'/docs/another.txt'
)
expect
(
data
).
toContain
(
'some text'
)
})
}
describe
(
'Custom routes'
,
()
=>
{
describe
(
'dev mode'
,
()
=>
{
beforeAll
(
async
()
=>
{
appPort
=
await
findPort
()
app
=
await
launchApp
(
appDir
,
appPort
)
buildId
=
'development'
})
afterAll
(()
=>
killApp
(
app
))
runTests
(
true
)
})
describe
(
'production mode'
,
()
=>
{
beforeAll
(
async
()
=>
{
await
nextBuild
(
appDir
)
appPort
=
await
findPort
()
app
=
await
nextStart
(
appDir
,
appPort
)
buildId
=
await
fs
.
readFile
(
join
(
appDir
,
'.next/BUILD_ID'
),
'utf8'
)
})
afterAll
(()
=>
killApp
(
app
))
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