Unverified Commit 38fa5ca2 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

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
parent c348784a
...@@ -101,19 +101,16 @@ export default function resolveRewrites( ...@@ -101,19 +101,16 @@ export default function resolveRewrites(
let finished = false let finished = false
for (let i = 0; i < rewrites.beforeFiles.length; i++) { for (let i = 0; i < rewrites.beforeFiles.length; i++) {
const rewrite = rewrites.beforeFiles[i] // we don't end after match in beforeFiles to allow
// continuing through all beforeFiles rewrites
if (handleRewrite(rewrite)) { handleRewrite(rewrites.beforeFiles[i])
finished = true
break
}
} }
matchedPage = pages.includes(fsPathname)
if (!pages.includes(fsPathname)) { if (!matchedPage) {
if (!finished) { if (!finished) {
for (let i = 0; i < rewrites.afterFiles.length; i++) { for (let i = 0; i < rewrites.afterFiles.length; i++) {
const rewrite = rewrites.afterFiles[i] if (handleRewrite(rewrites.afterFiles[i])) {
if (handleRewrite(rewrite)) {
finished = true finished = true
break break
} }
...@@ -129,8 +126,7 @@ export default function resolveRewrites( ...@@ -129,8 +126,7 @@ export default function resolveRewrites(
if (!finished) { if (!finished) {
for (let i = 0; i < rewrites.fallback.length; i++) { for (let i = 0; i < rewrites.fallback.length; i++) {
const rewrite = rewrites.fallback[i] if (handleRewrite(rewrites.fallback[i])) {
if (handleRewrite(rewrite)) {
finished = true finished = true
break break
} }
......
...@@ -902,11 +902,11 @@ export default class Server { ...@@ -902,11 +902,11 @@ export default class Server {
} as Route } as Route
}) })
const buildRewrite = (rewrite: Rewrite) => { const buildRewrite = (rewrite: Rewrite, check = true) => {
const rewriteRoute = getCustomRoute(rewrite, 'rewrite') const rewriteRoute = getCustomRoute(rewrite, 'rewrite')
return { return {
...rewriteRoute, ...rewriteRoute,
check: true, check,
type: rewriteRoute.type, type: rewriteRoute.type,
name: `Rewrite route ${rewriteRoute.source}`, name: `Rewrite route ${rewriteRoute.source}`,
match: rewriteRoute.match, match: rewriteRoute.match,
...@@ -975,11 +975,17 @@ export default class Server { ...@@ -975,11 +975,17 @@ export default class Server {
if (!this.minimalMode) { if (!this.minimalMode) {
if (Array.isArray(this.customRoutes.rewrites)) { if (Array.isArray(this.customRoutes.rewrites)) {
afterFiles = this.customRoutes.rewrites.map(buildRewrite) afterFiles = this.customRoutes.rewrites.map((r) => buildRewrite(r))
} else { } else {
beforeFiles = this.customRoutes.rewrites.beforeFiles.map(buildRewrite) beforeFiles = this.customRoutes.rewrites.beforeFiles.map((r) =>
afterFiles = this.customRoutes.rewrites.afterFiles.map(buildRewrite) buildRewrite(r, false)
fallback = this.customRoutes.rewrites.fallback.map(buildRewrite) )
afterFiles = this.customRoutes.rewrites.afterFiles.map((r) =>
buildRewrite(r)
)
fallback = this.customRoutes.rewrites.fallback.map((r) =>
buildRewrite(r)
)
} }
} }
......
...@@ -190,6 +190,10 @@ module.exports = { ...@@ -190,6 +190,10 @@ module.exports = {
], ],
destination: '/with-params?idk=:idk', destination: '/with-params?idk=:idk',
}, },
{
source: '/blog/about',
destination: '/hello',
},
], ],
beforeFiles: [ beforeFiles: [
{ {
...@@ -202,6 +206,10 @@ module.exports = { ...@@ -202,6 +206,10 @@ module.exports = {
], ],
destination: '/with-params?overridden=1', destination: '/with-params?overridden=1',
}, },
{
source: '/old-blog/:path*',
destination: '/blog/:path*',
},
], ],
} }
}, },
......
...@@ -32,5 +32,13 @@ export default () => ( ...@@ -32,5 +32,13 @@ export default () => (
<a id="to-rewritten-dynamic">to rewritten dynamic</a> <a id="to-rewritten-dynamic">to rewritten dynamic</a>
</Link> </Link>
<br /> <br />
<Link href="/hello?overrideMe=1">
<a id="to-overridden">to /hello?overrideMe=1</a>
</Link>
<br />
<Link href="/old-blog/about">
<a id="to-old-blog">to /old-blog/post-1</a>
</Link>
<br />
</> </>
) )
...@@ -38,6 +38,25 @@ let appPort ...@@ -38,6 +38,25 @@ let appPort
let app let app
const runTests = (isDev = false) => { const runTests = (isDev = false) => {
it('should continue in beforeFiles rewrites', async () => {
const res = await fetchViaHTTP(appPort, '/old-blog/about')
expect(res.status).toBe(200)
const html = await res.text()
const $ = cheerio.load(html)
expect($('#hello').text()).toContain('Hello')
const browser = await webdriver(appPort, '/nav')
await browser.eval('window.beforeNav = 1')
await browser
.elementByCss('#to-old-blog')
.click()
.waitForElementByCss('#hello')
expect(await browser.elementByCss('#hello').text()).toContain('Hello')
})
it('should not hang when proxy rewrite fails', async () => { it('should not hang when proxy rewrite fails', async () => {
const res = await fetchViaHTTP(appPort, '/to-nowhere', undefined, { const res = await fetchViaHTTP(appPort, '/to-nowhere', undefined, {
timeout: 5000, timeout: 5000,
...@@ -781,6 +800,20 @@ const runTests = (isDev = false) => { ...@@ -781,6 +800,20 @@ const runTests = (isDev = false) => {
overrideMe: '1', overrideMe: '1',
overridden: '1', overridden: '1',
}) })
const browser = await webdriver(appPort, '/nav')
await browser.eval('window.beforeNav = 1')
await browser.elementByCss('#to-overridden').click()
await browser.waitForElementByCss('#query')
expect(await browser.eval('window.next.router.pathname')).toBe(
'/with-params'
)
expect(JSON.parse(await browser.elementByCss('#query').text())).toEqual({
overridden: '1',
overrideMe: '1',
})
expect(await browser.eval('window.beforeNav')).toBe(1)
}) })
it('should match has header redirect correctly', async () => { it('should match has header redirect correctly', async () => {
...@@ -1401,6 +1434,13 @@ const runTests = (isDev = false) => { ...@@ -1401,6 +1434,13 @@ const runTests = (isDev = false) => {
regex: normalizeRegEx('^\\/hello$'), regex: normalizeRegEx('^\\/hello$'),
source: '/hello', source: '/hello',
}, },
{
destination: '/blog/:path*',
regex: normalizeRegEx(
'^\\/old-blog(?:\\/((?:[^\\/]+?)(?:\\/(?:[^\\/]+?))*))?$'
),
source: '/old-blog/:path*',
},
], ],
afterFiles: [ afterFiles: [
{ {
...@@ -1630,6 +1670,11 @@ const runTests = (isDev = false) => { ...@@ -1630,6 +1670,11 @@ const runTests = (isDev = false) => {
regex: normalizeRegEx('^\\/has-rewrite-7$'), regex: normalizeRegEx('^\\/has-rewrite-7$'),
source: '/has-rewrite-7', source: '/has-rewrite-7',
}, },
{
destination: '/hello',
regex: normalizeRegEx('^\\/blog\\/about$'),
source: '/blog/about',
},
], ],
fallback: [], fallback: [],
}, },
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment