Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Returning null in a GeneratePreviewURL function creates type and runtime errors #2472

Closed
evelynhathaway opened this issue Apr 10, 2023 · 3 comments · Fixed by #2507
Closed
Assignees
Labels
status: needs-triage Possible bug which hasn't been reproduced yet

Comments

@evelynhathaway
Copy link

Link to reproduction

https://payloadcms.com/docs/configuration/collections#preview:~:text=Example%20collection%20with%20preview%20function

To Reproduce

Use the example preview collection function on any collection.

Describe the Bug

TypeScript errors on the return of null.

Type '(document: Partial<CollectionType>, { locale }: GeneratePreviewURLOptions) => string | null' is not assignable to type 'GeneratePreviewURL'.
  Type 'string | null' is not assignable to type 'string | Promise<string>'.
    Type 'null' is not assignable to type 'string | Promise<string>'.

The preview button still appears in the Admin UI, and clicking it opens a not found page. It tries to load the URL null relative to the current page, which redirects to the generic not found page for the collection.

I'd expect that the example function would not have a TypeScript issue and returning null would hide the button or at least let the user know that the preview is not available for the current document and its fields.

Payload Version

1.6.28

@evelynhathaway evelynhathaway added the status: needs-triage Possible bug which hasn't been reproduced yet label Apr 10, 2023
@JessChowdhury
Copy link
Member

Hi @evelynhathaway - I haven't been able to reproduce this on 1.6.32, if you update to this version (payload@latest) do you still see the type error?

Definitely agree with you on the preview button, it would be great to either hide or clearly show it as disabled when you return null - this will likely be a feature request rather than an issue, I'll see what the team thinks here.

@jacobsfletch
Copy link
Member

jacobsfletch commented Apr 17, 2023

@evelynhathaway you were right in that our docs incorrectly indicated that null could be returned from admin.preview—I just opened a PR to support this. Once released, a toast notification is thrown when a null preview URL is generated after clicking that button.

jacobsfletch added a commit that referenced this issue Apr 17, 2023
Copy link
Contributor

github-actions bot commented Sep 7, 2024

This issue has been automatically locked.
Please open a new issue if this issue persists with any additional detail.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 7, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
status: needs-triage Possible bug which hasn't been reproduced yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants