-
Notifications
You must be signed in to change notification settings - Fork 27.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make sure to break rewrites chain when dynamic route matches (#16455)
This makes sure to also check if a dynamic route matched after resolving a rewrite on the client to match behavior on the server. It also adds tests for this behavior to ensure it is working properly. Fixes: #16454
- Loading branch information
Showing
10 changed files
with
154 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module.exports = { | ||
rewrites() { | ||
return [ | ||
{ | ||
source: '/:path*', | ||
destination: '/:path*', | ||
}, | ||
{ | ||
source: '/:path*(/?(?!.html))', | ||
destination: '/category/:path*', | ||
}, | ||
] | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default () => '404 page' |
5 changes: 5 additions & 0 deletions
5
test/integration/rewrites-client-resolving/pages/category/[...slug].js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { useRouter } from 'next/router' | ||
|
||
export default () => ( | ||
<p id="category">category: {useRouter().query.slug?.join('/')}</p> | ||
) |
1 change: 1 addition & 0 deletions
1
test/integration/rewrites-client-resolving/pages/category/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default () => <p id="categories">categories</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import Link from 'next/link' | ||
|
||
export default () => ( | ||
<> | ||
<Link href="/product"> | ||
<a id="products-link">to /product</a> | ||
</Link> | ||
<br /> | ||
<Link href="/product/first"> | ||
<a id="product-link">to /product/first</a> | ||
</Link> | ||
<br /> | ||
<Link href="/category"> | ||
<a id="categories-link">to /category</a> | ||
</Link> | ||
<br /> | ||
<Link href="/category/first"> | ||
<a id="category-link">to /category/first</a> | ||
</Link> | ||
<br /> | ||
<Link href="/category/hello/world"> | ||
<a id="category-link-again">to /category/hello/world</a> | ||
</Link> | ||
<br /> | ||
</> | ||
) |
3 changes: 3 additions & 0 deletions
3
test/integration/rewrites-client-resolving/pages/product/[productId].js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { useRouter } from 'next/router' | ||
|
||
export default () => <p id="product">product: {useRouter().query.productId}</p> |
1 change: 1 addition & 0 deletions
1
test/integration/rewrites-client-resolving/pages/product/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default () => <p id="products">products</p> |
86 changes: 86 additions & 0 deletions
86
test/integration/rewrites-client-resolving/test/index.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* eslint-env jest */ | ||
|
||
import { join } from 'path' | ||
import webdriver from 'next-webdriver' | ||
import { | ||
killApp, | ||
findPort, | ||
nextBuild, | ||
nextStart, | ||
launchApp, | ||
} from 'next-test-utils' | ||
|
||
jest.setTimeout(1000 * 60 * 2) | ||
|
||
const appDir = join(__dirname, '../') | ||
let appPort | ||
let app | ||
|
||
const runTests = () => { | ||
it('should break rewrites chain when dynamic route matches', async () => { | ||
const browser = await webdriver(appPort, '/') | ||
await browser.elementByCss('#product-link').click() | ||
await browser.waitForElementByCss('#product') | ||
|
||
expect(await browser.elementByCss('#product').text()).toBe('product: first') | ||
}) | ||
|
||
it('should break rewrites chain when normal page matches', async () => { | ||
const browser = await webdriver(appPort, '/') | ||
await browser.elementByCss('#products-link').click() | ||
await browser.waitForElementByCss('#products') | ||
|
||
expect(await browser.elementByCss('#products').text()).toBe('products') | ||
}) | ||
|
||
it('should break rewrites chain when dynamic catch-all route matches', async () => { | ||
const browser = await webdriver(appPort, '/') | ||
await browser.elementByCss('#category-link').click() | ||
await browser.waitForElementByCss('#category') | ||
|
||
expect(await browser.elementByCss('#category').text()).toBe( | ||
'category: first' | ||
) | ||
}) | ||
|
||
it('should break rewrites chain when dynamic catch-all route multi-level matches', async () => { | ||
const browser = await webdriver(appPort, '/') | ||
await browser.elementByCss('#category-link-again').click() | ||
await browser.waitForElementByCss('#category') | ||
|
||
expect(await browser.elementByCss('#category').text()).toBe( | ||
'category: hello/world' | ||
) | ||
}) | ||
|
||
it('should break rewrites chain after matching /category', async () => { | ||
const browser = await webdriver(appPort, '/') | ||
await browser.elementByCss('#categories-link').click() | ||
await browser.waitForElementByCss('#categories') | ||
|
||
expect(await browser.elementByCss('#categories').text()).toBe('categories') | ||
}) | ||
} | ||
|
||
describe('Client-side rewrites resolving', () => { | ||
describe('dev mode', () => { | ||
beforeAll(async () => { | ||
appPort = await findPort() | ||
app = await launchApp(appDir, appPort) | ||
}) | ||
afterAll(() => killApp(app)) | ||
|
||
runTests() | ||
}) | ||
|
||
describe('production mode', () => { | ||
beforeAll(async () => { | ||
appPort = await findPort() | ||
await nextBuild(appDir) | ||
app = await nextStart(appDir, appPort) | ||
}) | ||
afterAll(() => killApp(app)) | ||
|
||
runTests() | ||
}) | ||
}) |