|
| 1 | +--- |
| 2 | +slug: cheerio-1.0 |
| 3 | +title: Cheerio 1.0 Released, Batteries Included 🔋 |
| 4 | +authors: fb55 |
| 5 | +tags: [release, announcement] |
| 6 | +--- |
| 7 | + |
| 8 | +# Cheerio 1.0 Released, Batteries Included 🔋 |
| 9 | + |
| 10 | +Cheerio 1.0 is out! After 12 release candidates and just a short seven years |
| 11 | +after the initial 1.0 release candidate, it is finally time to call Cheerio 1.0 |
| 12 | +complete. The theme for this release is "batteries included", with common use |
| 13 | +cases now supported out of the box. |
| 14 | + |
| 15 | +So grab a pair of double-As, and read below for what's new, what's changed, and |
| 16 | +how to upgrade! |
| 17 | + |
| 18 | +<!--truncate--> |
| 19 | + |
| 20 | +## New Website and Documentation |
| 21 | + |
| 22 | +Since the last release, we've published a new website and documentation for |
| 23 | +Cheerio. The new site features detailed guides and API documentation to get the |
| 24 | +most from Cheerio. Check it out at [cheerio.js.org](https://cheerio.js.org/). |
| 25 | + |
| 26 | +## A new way to load documents |
| 27 | + |
| 28 | +Loading documents into Cheerio has been revamped. Cheerio now supports multiple |
| 29 | +loading methods, each tailored to different use cases: |
| 30 | + |
| 31 | +- `load`: The classic method for parsing HTML or XML strings. |
| 32 | +- `loadBuffer`: Works with binary data, automatically detecting the document |
| 33 | + encoding. |
| 34 | +- `stringStream` and `decodeStream`: Parse HTML directly from streams. |
| 35 | +- `fromURL`: Fetch and parse HTML from a URL in one go. |
| 36 | + |
| 37 | +Dive deeper into these methods in the |
| 38 | +[Loading Documents](http:///docs/basics/loading) tutorial. |
| 39 | + |
| 40 | +## Simplified Data Extraction |
| 41 | + |
| 42 | +The new `extract` method allows you to extract data from an HTML document and |
| 43 | +store it in an object. To fetch the latest release of Cheerio from GitHub and |
| 44 | +extract the release date and the release notes from the release page is now as |
| 45 | +simple as: |
| 46 | + |
| 47 | +```ts |
| 48 | +import * as cheerio from 'cheerio'; |
| 49 | + |
| 50 | +const $ = await cheerio.fromURL( |
| 51 | + 'https://github.com/cheeriojs/cheerio/releases', |
| 52 | +); |
| 53 | +const data = $.extract({ |
| 54 | + releases: [ |
| 55 | + { |
| 56 | + // First, we select individual release sections. |
| 57 | + selector: 'section', |
| 58 | + // Then, we extract the release date, name, and notes from each section. |
| 59 | + value: { |
| 60 | + // Selectors are executed within the context of the selected element. |
| 61 | + name: 'h2', |
| 62 | + date: { |
| 63 | + selector: 'relative-time', |
| 64 | + // The actual release date is stored in the `datetime` attribute. |
| 65 | + value: 'datetime', |
| 66 | + }, |
| 67 | + notes: { |
| 68 | + selector: '.markdown-body', |
| 69 | + // We are looking for the HTML content of the element. |
| 70 | + value: 'innerHTML', |
| 71 | + }, |
| 72 | + }, |
| 73 | + }, |
| 74 | + ], |
| 75 | +}); |
| 76 | +``` |
| 77 | + |
| 78 | +Read more about all of the available options in the |
| 79 | +[Extracting Data](/docs/advanced/extract) guide. |
| 80 | + |
| 81 | +## Breaking Changes and Upgrade Guide |
| 82 | + |
| 83 | +Cheerio 1.0 introduces several breaking changes, most notably: |
| 84 | + |
| 85 | +- The minimum NodeJS version is now 18.17 or higher. |
| 86 | +- Import paths were simplified. For example, use `cheerio/slim` instead of |
| 87 | + `cheerio/lib/slim`. |
| 88 | +- The deprecated default Cheerio instance and static methods were removed. |
| 89 | + |
| 90 | + Before, it was possible to write code like this: |
| 91 | + |
| 92 | + ```ts |
| 93 | + import cheerio, { html } from 'cheerio'; |
| 94 | + |
| 95 | + html(cheerio('<test></test>')); // ~ '<test></test>' -- NO LONGER WORKS |
| 96 | + ``` |
| 97 | + |
| 98 | + Make sure to always load documents first: |
| 99 | + |
| 100 | + ```ts |
| 101 | + import * as cheerio from 'cheerio'; |
| 102 | + |
| 103 | + cheerio.load('<test></test>').html(); |
| 104 | + ``` |
| 105 | + |
| 106 | +- htmlparser2 options now reside exclusively under the `xml` key: |
| 107 | + |
| 108 | + ```ts |
| 109 | + const $ = cheerio.load('<html>', { |
| 110 | + xml: { |
| 111 | + withStartIndices: true, |
| 112 | + }, |
| 113 | + }); |
| 114 | + ``` |
| 115 | + |
| 116 | +- Node types previously re-exported by Cheerio must now be imported directly |
| 117 | + from [`domhandler`](https://github.com/fb55/domhandler). |
| 118 | + |
| 119 | +For a comprehensive list of changes, please consult |
| 120 | +[the changelog](https://github.com/cheeriojs/cheerio/releases). |
| 121 | + |
| 122 | +## Upgrading to Cheerio 1.0 |
| 123 | + |
| 124 | +To upgrade to Cheerio 1.0, just run: |
| 125 | + |
| 126 | +```bash npm2yarn |
| 127 | +npm install cheerio@latest |
| 128 | +``` |
| 129 | + |
| 130 | +## Get Involved |
| 131 | + |
| 132 | +Explore the new features and let us know what you think! Encounter an issue? |
| 133 | +Report it on our |
| 134 | +[GitHub issue tracker](https://github.com/cheeriojs/cheerio/issues). Have an |
| 135 | +idea for an improvement? Pull requests welcome :) |
| 136 | + |
| 137 | +## Thank You |
| 138 | + |
| 139 | +Thanks to [@jugglinmike](https://github.com/jugglinmike) for kick-starting |
| 140 | +Cheerio 1.0, and to all the contributors who have helped shape this release. We |
| 141 | +couldn't have done it without you. |
| 142 | + |
| 143 | +Thanks to our |
| 144 | +[sponsors and backers](https://github.com/cheeriojs/cheerio?sponsor) for |
| 145 | +supporting Cheerio's development. If you use Cheerio at work, consider asking |
| 146 | +your company to support us! |
| 147 | + |
| 148 | +And finally, thank you for using Cheerio 🙇🙇♀️ |
0 commit comments