Fix: Time to Read block showing "this block has encountered an error" - #61459
What?
- Issue - https://github.com/WordPress/gutenberg/issues/61459
Why?
- This PR passes empty text to
wordCountfunction if we get the content as undefined.
How?
- This PR updates the time to read block, so that the
contentshould not be passed as undefined to the function.
Testing Instructions
- Open site editor.
- Go to single post template.
- Add the
post-time-to-readblock. - Now error is not visible and block is working as expected.
Testing Instructions for Keyboard
- NIL
Screenshots or screencast
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 <[email protected]>
Co-authored-by: t-hamano <[email protected]>
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.
Thanks for the PR!
This PR makes changes to the utility functions in the
wordcountpackage, 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
contentbecomesundefinedhere:https://github.com/WordPress/gutenberg/blob/57741ee5e4d434c9fa6988b9f5b5966ef2768b8b/packages/block-library/src/post-time-to-read/edit.js#L69
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.
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.
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 🙏.