Unverified Commit bbce270c authored by Andrew Clark's avatar Andrew Clark Committed by GitHub

[Segment Cache] Fix: canonicalURL omits origin (#76444)

The canonical URL is the href without the origin. I think historically
the reason for this is because the initial canonical URL gets passed as
a prop to the top-level React component, which means it needs to be
computed during SSR. If it were to include the origin, it would need to
always be same as location.origin on the client, to prevent a hydration
mismatch. To sidestep this complexity, we omit the origin.

However, since this is neither a native URL object nor a fully qualified
URL string, we need to be careful about how we use it. To prevent subtle
mistakes, we should create a special type for it, instead of just
string. Or, we should just use a (readonly) URL object instead. The type
of the prop that we pass to seed the initial state does not need to be
the same type as the state itself.

For now, I've left the type as-is and fixed its handling in the Segment
Cache. This fixes a test that was failing because the origin was not
being removed from the canonicalURL field. I'll refactor the type as a
follow-up.
parent 9fd1766f
...@@ -912,9 +912,10 @@ export async function fetchRouteOnCacheMiss( ...@@ -912,9 +912,10 @@ export async function fetchRouteOnCacheMiss(
} }
// In output: "export" mode, we need to add the segment path to the URL. // In output: "export" mode, we need to add the segment path to the URL.
const url = new URL(href)
const requestUrl = isOutputExportMode const requestUrl = isOutputExportMode
? addSegmentPathToUrlInOutputExportMode(href, segmentPath) ? addSegmentPathToUrlInOutputExportMode(url, segmentPath)
: href : url
try { try {
const response = await fetchPrefetchResponse(requestUrl, headers) const response = await fetchPrefetchResponse(requestUrl, headers)
...@@ -933,19 +934,30 @@ export async function fetchRouteOnCacheMiss( ...@@ -933,19 +934,30 @@ export async function fetchRouteOnCacheMiss(
return null return null
} }
// This is a bit convoluted but it's taken from router-reducer and // TODO: The canonical URL is the href without the origin. I think
// fetch-server-response // historically the reason for this is because the initial canonical URL
const canonicalUrl = response.redirected // gets passed as a prop to the top-level React component, which means it
? createHrefFromUrl( // needs to be computed during SSR. If it were to include the origin, it
new URL( // would need to always be same as location.origin on the client, to prevent
removeSegmentPathFromURLInOutputExportMode( // a hydration mismatch. To sidestep this complexity, we omit the origin.
//
// However, since this is neither a native URL object nor a fully qualified
// URL string, we need to be careful about how we use it. To prevent subtle
// mistakes, we should create a special type for it, instead of just string.
// Or, we should just use a (readonly) URL object instead. The type of the
// prop that we pass to seed the initial state does not need to be the same
// type as the state itself.
const canonicalUrl = createHrefFromUrl(
new URL(
response.redirected
? removeSegmentPathFromURLInOutputExportMode(
href, href,
requestUrl, requestUrl.href,
response.url response.url
) )
) : href
) )
: href )
// Check whether the response varies based on the Next-Url header. // Check whether the response varies based on the Next-Url header.
const varyHeader = response.headers.get('vary') const varyHeader = response.headers.get('vary')
...@@ -1072,21 +1084,12 @@ export async function fetchSegmentOnCacheMiss( ...@@ -1072,21 +1084,12 @@ export async function fetchSegmentOnCacheMiss(
// //
// Segment fetches are non-blocking so we don't need to ping the scheduler // Segment fetches are non-blocking so we don't need to ping the scheduler
// on completion. // on completion.
const href =
route.canonicalUrl !== routeKey.href // Use the canonical URL to request the segment, not the original URL. These
? // The route was redirected. If we request the segment data using the // are usually the same, but the canonical URL will be different if the route
// same URL, that request will be redirected, too. To avoid an extra // tree response was redirected. To avoid an extra waterfall on every segment
// waterfall on every segment request, pass the redirected URL instead // request, we pass the redirected URL instead of the original one.
// of the original one. const url = new URL(route.canonicalUrl, routeKey.href)
//
// Since the redirected URL might be a relative path, we need to resolve
// it against the original href, which is a fully qualified URL.
//
// TODO: We should just store the fully qualified URL as canonical URL.
// There are other parts of the router that currently expect a relative
// path, so need to update those, too.
new URL(route.canonicalUrl, routeKey.href).href
: routeKey.href
const nextUrl = routeKey.nextUrl const nextUrl = routeKey.nextUrl
const normalizedSegmentPath = const normalizedSegmentPath =
...@@ -1109,11 +1112,10 @@ export async function fetchSegmentOnCacheMiss( ...@@ -1109,11 +1112,10 @@ export async function fetchSegmentOnCacheMiss(
headers[NEXT_URL] = nextUrl headers[NEXT_URL] = nextUrl
} }
// In output: "export" mode, we need to add the segment path to the URL.
const requestUrl = isOutputExportMode const requestUrl = isOutputExportMode
? addSegmentPathToUrlInOutputExportMode(href, normalizedSegmentPath) ? // In output: "export" mode, we need to add the segment path to the URL.
: href addSegmentPathToUrlInOutputExportMode(url, normalizedSegmentPath)
: url
try { try {
const response = await fetchPrefetchResponse(requestUrl, headers) const response = await fetchPrefetchResponse(requestUrl, headers)
if ( if (
...@@ -1191,7 +1193,7 @@ export async function fetchSegmentPrefetchesUsingDynamicRequest( ...@@ -1191,7 +1193,7 @@ export async function fetchSegmentPrefetchesUsingDynamicRequest(
dynamicRequestTree: FlightRouterState, dynamicRequestTree: FlightRouterState,
spawnedEntries: Map<string, PendingSegmentCacheEntry> spawnedEntries: Map<string, PendingSegmentCacheEntry>
): Promise<PrefetchSubtaskResult<null> | null> { ): Promise<PrefetchSubtaskResult<null> | null> {
const href = task.key.href const url = new URL(route.canonicalUrl, task.key.href)
const nextUrl = task.key.nextUrl const nextUrl = task.key.nextUrl
const headers: RequestHeaders = { const headers: RequestHeaders = {
[RSC_HEADER]: '1', [RSC_HEADER]: '1',
...@@ -1210,7 +1212,7 @@ export async function fetchSegmentPrefetchesUsingDynamicRequest( ...@@ -1210,7 +1212,7 @@ export async function fetchSegmentPrefetchesUsingDynamicRequest(
headers[NEXT_ROUTER_PREFETCH_HEADER] = '1' headers[NEXT_ROUTER_PREFETCH_HEADER] = '1'
} }
try { try {
const response = await fetchPrefetchResponse(href, headers) const response = await fetchPrefetchResponse(url, headers)
if (!response || !response.ok || !response.body) { if (!response || !response.ok || !response.body) {
// Server responded with an error, or with a miss. We should still cache // Server responded with an error, or with a miss. We should still cache
// the response, but we can try again after 10 seconds. // the response, but we can try again after 10 seconds.
...@@ -1492,11 +1494,11 @@ function writeSeedDataIntoCache( ...@@ -1492,11 +1494,11 @@ function writeSeedDataIntoCache(
} }
async function fetchPrefetchResponse( async function fetchPrefetchResponse(
href: string, url: URL,
headers: RequestHeaders headers: RequestHeaders
): Promise<Response | null> { ): Promise<Response | null> {
const fetchPriority = 'low' const fetchPriority = 'low'
const response = await createFetch(new URL(href), headers, fetchPriority) const response = await createFetch(url, headers, fetchPriority)
if (!response.ok) { if (!response.ok) {
return null return null
} }
...@@ -1566,9 +1568,9 @@ function createPrefetchResponseStream( ...@@ -1566,9 +1568,9 @@ function createPrefetchResponseStream(
} }
function addSegmentPathToUrlInOutputExportMode( function addSegmentPathToUrlInOutputExportMode(
url: string, url: URL,
segmentPath: string segmentPath: string
) { ): URL {
if (isOutputExportMode) { if (isOutputExportMode) {
// In output: "export" mode, we cannot use a header to encode the segment // In output: "export" mode, we cannot use a header to encode the segment
// path. Instead, we append it to the end of the pathname. // path. Instead, we append it to the end of the pathname.
...@@ -1579,7 +1581,7 @@ function addSegmentPathToUrlInOutputExportMode( ...@@ -1579,7 +1581,7 @@ function addSegmentPathToUrlInOutputExportMode(
const staticExportFilename = const staticExportFilename =
convertSegmentPathToStaticExportFilename(segmentPath) convertSegmentPathToStaticExportFilename(segmentPath)
staticUrl.pathname = `${routeDir}/${staticExportFilename}` staticUrl.pathname = `${routeDir}/${staticExportFilename}`
return staticUrl.href return staticUrl
} }
return url return url
} }
......
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