Skip to content

Conversation

BugReportOnWeb
Copy link
Contributor

What?

Closes #70908

This PR fixes the issue in the File block where PDF files with query parameters are not recognised as PDFs, resulting in the embedded preview not rendering.

Why?

Some media storage plugins or CDN setups generate signed PDF URLs with query parameters for access control. These still point to valid PDFs, but the current logic fails to detect them as such, disabling the embedded preview.

How?

Updated the PDF detection logic to use URL API. This safely extracts the pathname from the media URL, ignoring query parameters and hash fragments.

Testing Instructions

  1. Follow the instructions from the original issue to set up and offload media to cloud storage.
  2. Use a File block in the block editor and select an offloaded PDF file
  3. Confirm that the embedded PDF preview appears correctly, even if the media URL has query parameters.

Manual Testing with Code Injection

If not able to set up cloud storage, test manually with:

  1. Go to the PDF check in FileEdit component
  2. Modify the code just before the PDF check:
newMedia.url = newMedia.url + '?foo=bar';
  1. Open the editor, insert a File block, and select any locally uploaded PDF.
  2. Confirm that:
    • The PDF source includes the query params.
    • The preview still renders correctly.

Screenshots or screencast

Before After
Screenshot 2025-07-25 at 10 47 33 PM Screenshot 2025-07-25 at 11 36 05 PM

@BugReportOnWeb BugReportOnWeb marked this pull request as ready for review July 25, 2025 18:46
Copy link

github-actions bot commented Jul 25, 2025

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @MichaelCastelbuono.

Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Unlinked contributors: MichaelCastelbuono.

Co-authored-by: BugReportOnWeb <devasheeshkaul@git.wordpress.org>
Co-authored-by: t-hamano <wildworks@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Copy link
Contributor

@t-hamano t-hamano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

Did you try to use the getFilename utility?

import { getFilename } from '@wordpress/url';
const isPdf = getFilename( newMedia.url )?.toLowerCase().endsWith( '.pdf' );

@t-hamano t-hamano added [Type] Bug An existing feature does not function as intended [Block] File Affects the File Block labels Jul 26, 2025
@BugReportOnWeb
Copy link
Contributor Author

Thank you for reviewing! I hadn’t tried it, but this does look cleaner and more robust. I’ll try it out and update with the change.

Copy link
Contributor

@t-hamano t-hamano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@t-hamano t-hamano merged commit fb5e28a into WordPress:trunk Jul 26, 2025
60 checks passed
@github-actions github-actions bot added this to the Gutenberg 21.4 milestone Jul 26, 2025
adamsilverstein pushed a commit to adamsilverstein/gutenberg that referenced this pull request Jul 31, 2025
…Press#70915)

* fix: detect PDF urls with query strings in file block

* fix: use `getFilename` to fetch filename from media URL

Unlinked contributors: MichaelCastelbuono.

Co-authored-by: BugReportOnWeb <devasheeshkaul@git.wordpress.org>
Co-authored-by: t-hamano <wildworks@git.wordpress.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] File Affects the File Block [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

File Block: Cannot Embed PDFs with Query Params
2 participants