Skip to content

Commit

Permalink
Merge branch 'canary' into add-collections
Browse files Browse the repository at this point in the history
  • Loading branch information
kodiakhq[bot] committed Nov 9, 2021
2 parents e55b8f5 + 764e29c commit 242e9c1
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 8 deletions.
6 changes: 5 additions & 1 deletion packages/next/build/swc/options.js
Expand Up @@ -43,7 +43,11 @@ function getBaseSWCOptions({
},
},
},
styledComponents: styledComponents ? {} : null,
styledComponents: styledComponents
? {
displayName: Boolean(development),
}
: null,
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/next/shared/lib/router/router.ts
Expand Up @@ -1697,7 +1697,7 @@ export default class Router implements BaseRouter {
}

const effects = await this._preflightRequest({
as: asPath,
as: addBasePath(asPath),
cache: true,
pages,
pathname,
Expand Down
10 changes: 10 additions & 0 deletions test/integration/middleware/with-base-path/pages/_middleware.js
Expand Up @@ -2,6 +2,16 @@ import { NextResponse } from 'next/server'

export async function middleware(request) {
const url = request.nextUrl

if (
request.method === 'HEAD' &&
url.basePath === '/root' &&
url.pathname === '/redirect-me-to-about'
) {
url.pathname = '/about'
return NextResponse.redirect(url)
}

if (url.pathname === '/redirect-with-basepath' && !url.basePath) {
url.basePath = '/root'
return NextResponse.redirect(url)
Expand Down
5 changes: 5 additions & 0 deletions test/integration/middleware/with-base-path/pages/index.js
Expand Up @@ -22,6 +22,11 @@ export default function Main({ message }) {
<a>Rewrite me to Vercel</a>
</Link>
</li>
<li>
<Link href="/redirect-me-to-about">
<a>redirect me to about</a>
</Link>
</li>
</ul>
</div>
)
Expand Down
50 changes: 44 additions & 6 deletions test/integration/middleware/with-base-path/test/index.test.js
Expand Up @@ -2,7 +2,14 @@

jest.setTimeout(1000 * 60 * 2)

import { fetchViaHTTP, findPort, killApp, launchApp } from 'next-test-utils'
import {
fetchViaHTTP,
findPort,
killApp,
launchApp,
nextBuild,
nextStart,
} from 'next-test-utils'
import { join } from 'path'
import cheerio from 'cheerio'
import webdriver from 'next-webdriver'
Expand All @@ -11,12 +18,25 @@ const context = {}
context.appDir = join(__dirname, '../')

describe('Middleware base tests', () => {
beforeAll(async () => {
context.appPort = await findPort()
context.app = await launchApp(context.appDir, context.appPort)
describe('dev mode', () => {
beforeAll(async () => {
context.appPort = await findPort()
context.app = await launchApp(context.appDir, context.appPort)
})
afterAll(() => killApp(context.app))
runTests()
})

describe('production mode', () => {
beforeAll(async () => {
await nextBuild(context.appDir)
context.appPort = await findPort()
context.app = await nextStart(context.appDir, context.appPort)
})
afterAll(() => killApp(context.app))
runTests()
runPreflightTests()
})
afterAll(() => killApp(context.app))
runTests()
})

function runTests() {
Expand All @@ -39,3 +59,21 @@ function runTests() {
expect($('.title').text()).toBe('About Page')
})
}

function runPreflightTests() {
it('should redirect via preflight middleware request', async () => {
const browser = await webdriver(context.appPort, '/root')

try {
await browser.waitForCondition(
'next.router && Object.keys(next.router.sde).length == 4'
)
const redirect = await browser.eval(
`next.router.sde["http://localhost:${context.appPort}/root/redirect-me-to-about"].redirect`
)
expect(redirect).toBe('/root/about')
} finally {
await browser.close()
}
})
}

0 comments on commit 242e9c1

Please sign in to comment.