remove external page links from consolidated views (#5880)
This commit is contained in:
parent
b0e8c8bdd0
commit
dfbf0a9f4e
|
|
@ -29,7 +29,7 @@ export function parseSiteFromDataset(dataset: DOMStringMap): PlausibleSite {
|
||||||
// Update this object when new feature flags are added to the frontend.
|
// Update this object when new feature flags are added to the frontend.
|
||||||
type FeatureFlags = Record<never, boolean>
|
type FeatureFlags = Record<never, boolean>
|
||||||
|
|
||||||
const siteContextDefaultValue = {
|
export const siteContextDefaultValue = {
|
||||||
domain: '',
|
domain: '',
|
||||||
/** offset in seconds from UTC at site load time, @example 7200 */
|
/** offset in seconds from UTC at site load time, @example 7200 */
|
||||||
offset: 0,
|
offset: 0,
|
||||||
|
|
|
||||||
|
|
@ -51,7 +51,7 @@ function SpecialPropBreakdown({ prop, afterFetchData }) {
|
||||||
|
|
||||||
function getExternalLinkUrlFactory() {
|
function getExternalLinkUrlFactory() {
|
||||||
if (prop === 'path') {
|
if (prop === 'path') {
|
||||||
return (listItem) => url.externalLinkForPage(site.domain, listItem.name)
|
return (listItem) => url.externalLinkForPage(site, listItem.name)
|
||||||
} else if (prop === 'search_query') {
|
} else if (prop === 'search_query') {
|
||||||
return null // WP Search Queries should not become external links
|
return null // WP Search Queries should not become external links
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,7 @@ function EntryPages({ afterFetchData }) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function getExternalLinkUrl(page) {
|
function getExternalLinkUrl(page) {
|
||||||
return url.externalLinkForPage(site.domain, page.name)
|
return url.externalLinkForPage(site, page.name)
|
||||||
}
|
}
|
||||||
|
|
||||||
function getFilterInfo(listItem) {
|
function getFilterInfo(listItem) {
|
||||||
|
|
@ -66,7 +66,7 @@ function ExitPages({ afterFetchData }) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function getExternalLinkUrl(page) {
|
function getExternalLinkUrl(page) {
|
||||||
return url.externalLinkForPage(site.domain, page.name)
|
return url.externalLinkForPage(site, page.name)
|
||||||
}
|
}
|
||||||
|
|
||||||
function getFilterInfo(listItem) {
|
function getFilterInfo(listItem) {
|
||||||
|
|
@ -112,7 +112,7 @@ function TopPages({ afterFetchData }) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function getExternalLinkUrl(page) {
|
function getExternalLinkUrl(page) {
|
||||||
return url.externalLinkForPage(site.domain, page.name)
|
return url.externalLinkForPage(site, page.name)
|
||||||
}
|
}
|
||||||
|
|
||||||
function getFilterInfo(listItem) {
|
function getFilterInfo(listItem) {
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
import { apiPath, externalLinkForPage, isValidHttpUrl, trimURL } from './url'
|
import { apiPath, externalLinkForPage, isValidHttpUrl, trimURL } from './url'
|
||||||
|
import { siteContextDefaultValue } from '../site-context'
|
||||||
|
|
||||||
describe('apiPath', () => {
|
describe('apiPath', () => {
|
||||||
it.each([
|
it.each([
|
||||||
|
|
@ -32,10 +33,19 @@ describe('externalLinkForPage', () => {
|
||||||
])(
|
])(
|
||||||
'when domain is %s and page is %s, it should return %s',
|
'when domain is %s and page is %s, it should return %s',
|
||||||
(domain, page, expected) => {
|
(domain, page, expected) => {
|
||||||
const result = externalLinkForPage(domain, page)
|
const site = { ...siteContextDefaultValue, domain: domain }
|
||||||
|
const result = externalLinkForPage(site, page)
|
||||||
expect(result).toBe(expected)
|
expect(result).toBe(expected)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
it('returns null for consolidated view', () => {
|
||||||
|
const consolidatedView = {
|
||||||
|
...siteContextDefaultValue,
|
||||||
|
isConsolidatedView: true
|
||||||
|
}
|
||||||
|
expect(externalLinkForPage(consolidatedView, '/some-page')).toBe(null)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('isValidHttpUrl', () => {
|
describe('isValidHttpUrl', () => {
|
||||||
|
|
|
||||||
|
|
@ -8,11 +8,15 @@ export function apiPath(
|
||||||
}
|
}
|
||||||
|
|
||||||
export function externalLinkForPage(
|
export function externalLinkForPage(
|
||||||
domain: PlausibleSite['domain'],
|
site: PlausibleSite,
|
||||||
page: string
|
page: string
|
||||||
): string | null {
|
): string | null {
|
||||||
|
if (site.isConsolidatedView) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const domainURL = new URL(`https://${domain}`)
|
const domainURL = new URL(`https://${site.domain}`)
|
||||||
return `https://${domainURL.host}${page}`
|
return `https://${domainURL.host}${page}`
|
||||||
} catch (_error) {
|
} catch (_error) {
|
||||||
return null
|
return null
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue