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

Avoid prefetch request before meta-tag-based redirect #70350

Merged
merged 1 commit into from
Sep 24, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ export function navigateReducer(
return handleExternalUrl(state, mutable, url.toString(), pendingPush)
}

// Handles case where `<meta http-equiv="refresh">` tag is present,
// which will trigger an MPA navigation.
if (document.getElementById('__next-page-redirect')) {
return handleExternalUrl(state, mutable, href, pendingPush)
}

const prefetchValues = getOrCreatePrefetchCacheEntry({
url,
nextUrl: state.nextUrl,
Expand All @@ -145,12 +151,6 @@ export function navigateReducer(
return handleExternalUrl(state, mutable, flightData, pendingPush)
}

// Handles case where `<meta http-equiv="refresh">` tag is present,
// which will trigger an MPA navigation.
if (document.getElementById('__next-page-redirect')) {
return handleExternalUrl(state, mutable, href, pendingPush)
}

// When the server indicates an override for the canonical URL (such as a redirect in middleware)
// we only want to use that if we're not using an aliased entry as the redirect will correspond with
// the aliased prefetch which might have different search params. Since we're only using the aliased entry
Expand Down
20 changes: 18 additions & 2 deletions test/e2e/app-dir/navigation/navigation.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { nextTestSetup } from 'e2e-utils'
import { retry, waitFor } from 'next-test-utils'
import type { Response } from 'playwright'
import type { Request, Response } from 'playwright'

describe('app dir - navigation', () => {
const { next, isNextDev, isNextStart, isNextDeploy } = nextTestSetup({
Expand Down Expand Up @@ -488,7 +488,16 @@ describe('app dir - navigation', () => {
it.each(['/redirect/servercomponent', 'redirect/redirect-with-loading'])(
'should only trigger the redirect once (%s)',
async (path) => {
const browser = await next.browser(path)
const requestedPathnames: string[] = []

const browser = await next.browser(path, {
beforePageLoad(page) {
page.on('request', async (req: Request) => {
requestedPathnames.push(new URL(req.url()).pathname)
})
},
})

const initialTimestamp = await browser
.waitForElementByCss('#timestamp')
.text()
Expand Down Expand Up @@ -523,6 +532,13 @@ describe('app dir - navigation', () => {
}
// If it's our "forcing continue" error, do nothing. This means we succeeded.
}

// Ensure the redirect target page was only requested once.
expect(
requestedPathnames.filter(
(pathname) => pathname === '/redirect/result'
)
).toHaveLength(1)
}
)
})
Expand Down
Loading