Skip to content

Commit

Permalink
fix: prefetch causing stale data (#9020)
Browse files Browse the repository at this point in the history
Potentially fixes #9012 by disabling prefetch for all Next.js `Link`
component usage.

With prefetch left as the default and _on_, there were cases where the
prefetch could fetch stale data for Edit routes. Then, when navigating
to the Edit route, the data could be stale.

In addition, I think there is some strangeness happening on the Next.js
side where prefetched data might still come from the router cache even
though router cache is disabled.

This fix should be done regardless, but I suspect it will solve for a
lot of stale data issues.
  • Loading branch information
jmikrut authored Nov 4, 2024
1 parent 6b9f178 commit 35b107a
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export const DocumentTabLink: React.FC<{
<Link
className={`${baseClass}__link`}
href={!isActive || href !== pathname ? hrefWithLocale : ''}
prefetch={false}
{...(newTab && { rel: 'noopener noreferrer', target: '_blank' })}
tabIndex={isActive ? -1 : 0}
>
Expand Down
1 change: 1 addition & 0 deletions packages/next/src/elements/Nav/index.client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export const DefaultNavClient: React.FC = () => {
href={href}
id={id}
key={i}
prefetch={Link ? false : undefined}
tabIndex={!navOpen ? -1 : undefined}
>
{activeCollection && <div className={`${baseClass}__link-indicator`} />}
Expand Down
2 changes: 2 additions & 0 deletions packages/next/src/views/ForgotPassword/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export const ForgotPasswordView: React.FC<AdminViewProps> = ({ initPageResult })
adminRoute,
path: accountRoute,
})}
prefetch={false}
>
{children}
</Link>
Expand All @@ -68,6 +69,7 @@ export const ForgotPasswordView: React.FC<AdminViewProps> = ({ initPageResult })
adminRoute,
path: loginRoute,
})}
prefetch={false}
>
{i18n.t('authentication:backToLogin')}
</Link>
Expand Down
1 change: 1 addition & 0 deletions packages/next/src/views/Login/LoginForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export const LoginForm: React.FC<{
adminRoute,
path: forgotRoute,
})}
prefetch={false}
>
{t('authentication:forgotPasswordQuestion')}
</Link>
Expand Down
2 changes: 2 additions & 0 deletions packages/next/src/views/ResetPassword/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export const ResetPassword: React.FC<AdminViewProps> = ({ initPageResult, params
adminRoute,
path: accountRoute,
})}
prefetch={false}
>
{children}
</Link>
Expand Down Expand Up @@ -75,6 +76,7 @@ export const ResetPassword: React.FC<AdminViewProps> = ({ initPageResult, params
adminRoute,
path: loginRoute,
})}
prefetch={false}
>
{i18n.t('authentication:backToLogin')}
</Link>
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/views/Versions/cells/CreatedAt/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const CreatedAtCell: React.FC<CreatedAtCellProps> = ({
}

return (
<Link href={to}>
<Link href={to} prefetch={false}>
{cellData &&
formatDate({ date: cellData as Date | number | string, i18n, pattern: dateFormat })}
</Link>
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/elements/AppHeader/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export const AppHeader: React.FC = () => {
aria-label={t('authentication:account')}
className={`${baseClass}__account`}
href={formatAdminURL({ adminRoute, path: accountRoute })}
prefetch={Link ? false : undefined}
tabIndex={0}
>
<Account />
Expand Down
5 changes: 4 additions & 1 deletion packages/ui/src/elements/Button/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ export const Button = forwardRef<HTMLAnchorElement | HTMLButtonElement, Props>((
}

let buttonElement
let prefetch

switch (el) {
case 'anchor':
Expand All @@ -147,10 +148,12 @@ export const Button = forwardRef<HTMLAnchorElement | HTMLButtonElement, Props>((

if (disabled) {
LinkTag = 'div'
} else {
prefetch = false
}

buttonElement = (
<LinkTag {...buttonProps} href={to || url} to={to || url}>
<LinkTag {...buttonProps} href={to || url} prefetch={prefetch} to={to || url}>
<ButtonContents icon={icon} showTooltip={showTooltip} tooltip={tooltip}>
{children}
</ButtonContents>
Expand Down
11 changes: 8 additions & 3 deletions packages/ui/src/elements/Logout/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,21 @@ const DefaultLogout: React.FC<{
const basePath = process.env.NEXT_BASE_PATH ?? ''
const LinkElement = Link || 'a'

const props = {
'aria-label': t('authentication:logOut'),
className: `${baseClass}__log-out`,
prefetch: Link ? false : undefined,
tabIndex,
}

return (
<LinkElement
aria-label={t('authentication:logOut')}
className={`${baseClass}__log-out`}
{...props}
href={formatAdminURL({
adminRoute,
basePath,
path: logoutRoute,
})}
tabIndex={tabIndex}
>
<LogOutIcon />
</LinkElement>
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/elements/Popup/PopupButtonList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export const Button: React.FC<MenuButtonProps> = ({
onClick(e)
}
}}
prefetch={false}
>
{children}
</Link>
Expand Down
13 changes: 11 additions & 2 deletions packages/ui/src/elements/StepNav/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,20 @@ const StepNav: React.FC<{

const LinkElement = Link || 'a'

const baseLinkProps = {
prefetch: Link ? false : undefined,
}

return (
<Fragment>
{stepNav.length > 0 ? (
<nav className={[baseClass, className].filter(Boolean).join(' ')}>
<LinkElement className={`${baseClass}__home`} href={admin} tabIndex={0}>
<LinkElement
className={`${baseClass}__home`}
href={admin}
tabIndex={0}
{...baseLinkProps}
>
<span title={t('general:dashboard')}>
<RenderComponent
Component={PayloadIcon}
Expand All @@ -58,7 +67,7 @@ const StepNav: React.FC<{
) : (
<Fragment key={i}>
{item.url ? (
<LinkElement href={item.url}>
<LinkElement href={item.url} {...baseLinkProps}>
<span key={i}>{StepLabel}</span>
</LinkElement>
) : (
Expand Down
5 changes: 3 additions & 2 deletions packages/ui/src/elements/Table/DefaultCell/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,22 @@ export const DefaultCell: React.FC<CellComponentProps> = (props) => {
classNameFromConfigContext

const onClick = onClickFromProps || onClickFromContext
const isLink = link !== undefined ? link : columnIndex === 0

let WrapElement: React.ComponentType<any> | string = 'span'

const wrapElementProps: {
className?: string
href?: string
onClick?: () => void
prefetch?: false
type?: 'button'
} = {
className,
}

const isLink = link !== undefined ? link : columnIndex === 0

if (isLink) {
wrapElementProps.prefetch = false
WrapElement = Link
wrapElementProps.href = customCellContext?.collectionSlug
? formatAdminURL({
Expand Down

0 comments on commit 35b107a

Please sign in to comment.