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

Fix: Time to Read block showing "this block has encountered an error" - #61459 #61614

Merged
merged 3 commits into from
May 14, 2024

Conversation

hbhalodia
Copy link
Contributor

@hbhalodia hbhalodia commented May 13, 2024

What?

Why?

  • This PR passes empty text to wordCount function if we get the content as undefined.

How?

  • This PR updates the time to read block, so that the content should not be passed as undefined to the function.

Testing Instructions

  1. Open site editor.
  2. Go to single post template.
  3. Add the post-time-to-read block.
  4. Now error is not visible and block is working as expected.

Testing Instructions for Keyboard

  • NIL

Screenshots or screencast

GB.Issue.-.61459.webm

…h optional chaining so undefined can be prevented
Copy link

github-actions bot commented May 13, 2024

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: @bradhogan.

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: bradhogan.

Co-authored-by: hbhalodia <hbhalodia@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.

@t-hamano t-hamano self-requested a review May 13, 2024 15:41
@t-hamano t-hamano added [Type] Bug An existing feature does not function as intended [Block] Time to Read Affects the Time to Read Block labels May 13, 2024
@t-hamano t-hamano linked an issue May 13, 2024 that may be closed by this pull request
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!

This PR makes changes to the utility functions in the wordcount package, but as the JSDoc shows, these utility functions are not expected to be passed undefined text, so unexpected problems may occur.

I think this problem needs to be solved on the block side. I guess the fundamental problem is that content becomes undefined here:

wordCount( content, wordCountType ) / AVERAGE_READING_RATE

I haven't tested it, but could it be resolved by passing an empty string as a fallback as shown below?

diff --git a/packages/block-library/src/post-time-to-read/edit.js b/packages/block-library/src/post-time-to-read/edit.js
index 5cdb81c05e..abfdce6630 100644
--- a/packages/block-library/src/post-time-to-read/edit.js
+++ b/packages/block-library/src/post-time-to-read/edit.js
@@ -66,7 +66,7 @@ function PostTimeToReadEdit( { attributes, setAttributes, context } ) {
                const minutesToRead = Math.max(
                        1,
                        Math.round(
-                               wordCount( content, wordCountType ) / AVERAGE_READING_RATE
+                               wordCount( content || '', wordCountType ) / AVERAGE_READING_RATE
                        )
                );

@hbhalodia
Copy link
Contributor Author

Thanks for the PR!

This PR makes changes to the utility functions in the wordcount package, but as the JSDoc shows, these utility functions are not expected to be passed undefined text, so unexpected problems may occur.

I think this problem needs to be solved on the block side. I guess the fundamental problem is that content becomes undefined here:

wordCount( content, wordCountType ) / AVERAGE_READING_RATE

I haven't tested it, but could it be resolved by passing an empty string as a fallback as shown below?

diff --git a/packages/block-library/src/post-time-to-read/edit.js b/packages/block-library/src/post-time-to-read/edit.js
index 5cdb81c05e..abfdce6630 100644
--- a/packages/block-library/src/post-time-to-read/edit.js
+++ b/packages/block-library/src/post-time-to-read/edit.js
@@ -66,7 +66,7 @@ function PostTimeToReadEdit( { attributes, setAttributes, context } ) {
                const minutesToRead = Math.max(
                        1,
                        Math.round(
-                               wordCount( content, wordCountType ) / AVERAGE_READING_RATE
+                               wordCount( content || '', wordCountType ) / AVERAGE_READING_RATE
                        )
                );

Thanks @t-hamano, I have'nt looked at the JS Doc comment, hence routed that approach. Would look at the patch above and would make the changes to PR asap.

@hbhalodia hbhalodia requested a review from ajitbohra as a code owner May 13, 2024 16:56
@hbhalodia
Copy link
Contributor Author

Hello @t-hamano, have tested the changes on the local and yes, the content is undefined at that point which results in the error. Have updated the PR with description and required changes.

Thank You.

@hbhalodia
Copy link
Contributor Author

Hi @t-hamano, Just a quick question, this function may be used else whereas well, so we need to update this type of issue at other places too (if present). I am curious can't we just update all the functions with the optional chaining, so that we do not change on every place, instead we just change the function itself?

Thank You 🙏.

@hbhalodia hbhalodia requested a review from t-hamano May 13, 2024 17:00
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!

I am curious can't we just update all the functions with the optional chaining, so that we do not change on every place, instead we just change the function itself?

I think the logic in the wordcount package's utility functions is based on the assumption that some string exists. Therefore, if we allow undefined value, I think we will get a result that is not calculated correctly.

It may be good idea to implement the safeguards you suggest in the future, but for now I think it's enough to just fix the block side.

@t-hamano t-hamano merged commit f5b456a into WordPress:trunk May 14, 2024
63 checks passed
@github-actions github-actions bot added this to the Gutenberg 18.4 milestone May 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Time to Read Affects the Time to Read Block [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Time to Read block showing "this block has encountered an error"
2 participants