Unverified Commit 363783db authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

Simplify `getImplicitTags` to accept pathname instead of url object (#88753)

The function only used `url.pathname`, not `url.search`.
parent e5926d3d
...@@ -2023,15 +2023,12 @@ async function renderToHTMLOrFlightImpl( ...@@ -2023,15 +2023,12 @@ async function renderToHTMLOrFlightImpl(
// fetch cache. Using the destination path ensures that // fetch cache. Using the destination path ensures that
// revalidatePath('/dest') invalidates cache entries for pages rewritten to // revalidatePath('/dest') invalidates cache entries for pages rewritten to
// that destination. // that destination.
// TODO: simplify getImplicitTags to accept pathname instead of url object. const implicitTagsPathname =
const rewrittenPathname = getRequestMeta(req, 'rewrittenPathname') getRequestMeta(req, 'rewrittenPathname') || url.pathname
const implicitTagsUrl = rewrittenPathname
? { ...url, pathname: rewrittenPathname }
: url
const implicitTags = await getImplicitTags( const implicitTags = await getImplicitTags(
workStore.page, workStore.page,
implicitTagsUrl, implicitTagsPathname,
fallbackRouteParams fallbackRouteParams
) )
......
...@@ -4,49 +4,49 @@ import { getImplicitTags } from './implicit-tags' ...@@ -4,49 +4,49 @@ import { getImplicitTags } from './implicit-tags'
describe('getImplicitTags()', () => { describe('getImplicitTags()', () => {
it.each<{ it.each<{
page: string page: string
url: { pathname: string; search: string } pathname: string
fallbackRouteParams: null | OpaqueFallbackRouteParams fallbackRouteParams: null | OpaqueFallbackRouteParams
expectedTags: string[] expectedTags: string[]
}>([ }>([
{ {
page: '/', page: '/',
url: { pathname: '/', search: '' }, pathname: '/',
fallbackRouteParams: null, fallbackRouteParams: null,
expectedTags: ['_N_T_/layout', '_N_T_/', '_N_T_/index'], expectedTags: ['_N_T_/layout', '_N_T_/', '_N_T_/index'],
}, },
{ {
page: '', page: '',
url: { pathname: '/', search: '' }, pathname: '/',
fallbackRouteParams: null, fallbackRouteParams: null,
expectedTags: ['_N_T_/layout', '_N_T_/', '_N_T_/index'], expectedTags: ['_N_T_/layout', '_N_T_/', '_N_T_/index'],
}, },
{ {
page: '/', page: '/',
url: { pathname: '', search: '' }, pathname: '',
fallbackRouteParams: null, fallbackRouteParams: null,
expectedTags: ['_N_T_/layout'], expectedTags: ['_N_T_/layout'],
}, },
{ {
page: '/page', page: '/page',
url: { pathname: '', search: '' }, pathname: '',
fallbackRouteParams: null, fallbackRouteParams: null,
expectedTags: ['_N_T_/layout', '_N_T_/page'], expectedTags: ['_N_T_/layout', '_N_T_/page'],
}, },
{ {
page: '/page', page: '/page',
url: { pathname: '/', search: '' }, pathname: '/',
fallbackRouteParams: null, fallbackRouteParams: null,
expectedTags: ['_N_T_/layout', '_N_T_/page', '_N_T_/', '_N_T_/index'], expectedTags: ['_N_T_/layout', '_N_T_/page', '_N_T_/', '_N_T_/index'],
}, },
{ {
page: '/page', page: '/page',
url: { pathname: '/page', search: '' }, pathname: '/page',
fallbackRouteParams: null, fallbackRouteParams: null,
expectedTags: ['_N_T_/layout', '_N_T_/page'], expectedTags: ['_N_T_/layout', '_N_T_/page'],
}, },
{ {
page: '/index', page: '/index',
url: { pathname: '/', search: '' }, pathname: '/',
fallbackRouteParams: null, fallbackRouteParams: null,
expectedTags: [ expectedTags: [
'_N_T_/layout', '_N_T_/layout',
...@@ -57,13 +57,13 @@ describe('getImplicitTags()', () => { ...@@ -57,13 +57,13 @@ describe('getImplicitTags()', () => {
}, },
{ {
page: '/hello', page: '/hello',
url: { pathname: '/hello', search: '' }, pathname: '/hello',
fallbackRouteParams: null, fallbackRouteParams: null,
expectedTags: ['_N_T_/layout', '_N_T_/hello/layout', '_N_T_/hello'], expectedTags: ['_N_T_/layout', '_N_T_/hello/layout', '_N_T_/hello'],
}, },
{ {
page: '/foo/bar/baz', page: '/foo/bar/baz',
url: { pathname: '/foo/bar/baz', search: '' }, pathname: '/foo/bar/baz',
fallbackRouteParams: null, fallbackRouteParams: null,
expectedTags: [ expectedTags: [
'_N_T_/layout', '_N_T_/layout',
...@@ -74,9 +74,9 @@ describe('getImplicitTags()', () => { ...@@ -74,9 +74,9 @@ describe('getImplicitTags()', () => {
], ],
}, },
])( ])(
'for page $page with url $url and $fallback', 'for page $page with pathname $pathname',
async ({ page, url, fallbackRouteParams, expectedTags }) => { async ({ page, pathname, fallbackRouteParams, expectedTags }) => {
const result = await getImplicitTags(page, url, fallbackRouteParams) const result = await getImplicitTags(page, pathname, fallbackRouteParams)
expect(result.tags).toEqual(expectedTags) expect(result.tags).toEqual(expectedTags)
} }
) )
......
...@@ -73,10 +73,7 @@ function createTagsExpirationsByCacheKind( ...@@ -73,10 +73,7 @@ function createTagsExpirationsByCacheKind(
export async function getImplicitTags( export async function getImplicitTags(
page: string, page: string,
url: { pathname: string,
pathname: string
search?: string
},
fallbackRouteParams: null | OpaqueFallbackRouteParams fallbackRouteParams: null | OpaqueFallbackRouteParams
): Promise<ImplicitTags> { ): Promise<ImplicitTags> {
const tags = new Set<string>() const tags = new Set<string>()
...@@ -90,11 +87,8 @@ export async function getImplicitTags( ...@@ -90,11 +87,8 @@ export async function getImplicitTags(
// Add the tags from the pathname. If the route has unknown params, we don't // Add the tags from the pathname. If the route has unknown params, we don't
// want to add the pathname as a tag, as it will be invalid. // want to add the pathname as a tag, as it will be invalid.
if ( if (pathname && (!fallbackRouteParams || fallbackRouteParams.size === 0)) {
url.pathname && const tag = `${NEXT_CACHE_IMPLICIT_TAG_ID}${pathname}`
(!fallbackRouteParams || fallbackRouteParams.size === 0)
) {
const tag = `${NEXT_CACHE_IMPLICIT_TAG_ID}${url.pathname}`
tags.add(tag) tags.add(tag)
} }
......
...@@ -703,7 +703,7 @@ export class AppRouteRouteModule extends RouteModule< ...@@ -703,7 +703,7 @@ export class AppRouteRouteModule extends RouteModule<
const implicitTags = await getImplicitTags( const implicitTags = await getImplicitTags(
this.definition.page, this.definition.page,
req.nextUrl, req.nextUrl.pathname,
// App Routes don't support unknown route params. // App Routes don't support unknown route params.
null null
) )
......
...@@ -283,7 +283,7 @@ export async function adapter( ...@@ -283,7 +283,7 @@ export async function adapter(
const implicitTags = await getImplicitTags( const implicitTags = await getImplicitTags(
page, page,
request.nextUrl, request.nextUrl.pathname,
fallbackRouteParams fallbackRouteParams
) )
......
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