Skip to content

Commit 0f85a6e

Browse files
fix(plugin-search): generates full docURL with basePath from next config (#10910)
Fixes #10878. The Search Plugin displays a link within the search results collection that points to the underlying document that is related to that result. The href used, however, was not accounting for any `basePath` provided to the `next.config.js`, leading to a 404 if using a custom base path. The fix is to use the `Link` component from `next/link` instead of an anchor tag directly. This will automatically inject the the base path into the href before rendering it. This PR also brings back the `CopyToClipboard` component. This makes it easy for the user to copy the href instead of navigating to it. --------- Co-authored-by: Jacob Fletcher <jacobsfletch@gmail.com>
1 parent 1771271 commit 0f85a6e

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

packages/plugin-search/src/Search/ui/LinkToDoc/index.client.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
'use client'
22

3-
import { useConfig, useField } from '@payloadcms/ui'
3+
import { CopyToClipboard, useConfig, useField } from '@payloadcms/ui'
44
import { formatAdminURL } from '@payloadcms/ui/shared'
5+
import LinkImport from 'next/link.js'
56
import React from 'react'
6-
// TODO: fix this import to work in dev mode within the monorepo in a way that is backwards compatible with 1.x
7-
// import CopyToClipboard from 'payload/dist/admin/components/elements/CopyToClipboard'
7+
8+
const Link = (LinkImport.default || LinkImport) as unknown as typeof LinkImport.default
89

910
export const LinkToDocClient: React.FC = () => {
1011
const { config } = useConfig()
@@ -27,6 +28,8 @@ export const LinkToDocClient: React.FC = () => {
2728
path: `/collections/${value.relationTo || ''}/${value.value || ''}`,
2829
})}`
2930

31+
const hrefToDisplay = `${process.env.NEXT_BASE_PATH || ''}${href}`
32+
3033
return (
3134
<div style={{ marginBottom: 'var(--spacing-field, 1rem)' }}>
3235
<div>
@@ -38,7 +41,7 @@ export const LinkToDocClient: React.FC = () => {
3841
>
3942
Doc URL
4043
</span>
41-
{/* <CopyToClipboard value={href} /> */}
44+
<CopyToClipboard value={hrefToDisplay} />
4245
</div>
4346
<div
4447
style={{
@@ -47,9 +50,9 @@ export const LinkToDocClient: React.FC = () => {
4750
textOverflow: 'ellipsis',
4851
}}
4952
>
50-
<a href={href} target="_blank">
51-
{href}
52-
</a>
53+
<Link href={href} passHref {...{ rel: 'noopener noreferrer', target: '_blank' }}>
54+
{hrefToDisplay}
55+
</Link>
5356
</div>
5457
</div>
5558
)

tsconfig.base.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
}
3232
],
3333
"paths": {
34-
"@payload-config": ["./test/fields/config.ts"],
34+
"@payload-config": ["./test/plugin-search/config.ts"],
3535
"@payloadcms/live-preview": ["./packages/live-preview/src"],
3636
"@payloadcms/live-preview-react": ["./packages/live-preview-react/src/index.ts"],
3737
"@payloadcms/live-preview-vue": ["./packages/live-preview-vue/src/index.ts"],

0 commit comments

Comments
 (0)