Unverified Commit d5b1d595 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

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
parent 84c299bd
...@@ -1132,13 +1132,16 @@ export default class Router implements BaseRouter { ...@@ -1132,13 +1132,16 @@ export default class Router implements BaseRouter {
// handle redirect on client-transition // handle redirect on client-transition
if ((__N_SSG || __N_SSP) && props) { if ((__N_SSG || __N_SSP) && props) {
if ((props as any).pageProps && (props as any).pageProps.__N_REDIRECT) { if (props.pageProps && props.pageProps.__N_REDIRECT) {
const destination = (props as any).pageProps.__N_REDIRECT const destination = props.pageProps.__N_REDIRECT
// check if destination is internal (resolves to a page) and attempt // check if destination is internal (resolves to a page) and attempt
// client-navigation if it is falling back to hard navigation if // client-navigation if it is falling back to hard navigation if
// it's not // it's not
if (destination.startsWith('/')) { if (
destination.startsWith('/') &&
props.pageProps.__N_REDIRECT_BASE_PATH !== false
) {
const parsedHref = parseRelativeUrl(destination) const parsedHref = parseRelativeUrl(destination)
parsedHref.pathname = resolveDynamicRoute( parsedHref.pathname = resolveDynamicRoute(
parsedHref.pathname, parsedHref.pathname,
......
module.exports = (phase, { isServer }) => {
return new Promise((resolve) => {
resolve({ target: 'serverless' })
})
}
/* eslint-env jest */ /* eslint-env jest */
import fs from 'fs-extra'
import { join } from 'path' 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', () => { 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 () => { 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 }) => { module.exports = (phase, { isServer }) => {
return new Promise((resolve) => { return new Promise((resolve) => {
resolve({ target: 'serverless' }) 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:\/\// /Error: > Promise returned in next config\. https:\/\//
) )
}) })
it('should warn when a promise is returned on webpack', async () => { it('should warn when a promise is returned on webpack', async () => {
configFile.write(` fs.writeFile(
setTimeout(() => process.exit(0), 2 * 1000) join(appDir, 'next.config.js'),
`
module.exports = (phase, { isServer }) => { module.exports = (phase, { isServer }) => {
return { return {
webpack: async (config) => { webpack: async (config) => {
...@@ -37,13 +42,15 @@ describe('Promise in next 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:\/\//
)
}) })
}) })
...@@ -51,9 +51,18 @@ const runTests = (isDev) => { ...@@ -51,9 +51,18 @@ const runTests = (isDev) => {
) )
expect(res.status).toBe(307) 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`)
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/
)
expect(pathname).toBe(`/404`) 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 () => { it('should apply permanent redirect when visited directly for GSSP page', async () => {
......
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