Fix basePath: false not being honored for client-side redirect (#29235)

* Fix basePath: false not being honored for client-side redirect

* update test

* fix remove flakey exit
This commit is contained in:
JJ Kasper 2021-09-21 09:23:13 -05:00 committed by GitHub
parent 84c299bd57
commit d5b1d595c5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 28 deletions

View file

@ -1132,13 +1132,16 @@ export default class Router implements BaseRouter {
// handle redirect on client-transition
if ((__N_SSG || __N_SSP) && props) {
if ((props as any).pageProps && (props as any).pageProps.__N_REDIRECT) {
const destination = (props as any).pageProps.__N_REDIRECT
if (props.pageProps && props.pageProps.__N_REDIRECT) {
const destination = props.pageProps.__N_REDIRECT
// check if destination is internal (resolves to a page) and attempt
// client-navigation if it is falling back to hard navigation if
// it's not
if (destination.startsWith('/')) {
if (
destination.startsWith('/') &&
props.pageProps.__N_REDIRECT_BASE_PATH !== false
) {
const parsedHref = parseRelativeUrl(destination)
parsedHref.pathname = resolveDynamicRoute(
parsedHref.pathname,

View file

@ -1,5 +0,0 @@
module.exports = (phase, { isServer }) => {
return new Promise((resolve) => {
resolve({ target: 'serverless' })
})
}

View file

@ -1,35 +1,40 @@
/* eslint-env jest */
import fs from 'fs-extra'
import { join } from 'path'
import { runNextCommand, findPort, File } from 'next-test-utils'
import { nextBuild } from 'next-test-utils'
const configFile = new File(join(__dirname, '../next.config.js'))
const appDir = join(__dirname, '..')
describe('Promise in next config', () => {
afterAll(() => configFile.restore())
afterEach(() => fs.remove(join(appDir, 'next.config.js')))
it('should throw error when a promise is return on config', async () => {
configFile.write(`
fs.writeFile(
join(appDir, 'next.config.js'),
`
module.exports = (phase, { isServer }) => {
return new Promise((resolve) => {
resolve({ target: 'serverless' })
})
}
`)
const { stderr } = await runNextCommand(
['dev', join(__dirname, '..'), '-p', await findPort()],
{ stderr: true }
`
)
expect(stderr).toMatch(
const { stderr, stdout } = await nextBuild(appDir, undefined, {
stderr: true,
stdout: true,
})
expect(stderr + stdout).toMatch(
/Error: > Promise returned in next config\. https:\/\//
)
})
it('should warn when a promise is returned on webpack', async () => {
configFile.write(`
setTimeout(() => process.exit(0), 2 * 1000)
fs.writeFile(
join(appDir, 'next.config.js'),
`
module.exports = (phase, { isServer }) => {
return {
webpack: async (config) => {
@ -37,13 +42,15 @@ describe('Promise in next config', () => {
}
}
}
`)
const { stderr } = await runNextCommand(
['dev', join(__dirname, '..'), '-p', await findPort()],
{ stderr: true }
`
)
expect(stderr).toMatch(/> Promise returned in next config\. https:\/\//)
const { stderr, stdout } = await nextBuild(appDir, undefined, {
stderr: true,
stdout: true,
})
expect(stderr + stdout).toMatch(
/> Promise returned in next config\. https:\/\//
)
})
})

View file

@ -51,9 +51,18 @@ const runTests = (isDev) => {
)
expect(res.status).toBe(307)
const { pathname } = url.parse(res.headers.get('location'))
const parsedUrl = url.parse(res.headers.get('location'))
expect(parsedUrl.pathname).toBe(`/404`)
expect(pathname).toBe(`/404`)
const browser = await webdriver(appPort, `${basePath}`)
await browser.eval(`next.router.push('/gssp-blog/redirect-1-no-basepath-')`)
await check(
() => browser.eval('document.documentElement.innerHTML'),
/oops not found/
)
const parsedUrl2 = url.parse(await browser.eval('window.location.href'))
expect(parsedUrl2.pathname).toBe('/404')
})
it('should apply permanent redirect when visited directly for GSSP page', async () => {