Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix infinite navigate events when Promise is proxied #54394

Merged
merged 2 commits into from Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -199,7 +199,7 @@ export function navigateReducer(
mutable.canonicalUrl = href

state.prefetchCache.set(createHrefFromUrl(url, false), {
data: Promise.resolve(data),
data: createRecordFromThenable(Promise.resolve(data)),
// this will make sure that the entry will be discarded after 30s
kind: PrefetchKind.TEMPORARY,
prefetchTime: Date.now(),
Expand All @@ -226,7 +226,7 @@ export function navigateReducer(
)

const newPrefetchValue = {
data: Promise.resolve(data),
data: createRecordFromThenable(Promise.resolve(data)),
// this will make sure that the entry will be discarded after 30s
kind:
process.env.NODE_ENV === 'development'
Expand Down
Expand Up @@ -17,6 +17,7 @@ const CategoryNav = ({ categories }) => {
key={item.slug}
href={`/nested-navigation/${item.slug}`}
isActive={item.slug === selectedLayoutSegment}
prefetch={item.prefetch}
>
{item.name}
</TabNavItem>
Expand Down
@@ -1,8 +1,12 @@
import Link from 'next/link'

export const TabNavItem = ({ children, href }) => {
export const TabNavItem = ({ children, href, prefetch }) => {
return (
<Link href={href} style={{ margin: '10px', display: 'block' }}>
<Link
href={href}
style={{ margin: '10px', display: 'block' }}
prefetch={prefetch}
>
{children}
</Link>
)
Expand Down
Expand Up @@ -29,6 +29,13 @@ export const getCategories = () => [
{ name: 'Education', slug: 'education', count: 3 },
],
},
{
name: 'Shoes',
slug: 'shoes',
count: 5,
items: [],
prefetch: false,
},
]

export async function fetchCategoryBySlug(slug) {
Expand Down
40 changes: 40 additions & 0 deletions test/e2e/app-dir/navigation/navigation.test.ts
Expand Up @@ -571,5 +571,45 @@ createNextDescribe(
)
})
})

describe('navigations when attaching a Proxy to `window.Promise`', () => {
it('should navigate without issue', async () => {
const browser = await next.browser('/nested-navigation')
await browser.eval(`window.Promise = new Proxy(window.Promise, {})`)

expect(await browser.elementByCss('h1').text()).toBe('Home')

const pages = [
['Electronics', ['Phones', 'Tablets', 'Laptops']],
['Clothing', ['Tops', 'Shorts', 'Shoes']],
['Books', ['Fiction', 'Biography', 'Education']],
['Shoes', []],
] as const

for (const [category, subCategories] of pages) {
expect(
await browser
.elementByCss(
`a[href="/nested-navigation/${category.toLowerCase()}"]`
)
.click()
.waitForElementByCss(`#all-${category.toLowerCase()}`)
.text()
).toBe(`All ${category}`)

for (const subcategory of subCategories) {
expect(
await browser
.elementByCss(
`a[href="/nested-navigation/${category.toLowerCase()}/${subcategory.toLowerCase()}"]`
)
.click()
.waitForElementByCss(`#${subcategory.toLowerCase()}`)
.text()
).toBe(`${subcategory}`)
}
}
})
})
}
)