Unverified Commit 6d71eef5 authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

Do not assign to readonly property in Safari (#16051)

parent 4a04212d
import mitt from '../next-server/lib/mitt' import mitt from '../next-server/lib/mitt'
import { addBasePath, markLoadingError } from '../next-server/lib/router/router'
import escapePathDelimiters from '../next-server/lib/router/utils/escape-path-delimiters'
import getAssetPathFromRoute from './../next-server/lib/router/utils/get-asset-path-from-route'
import { isDynamicRoute } from './../next-server/lib/router/utils/is-dynamic' import { isDynamicRoute } from './../next-server/lib/router/utils/is-dynamic'
import { parseRelativeUrl } from './../next-server/lib/router/utils/parse-relative-url'
import { searchParamsToUrlQuery } from './../next-server/lib/router/utils/querystring'
import { getRouteMatcher } from './../next-server/lib/router/utils/route-matcher' import { getRouteMatcher } from './../next-server/lib/router/utils/route-matcher'
import { getRouteRegex } from './../next-server/lib/router/utils/route-regex' import { getRouteRegex } from './../next-server/lib/router/utils/route-regex'
import { searchParamsToUrlQuery } from './../next-server/lib/router/utils/querystring'
import { parseRelativeUrl } from './../next-server/lib/router/utils/parse-relative-url'
import escapePathDelimiters from '../next-server/lib/router/utils/escape-path-delimiters'
import getAssetPathFromRoute from './../next-server/lib/router/utils/get-asset-path-from-route'
import { addBasePath } from '../next-server/lib/router/router'
function hasRel(rel, link) { function hasRel(rel, link) {
try { try {
...@@ -16,9 +16,7 @@ function hasRel(rel, link) { ...@@ -16,9 +16,7 @@ function hasRel(rel, link) {
} }
function pageLoadError(route) { function pageLoadError(route) {
const error = new Error(`Error loading ${route}`) return markLoadingError(new Error(`Error loading ${route}`))
error.code = 'PAGE_LOAD_ERROR'
return error
} }
const relPrefetch = const relPrefetch =
......
...@@ -98,6 +98,11 @@ export function resolveHref(currentPath: string, href: Url): string { ...@@ -98,6 +98,11 @@ export function resolveHref(currentPath: string, href: Url): string {
} }
} }
const PAGE_LOAD_ERROR = Symbol('PAGE_LOAD_ERROR')
export function markLoadingError(err: Error): Error {
return Object.defineProperty(err, PAGE_LOAD_ERROR, {})
}
function prepareUrlAs(router: NextRouter, url: Url, as: Url) { function prepareUrlAs(router: NextRouter, url: Url, as: Url) {
// If url and as provided as an object representation, // If url and as provided as an object representation,
// we'll format them into the string version here. // we'll format them into the string version here.
...@@ -205,7 +210,7 @@ function fetchNextData(dataHref: string, isServerRender: boolean) { ...@@ -205,7 +210,7 @@ function fetchNextData(dataHref: string, isServerRender: boolean) {
// on a client-side transition. Otherwise, we'd get into an infinite // on a client-side transition. Otherwise, we'd get into an infinite
// loop. // loop.
if (!isServerRender) { if (!isServerRender) {
;(err as any).code = 'PAGE_LOAD_ERROR' markLoadingError(err)
} }
throw err throw err
}) })
...@@ -641,7 +646,7 @@ export default class Router implements BaseRouter { ...@@ -641,7 +646,7 @@ export default class Router implements BaseRouter {
throw err throw err
} }
if (err.code === 'PAGE_LOAD_ERROR' || loadErrorFail) { if (PAGE_LOAD_ERROR in err || loadErrorFail) {
Router.events.emit('routeChangeError', err, as) Router.events.emit('routeChangeError', err, as)
// If we can't load the page it could be one of following reasons // If we can't load the page it could be one of following reasons
......
...@@ -10,6 +10,16 @@ module.exports = { ...@@ -10,6 +10,16 @@ module.exports = {
destination: '/:lang/about', destination: '/:lang/about',
permanent: false, permanent: false,
}, },
{
source: '/nonexistent',
destination: '/about',
permanent: false,
},
{
source: '/shadowed-page',
destination: '/about',
permanent: false,
},
] ]
}, },
} }
export function getServerSideProps() {
throw new Error('oops!')
}
export default function ShadowedPage() {
return <div id="shadowed-page">Not routable!</div>
}
import Link from 'next/link'
export default function ToNonexistent() {
return (
<div id="blackhole-page">
<Link href="/nonexistent">
<a id="to-nonexistent-page">Nonexistent Page</a>
</Link>
</div>
)
}
import Link from 'next/link'
export default function ToShadowed() {
return (
<div id="shadowed-page-gateway">
<Link href="/shadowed-page">
<a id="to-shadowed-page">Shadowed Page</a>
</Link>
</div>
)
}
...@@ -373,6 +373,31 @@ describe('Production Usage', () => { ...@@ -373,6 +373,31 @@ describe('Production Usage', () => {
expect(title).toBe('hello from title') expect(title).toBe('hello from title')
expect(url).toBe('/with-title') expect(url).toBe('/with-title')
}) })
it('should reload page successfully (on bad link)', async () => {
const browser = await webdriver(appPort, '/to-nonexistent')
await browser.eval(function setup() {
window.__DATA_BE_GONE = 'true'
})
await browser.waitForElementByCss('#to-nonexistent-page')
await browser.click('#to-nonexistent-page')
await browser.waitForElementByCss('.about-page')
const oldData = await browser.eval(`window.__DATA_BE_GONE`)
expect(oldData).toBeFalsy()
})
it('should reload page successfully (on bad data fetch)', async () => {
const browser = await webdriver(appPort, '/to-shadowed-page')
await browser.eval(function setup() {
window.__DATA_BE_GONE = 'true'
})
await browser.waitForElementByCss('#to-shadowed-page').click()
await browser.waitForElementByCss('.about-page')
const oldData = await browser.eval(`window.__DATA_BE_GONE`)
expect(oldData).toBeFalsy()
})
}) })
it('should navigate to external site and back', async () => { it('should navigate to external site and back', 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