-
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.
Fix page checking failing with trailingSlash (#16362)
This fixes page checking failing due to the trailing slash being present which causes pages to proxied by a rewrite when they shouldn't be. This also adds additional tests to ensure rewriting to an external resource is working correctly with `trailingSlash: true` Fixes: #15700
- Loading branch information
Showing
8 changed files
with
145 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module.exports = { | ||
trailingSlash: true, | ||
|
||
async rewrites() { | ||
return [ | ||
{ | ||
source: '/:path*/', | ||
destination: '/:path*/', | ||
}, | ||
{ | ||
source: '/:path*/', | ||
destination: 'http://localhost:__EXTERNAL_PORT__/:path*', | ||
}, | ||
] | ||
}, | ||
} |
3 changes: 3 additions & 0 deletions
3
test/integration/trailing-slashes-rewrite/pages/catch-all/[...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,3 @@ | ||
export default function Page() { | ||
return <p>catch-all</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,3 @@ | ||
export default function Index() { | ||
return <p>index page</p> | ||
} |
3 changes: 3 additions & 0 deletions
3
test/integration/trailing-slashes-rewrite/pages/products/[product].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 @@ | ||
export default function Products() { | ||
return <p>a product</p> | ||
} |
3 changes: 3 additions & 0 deletions
3
test/integration/trailing-slashes-rewrite/pages/products/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,3 @@ | ||
export default function Products() { | ||
return <p>some products</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,10 @@ | ||
const http = require('http') | ||
const server = http.createServer((req, res) => { | ||
console.log('got request', req.url) | ||
res.end(req.url) | ||
}) | ||
const port = process.env.PORT || 3000 | ||
|
||
server.listen(port, () => { | ||
console.log('Ready on http://localhost:' + port) | ||
}) |
103 changes: 103 additions & 0 deletions
103
test/integration/trailing-slashes-rewrite/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,103 @@ | ||
/* eslint-env jest */ | ||
import { join } from 'path' | ||
import { | ||
findPort, | ||
killApp, | ||
nextBuild, | ||
nextStart, | ||
File, | ||
initNextServerScript, | ||
renderViaHTTP, | ||
launchApp, | ||
} from 'next-test-utils' | ||
|
||
jest.setTimeout(1000 * 60 * 2) | ||
|
||
let app | ||
let appPort | ||
let proxyPort | ||
let proxyServer | ||
const appDir = join(__dirname, '../') | ||
const nextConfig = new File(join(appDir, 'next.config.js')) | ||
|
||
const runTests = () => { | ||
it('should resolve index page correctly', async () => { | ||
const html = await renderViaHTTP(appPort, '/') | ||
expect(html).toContain('index page') | ||
}) | ||
|
||
it('should resolve products page correctly', async () => { | ||
const html = await renderViaHTTP(appPort, '/products') | ||
expect(html).toContain('some products') | ||
}) | ||
|
||
it('should resolve a dynamic page correctly', async () => { | ||
const html = await renderViaHTTP(appPort, '/products/first') | ||
expect(html).toContain('a product') | ||
}) | ||
|
||
it('should resolve a catch-all page correctly', async () => { | ||
const html = await renderViaHTTP(appPort, '/catch-all/hello') | ||
expect(html).toContain('catch-all') | ||
}) | ||
|
||
it('should proxy non-existent page correctly', async () => { | ||
const html = await renderViaHTTP(appPort, '/non-existent') | ||
expect(html).toBe('/non-existent') | ||
}) | ||
} | ||
|
||
describe('Trailing Slash Rewrite Proxying', () => { | ||
describe('production mode', () => { | ||
beforeAll(async () => { | ||
proxyPort = await findPort() | ||
proxyServer = await initNextServerScript( | ||
join(appDir, 'server.js'), | ||
/ready on/i, | ||
{ | ||
...process.env, | ||
PORT: proxyPort, | ||
} | ||
) | ||
|
||
nextConfig.replace('__EXTERNAL_PORT__', proxyPort) | ||
|
||
await nextBuild(appDir) | ||
appPort = await findPort() | ||
app = await nextStart(appDir, appPort) | ||
}) | ||
afterAll(async () => { | ||
nextConfig.restore() | ||
await killApp(app) | ||
await killApp(proxyServer) | ||
}) | ||
|
||
runTests() | ||
}) | ||
|
||
describe('dev mode', () => { | ||
beforeAll(async () => { | ||
proxyPort = await findPort() | ||
proxyServer = await initNextServerScript( | ||
join(appDir, 'server.js'), | ||
/ready on/i, | ||
{ | ||
...process.env, | ||
PORT: proxyPort, | ||
} | ||
) | ||
|
||
nextConfig.replace('__EXTERNAL_PORT__', proxyPort) | ||
|
||
appPort = await findPort() | ||
app = await launchApp(appDir, appPort) | ||
}) | ||
afterAll(async () => { | ||
nextConfig.restore() | ||
await killApp(app) | ||
await killApp(proxyServer) | ||
}) | ||
|
||
runTests() | ||
}) | ||
}) |