Skip to content

Commit 38fa5ca

Browse files
authored
Update beforeFiles rewrites to continue (#25418)
This updates beforeFiles rewrites to continue instead of matching a public file/page immediately after a match, this allows all beforeFiles routes to be checked before matching the filesystem. ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. ## Documentation / Examples - [ ] Make sure the linting passes
1 parent c348784 commit 38fa5ca

File tree

5 files changed

+80
-17
lines changed

5 files changed

+80
-17
lines changed

packages/next/next-server/lib/router/utils/resolve-rewrites.ts

+7-11
Original file line numberDiff line numberDiff line change
@@ -101,19 +101,16 @@ export default function resolveRewrites(
101101
let finished = false
102102

103103
for (let i = 0; i < rewrites.beforeFiles.length; i++) {
104-
const rewrite = rewrites.beforeFiles[i]
105-
106-
if (handleRewrite(rewrite)) {
107-
finished = true
108-
break
109-
}
104+
// we don't end after match in beforeFiles to allow
105+
// continuing through all beforeFiles rewrites
106+
handleRewrite(rewrites.beforeFiles[i])
110107
}
108+
matchedPage = pages.includes(fsPathname)
111109

112-
if (!pages.includes(fsPathname)) {
110+
if (!matchedPage) {
113111
if (!finished) {
114112
for (let i = 0; i < rewrites.afterFiles.length; i++) {
115-
const rewrite = rewrites.afterFiles[i]
116-
if (handleRewrite(rewrite)) {
113+
if (handleRewrite(rewrites.afterFiles[i])) {
117114
finished = true
118115
break
119116
}
@@ -129,8 +126,7 @@ export default function resolveRewrites(
129126

130127
if (!finished) {
131128
for (let i = 0; i < rewrites.fallback.length; i++) {
132-
const rewrite = rewrites.fallback[i]
133-
if (handleRewrite(rewrite)) {
129+
if (handleRewrite(rewrites.fallback[i])) {
134130
finished = true
135131
break
136132
}

packages/next/next-server/server/next-server.ts

+12-6
Original file line numberDiff line numberDiff line change
@@ -902,11 +902,11 @@ export default class Server {
902902
} as Route
903903
})
904904

905-
const buildRewrite = (rewrite: Rewrite) => {
905+
const buildRewrite = (rewrite: Rewrite, check = true) => {
906906
const rewriteRoute = getCustomRoute(rewrite, 'rewrite')
907907
return {
908908
...rewriteRoute,
909-
check: true,
909+
check,
910910
type: rewriteRoute.type,
911911
name: `Rewrite route ${rewriteRoute.source}`,
912912
match: rewriteRoute.match,
@@ -975,11 +975,17 @@ export default class Server {
975975

976976
if (!this.minimalMode) {
977977
if (Array.isArray(this.customRoutes.rewrites)) {
978-
afterFiles = this.customRoutes.rewrites.map(buildRewrite)
978+
afterFiles = this.customRoutes.rewrites.map((r) => buildRewrite(r))
979979
} else {
980-
beforeFiles = this.customRoutes.rewrites.beforeFiles.map(buildRewrite)
981-
afterFiles = this.customRoutes.rewrites.afterFiles.map(buildRewrite)
982-
fallback = this.customRoutes.rewrites.fallback.map(buildRewrite)
980+
beforeFiles = this.customRoutes.rewrites.beforeFiles.map((r) =>
981+
buildRewrite(r, false)
982+
)
983+
afterFiles = this.customRoutes.rewrites.afterFiles.map((r) =>
984+
buildRewrite(r)
985+
)
986+
fallback = this.customRoutes.rewrites.fallback.map((r) =>
987+
buildRewrite(r)
988+
)
983989
}
984990
}
985991

test/integration/custom-routes/next.config.js

+8
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,10 @@ module.exports = {
190190
],
191191
destination: '/with-params?idk=:idk',
192192
},
193+
{
194+
source: '/blog/about',
195+
destination: '/hello',
196+
},
193197
],
194198
beforeFiles: [
195199
{
@@ -202,6 +206,10 @@ module.exports = {
202206
],
203207
destination: '/with-params?overridden=1',
204208
},
209+
{
210+
source: '/old-blog/:path*',
211+
destination: '/blog/:path*',
212+
},
205213
],
206214
}
207215
},

test/integration/custom-routes/pages/nav.js

+8
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,13 @@ export default () => (
3232
<a id="to-rewritten-dynamic">to rewritten dynamic</a>
3333
</Link>
3434
<br />
35+
<Link href="/hello?overrideMe=1">
36+
<a id="to-overridden">to /hello?overrideMe=1</a>
37+
</Link>
38+
<br />
39+
<Link href="/old-blog/about">
40+
<a id="to-old-blog">to /old-blog/post-1</a>
41+
</Link>
42+
<br />
3543
</>
3644
)

test/integration/custom-routes/test/index.test.js

+45
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,25 @@ let appPort
3838
let app
3939

4040
const runTests = (isDev = false) => {
41+
it('should continue in beforeFiles rewrites', async () => {
42+
const res = await fetchViaHTTP(appPort, '/old-blog/about')
43+
expect(res.status).toBe(200)
44+
45+
const html = await res.text()
46+
const $ = cheerio.load(html)
47+
48+
expect($('#hello').text()).toContain('Hello')
49+
50+
const browser = await webdriver(appPort, '/nav')
51+
52+
await browser.eval('window.beforeNav = 1')
53+
await browser
54+
.elementByCss('#to-old-blog')
55+
.click()
56+
.waitForElementByCss('#hello')
57+
expect(await browser.elementByCss('#hello').text()).toContain('Hello')
58+
})
59+
4160
it('should not hang when proxy rewrite fails', async () => {
4261
const res = await fetchViaHTTP(appPort, '/to-nowhere', undefined, {
4362
timeout: 5000,
@@ -781,6 +800,20 @@ const runTests = (isDev = false) => {
781800
overrideMe: '1',
782801
overridden: '1',
783802
})
803+
804+
const browser = await webdriver(appPort, '/nav')
805+
await browser.eval('window.beforeNav = 1')
806+
await browser.elementByCss('#to-overridden').click()
807+
await browser.waitForElementByCss('#query')
808+
809+
expect(await browser.eval('window.next.router.pathname')).toBe(
810+
'/with-params'
811+
)
812+
expect(JSON.parse(await browser.elementByCss('#query').text())).toEqual({
813+
overridden: '1',
814+
overrideMe: '1',
815+
})
816+
expect(await browser.eval('window.beforeNav')).toBe(1)
784817
})
785818

786819
it('should match has header redirect correctly', async () => {
@@ -1401,6 +1434,13 @@ const runTests = (isDev = false) => {
14011434
regex: normalizeRegEx('^\\/hello$'),
14021435
source: '/hello',
14031436
},
1437+
{
1438+
destination: '/blog/:path*',
1439+
regex: normalizeRegEx(
1440+
'^\\/old-blog(?:\\/((?:[^\\/]+?)(?:\\/(?:[^\\/]+?))*))?$'
1441+
),
1442+
source: '/old-blog/:path*',
1443+
},
14041444
],
14051445
afterFiles: [
14061446
{
@@ -1630,6 +1670,11 @@ const runTests = (isDev = false) => {
16301670
regex: normalizeRegEx('^\\/has-rewrite-7$'),
16311671
source: '/has-rewrite-7',
16321672
},
1673+
{
1674+
destination: '/hello',
1675+
regex: normalizeRegEx('^\\/blog\\/about$'),
1676+
source: '/blog/about',
1677+
},
16331678
],
16341679
fallback: [],
16351680
},

0 commit comments

Comments
 (0)