Unverified Commit 8f914a3b authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

Fix error overlay hotlinking (#15658)

This pull request:

1. Relocates tests to correct location
1. Tests more scenarios
1. Fixes a bug with anchors at the start of error message
1. Fixes react key warning error

Fixes #15599
parent 59924967
...@@ -53,21 +53,33 @@ function getErrorSignature(ev: SupportedErrorEvent): string { ...@@ -53,21 +53,33 @@ function getErrorSignature(ev: SupportedErrorEvent): string {
return '' return ''
} }
function makeClickable(text: string): JSX.Element[] | string { const HotlinkedText: React.FC<{
// Regex Checks for http:// or https:// text: string
const linkRegex = /https?:\/\/[^\s/$.?#].[^\s]*/gi }> = function HotlinkedText(props) {
if (!linkRegex.test(text)) return text const { text } = props
return text.split(' ').map((word, index, array) => {
if (linkRegex.test(word)) { const linkRegex = /https?:\/\/[^\s/$.?#].[^\s]*/i
return ( return (
<> <>
<a href={word}>{word}</a> {linkRegex.test(text)
{index === array.length - 1 ? '' : ' '} ? text.split(' ').map((word, index, array) => {
</> if (linkRegex.test(word)) {
) return (
} <React.Fragment key={`link-${index}`}>
return index === array.length - 1 ? <>{word}</> : <>{word} </> <a href={word}>{word}</a>
}) {index === array.length - 1 ? '' : ' '}
</React.Fragment>
)
}
return index === array.length - 1 ? (
<React.Fragment key={`text-${index}`}>{word}</React.Fragment>
) : (
<React.Fragment key={`text-${index}`}>{word} </React.Fragment>
)
})
: text}
</>
)
} }
async function getErrorByType( async function getErrorByType(
...@@ -260,7 +272,7 @@ export const Errors: React.FC<ErrorsProps> = function Errors({ errors }) { ...@@ -260,7 +272,7 @@ export const Errors: React.FC<ErrorsProps> = function Errors({ errors }) {
</h1> </h1>
<p id="nextjs__container_errors_desc"> <p id="nextjs__container_errors_desc">
{activeError.error.name}:{' '} {activeError.error.name}:{' '}
{makeClickable(activeError.error.message)} <HotlinkedText text={activeError.error.message} />
</p> </p>
{isServerError ? ( {isServerError ? (
<div> <div>
......
...@@ -848,3 +848,197 @@ test('scss syntax errors', async () => { ...@@ -848,3 +848,197 @@ test('scss syntax errors', async () => {
await cleanup() await cleanup()
}) })
test('logbox: anchors links in error messages', async () => {
const [session, cleanup] = await sandbox()
await session.patch(
'index.js',
`
import { useCallback } from 'react'
export default function Index() {
const boom = useCallback(() => {
throw new Error('end http://nextjs.org')
}, [])
return (
<main>
<button onClick={boom}>Boom!</button>
</main>
)
}
`
)
expect(await session.hasRedbox()).toBe(false)
await session.evaluate(() => document.querySelector('button').click())
expect(await session.hasRedbox(true)).toBe(true)
const header = await session.getRedboxDescription()
expect(header).toMatchInlineSnapshot(`"Error: end http://nextjs.org"`)
expect(
await session.evaluate(
() =>
document
.querySelector('body > nextjs-portal')
.shadowRoot.querySelectorAll('#nextjs__container_errors_desc a')
.length
)
).toBe(1)
expect(
await session.evaluate(
() =>
document
.querySelector('body > nextjs-portal')
.shadowRoot.querySelector(
'#nextjs__container_errors_desc a:nth-of-type(1)'
).href
)
).toMatchInlineSnapshot(`"http://nextjs.org/"`)
await session.patch(
'index.js',
`
import { useCallback } from 'react'
export default function Index() {
const boom = useCallback(() => {
throw new Error('http://nextjs.org start')
}, [])
return (
<main>
<button onClick={boom}>Boom!</button>
</main>
)
}
`
)
expect(await session.hasRedbox()).toBe(false)
await session.evaluate(() => document.querySelector('button').click())
expect(await session.hasRedbox(true)).toBe(true)
const header2 = await session.getRedboxDescription()
expect(header2).toMatchInlineSnapshot(`"Error: http://nextjs.org start"`)
expect(
await session.evaluate(
() =>
document
.querySelector('body > nextjs-portal')
.shadowRoot.querySelectorAll('#nextjs__container_errors_desc a')
.length
)
).toBe(1)
expect(
await session.evaluate(
() =>
document
.querySelector('body > nextjs-portal')
.shadowRoot.querySelector(
'#nextjs__container_errors_desc a:nth-of-type(1)'
).href
)
).toMatchInlineSnapshot(`"http://nextjs.org/"`)
await session.patch(
'index.js',
`
import { useCallback } from 'react'
export default function Index() {
const boom = useCallback(() => {
throw new Error('middle http://nextjs.org end')
}, [])
return (
<main>
<button onClick={boom}>Boom!</button>
</main>
)
}
`
)
expect(await session.hasRedbox()).toBe(false)
await session.evaluate(() => document.querySelector('button').click())
expect(await session.hasRedbox(true)).toBe(true)
const header3 = await session.getRedboxDescription()
expect(header3).toMatchInlineSnapshot(`"Error: middle http://nextjs.org end"`)
expect(
await session.evaluate(
() =>
document
.querySelector('body > nextjs-portal')
.shadowRoot.querySelectorAll('#nextjs__container_errors_desc a')
.length
)
).toBe(1)
expect(
await session.evaluate(
() =>
document
.querySelector('body > nextjs-portal')
.shadowRoot.querySelector(
'#nextjs__container_errors_desc a:nth-of-type(1)'
).href
)
).toMatchInlineSnapshot(`"http://nextjs.org/"`)
await session.patch(
'index.js',
`
import { useCallback } from 'react'
export default function Index() {
const boom = useCallback(() => {
throw new Error('multiple http://nextjs.org links http://example.com')
}, [])
return (
<main>
<button onClick={boom}>Boom!</button>
</main>
)
}
`
)
expect(await session.hasRedbox()).toBe(false)
await session.evaluate(() => document.querySelector('button').click())
expect(await session.hasRedbox(true)).toBe(true)
const header4 = await session.getRedboxDescription()
expect(header4).toMatchInlineSnapshot(
`"Error: multiple http://nextjs.org links http://example.com"`
)
expect(
await session.evaluate(
() =>
document
.querySelector('body > nextjs-portal')
.shadowRoot.querySelectorAll('#nextjs__container_errors_desc a')
.length
)
).toBe(2)
expect(
await session.evaluate(
() =>
document
.querySelector('body > nextjs-portal')
.shadowRoot.querySelector(
'#nextjs__container_errors_desc a:nth-of-type(1)'
).href
)
).toMatchInlineSnapshot(`"http://nextjs.org/"`)
expect(
await session.evaluate(
() =>
document
.querySelector('body > nextjs-portal')
.shadowRoot.querySelector(
'#nextjs__container_errors_desc a:nth-of-type(2)'
).href
)
).toMatchInlineSnapshot(`"http://example.com/"`)
await cleanup()
})
...@@ -136,13 +136,26 @@ export async function sandbox(id = nanoid(), initialFiles = new Map()) { ...@@ -136,13 +136,26 @@ export async function sandbox(id = nanoid(), initialFiles = new Map()) {
} while (expected) } while (expected)
return false return false
}, },
async getRedboxDescription() {
return await this.evaluate(() => {
const portal = [].slice
.call(document.querySelectorAll('nextjs-portal'))
.find((p) =>
p.shadowRoot.querySelector('[data-nextjs-dialog-header]')
)
const root = portal.shadowRoot
return root
.querySelector('#nextjs__container_errors_desc')
.innerText.replace(/__WEBPACK_DEFAULT_EXPORT__/, 'Unknown')
})
},
async getRedboxSource(includeHeader = false) { async getRedboxSource(includeHeader = false) {
const header = includeHeader const header = includeHeader
? await this.evaluate(() => { ? await this.evaluate(() => {
const portal = [].slice const portal = [].slice
.call(document.querySelectorAll('nextjs-portal')) .call(document.querySelectorAll('nextjs-portal'))
.find((p) => .find((p) =>
p.shadowRoot.querySelector('[data-nextjs-dialog-header') p.shadowRoot.querySelector('[data-nextjs-dialog-header]')
) )
const root = portal.shadowRoot const root = portal.shadowRoot
return root return root
......
export default function Index() {
throw new Error(
'This error should be clickable. https://nextjs.org is the homepage. Visit https://nextjs.org/docs for documentation'
)
}
/* eslint-env jest */
import {
findPort,
killApp,
launchApp,
hasRedbox,
getRedboxHeader,
evaluate,
waitFor,
} from 'next-test-utils'
import webdriver from 'next-webdriver'
import { join } from 'path'
jest.setTimeout(1000 * 60 * 5)
let app
let appPort
const appDir = join(__dirname, '..')
describe('Clickable error link', () => {
beforeAll(async () => {
appPort = await findPort()
app = await launchApp(appDir, appPort, {
env: { __NEXT_TEST_WITH_DEVTOOL: 1 },
})
})
afterAll(() => killApp(app))
it('Shows error which is clickable and redirects', async () => {
const browser = await webdriver(appPort, '/first')
await hasRedbox(browser)
await evaluate(browser, () => {
document
.querySelector('body > nextjs-portal')
.shadowRoot.querySelector(
'#nextjs__container_errors_desc > a:nth-child(1)'
)
.click()
})
await waitFor(1000)
const url = await browser.url()
expect(url).toBe('https://nextjs.org/')
})
it('Handles multiple links in error', async () => {
const browser = await webdriver(appPort, '/first')
// Open the first link from error
await hasRedbox(browser)
await evaluate(browser, () => {
document
.querySelector('body > nextjs-portal')
.shadowRoot.querySelector(
'#nextjs__container_errors_desc > a:nth-child(1)'
)
.click()
})
await waitFor(1000)
const url1 = await browser.url()
// Go back to error page
await browser.back()
// Open the second link from error
await hasRedbox(browser)
await evaluate(browser, () => {
document
.querySelector('body > nextjs-portal')
.shadowRoot.querySelector(
'#nextjs__container_errors_desc > a:nth-child(2)'
)
.click()
})
await waitFor(1000)
const url2 = await browser.url()
expect({ url1, url2 }).toEqual({
url1: 'https://nextjs.org/',
url2: 'https://nextjs.org/docs',
})
})
it('Shows error message correctly', async () => {
const browser = await webdriver(appPort, '/first')
await hasRedbox(browser)
const headerText = await getRedboxHeader(browser)
expect(headerText).toMatch(
/Error: This error should be clickable\. https:\/\/nextjs\.org is the homepage\. Visit https:\/\/nextjs\.org\/docs for documentation/
)
})
})
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