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

Tutorial on block deprecation #71

Closed
mburridge opened this issue Feb 2, 2023 Discussed in #67 · 20 comments
Closed

Tutorial on block deprecation #71

mburridge opened this issue Feb 2, 2023 Discussed in #67 · 20 comments
Assignees

Comments

@mburridge
Copy link

Discussed in #67

Originally posted by mburridge January 26, 2023
If you make a change to the way that a block is rendered you will see the "invalid content" error message after the change.

this-block-contains-unexpected-or-invalid-content

This commonly occurs during development, and developers working with blocks are very aware of this and are used to dealing with it.

However, if you've already published a block and want to make changes to it in an update you won't want users to see the "invalid content" error message, which they will in each instance of the block across their site until recovery is done on each of them.

To circumvent this problem developers can "deprecate" the old version of the block.

A tutorial on how to deprecate a block, and when and why you should do it, would be a nice addition to the Developer Blog.

Reference:
Deprecation

@mburridge
Copy link
Author

mburridge commented Feb 22, 2023

@mburridge
Copy link
Author

mburridge commented Feb 22, 2023

Things to mention

  • Why deprecate? - to provide graceful fallback for earlier versions of the block
  • Primarily for static blocks - not needed for dynamic blocks as they don't have a save() function
  • Benefit is for the content editor - avoids the 'invalid content' error message in the editor
    • front-end displays what's in wp-content for the post regardless
    • front-end only changes when the content is updated when the post is saved with new version of save()
  • Each deprecated version of the block is an object, in the deprecated array, and has the following properties:
    • attributes
    • supports
    • save
    • migrate
    • isEligible
  • Create a deprecated.js file and import it into index.js and use it in the registerBlockType function call
  • Define each deprecated version as a const and reference in the exported array, e.g. [v3, v2, v1]

Simple demonstration

Scaffold a block with @wordpress/create-block
Create 3 posts - add the new block to each (plus some text "Test 1 / 2 /3 ")
Change text in save() and create first deprecation
Test in second post
Change text in save() and create second deprecation (also maybe change <p> to <div> - and view markup)
Test in third post
Test in first and second posts

@ndiego
Copy link
Member

ndiego commented Feb 24, 2023

@mburridge I have a lot of deprecation horror stories, so looking forward to this piece. Will definitely be a very valuable addition to the blog! 💪

@mburridge
Copy link
Author

I have a lot of deprecation horror stories

@ndiego please do share. It might be nice to have a section on pitfalls and gotchas to watch out for.

@ndiego
Copy link
Member

ndiego commented Feb 24, 2023

Will do. I'll put together some examples next week 💪

@carstingaxion
Copy link

I would like to read about some nice ways to deprecate third–party blocks in favor of matured core blocks.

@mburridge
Copy link
Author

mburridge commented Mar 7, 2023

A draft of the post is ready for review. Please test the code examples and that the iterations of the code and the deprecations of previous versions all work. Also any suggestions or corrections that might improve the post will be very welcome. Thanks.

@ndiego
Copy link
Member

ndiego commented Mar 7, 2023

@mburridge I will read through today and add any comments I might have, but one thing that has tripped me up frequently is multiple deprecation issues. If there is more than one, sometimes the error message in the console can lead you in the wrong direction since it will only display the first error it encounters. So maybe a warning that the error message is helpful but only displays one error at a time and will not always be able to diagnose every issue.

@justintadlock
Copy link

This is well-written, as usual, @mburridge. I learned a ton and think this is exactly the kind of content that we need on the Developer Blog. I left a handful of in-doc comments.

I think my biggest note would be to try to break up the text a bit more with sections. I think this is pushing 2,000 words, so having a few headings sprinkled in there will make it easier to digest for readers.

I still need to do a run-through of the code, but the copy is pretty solid at this point.

@mburridge
Copy link
Author

Thanks @justintadlock. I've broken up the wall-of-text with some headings, as per your suggestion. Let me know what you think.

@justintadlock
Copy link

@mburridge - The headings look good to me.

I've finished walking through the code and left my suggestions in the document. Overall, it felt pretty straightforward, and I successfully worked through each item. It was a really nice exercise for me since I haven't tried deprecation before. I'll definitely be more at ease with doing it for future blocks.

In addition to my mention of being precise about src/index.js vs. index.js, you could just mention earlier in the post that the reader will be working in the src folder. Just do it once instead of updating each file reference. I think that'd work better in this case.

@mburridge
Copy link
Author

Glad you found it useful @justintadlock - I guess I must be doing something right! 😅

you could just mention earlier in the post that the reader will be working in the src folder

Great suggestion, thanks - I've done that.

@mburridge
Copy link
Author

Public preview is now available for review.

@mburridge mburridge moved this from In Progress to Needs 2nd review in Developer Blog Content Board Mar 8, 2023
@mburridge mburridge added the flow: needs review reviewer wanted label Mar 8, 2023
@bph
Copy link
Collaborator

bph commented Mar 9, 2023

@mburridge Here is a publish checklist for you :-)

  • Post Title in Sentence case
  • Are Category or Categories selected?
  • Are Tags identifies?
  • Is there an Excerpt?
  • Props added?

@mburridge
Copy link
Author

The only thing outstanding is that I can't find Fabian Kägy's profile page for the props.

@bph
Copy link
Collaborator

bph commented Mar 9, 2023

You would need to do at it manually. The site only pulls from the list of users on this site, not on all of wordpress. It's a known issue, that hasn't been fixed yet.

@mburridge
Copy link
Author

Okay, I found it in the end - he spells his surname differently in his username than in is actual surname 🤷‍♂️

@mburridge
Copy link
Author

mburridge commented Mar 10, 2023

As far as I'm concerned this is ready to go @bph @abhansnuk @marybaum.

@mburridge
Copy link
Author

"Block deprecation – a tutorial" now published.

@github-project-automation github-project-automation bot moved this from Needs 2nd review to Ready to publish in Developer Blog Content Board Mar 10, 2023
@bph bph moved this from Ready to publish to Published (Done) in Developer Blog Content Board Mar 13, 2023
@Bruse-Lee
Copy link

Why not implement a plug-in that traverses all articles containing the Attempt Block Recovery button and simulates users clicking to go to batch Recovery?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Published (Done)
Development

No branches or pull requests

6 participants