Etsy Strikes from React to Preact

30
Etsy Strikes from React to Preact

Black Trans Lives Topic. Ahead of you begin right here, abet in suggestions setting up a month-to-month repeating donation to a non-profit akin to LGBTQ Freedom Fund.

This submit relies on technical documentation I build together as phase of labor to study and summarize the execs/cons of upgrading Etsy’s default React model from v15.6.2 to both Preact v10.X or React v16.13 (the most neatly-liked versions of every at time of writing). Whereas there are hundreds of adaptations on “ought to I employ Preact or React”, right here’s ours. There are hundreds of clarification why a staff would procure one library over every other, so I won’t faux that this covers the corpulent living of them — advantageous the ones which felt extraordinarily connected to making a resolution which used to be advantageous for Etsy. One most important piece of context is that Etsy currently has two main product stacks. For buyer-going via pages, we employ PHP server-based rendering with jQuery/vanilla JS on the customer to stitch issues together. For our vendor-going via pages and hundreds of our internal instruments, we employ React-rendered SPAs with minimal server-based HTML rendering, receiving records from the identical PHP server-aspect stack.

Since the submit ends up being quite long and pretty in-depth, I had mercy on my fellow developers and presented the abstract first. Not all people wants or wants to tear deep on figuring these items out, and one amongst the benefits of working at an organization that is Etsy’s dimension is that we will manage to pay for to relish a smaller assortment of other folks who specialise in several elements of our stack. I, particularly, employ hundreds of time working on the low-level bits that invent lifestyles more uncomplicated for the remaining of Etsy’s developers to achieve product work and ship aspects/fixes/lawful code.

This doc used to be shared internally with a survey in yelp to invent dawdle that I used to be not dictating the resolution for a gigantic assortment of engineers. The results were, happily, that Etsy will seemingly be upgrading from React v15.6.2 to Preact v10.X. Now, ample with the extra commentary — right here is the doc.

Summary of Findings

In my seek, Etsy ought emigrate from React v15.6.2 to Preact v10.X (most neatly-liked).

Preact is an alternative implementation of the React API. Migrating from React to Preact is a gorgeous straightforward job — this would not entail a gigantic rewrite or most important work from any different groups.

Right here is because of the:

  • The FES staff has already moved forward with a resolution to investigate Preact-based style within the customer-aspect of Etsy’s codebase with SSR. Selecting React over Preact for vendor instruments would expand the fragmentation of our style environment and invent our lives more sturdy.
  • The Preact v10.4.2 bundle is 6KB gzipped, vs 38.5KB gzipped for React v16.13.1.
  • The migration to Preact v10.4.2 would be seriously more uncomplicated and require far fewer steps to total, attributable to Preact’s emphasis on compatibility with each React v15 and React v16.
  • There attain not appear to be any main barriers from a developer tooling standpoint to adopting Preact.

That acknowledged, the aim of this document is to offer all people the records they need to originate their personal knowing, in order that we will all invent a resolution together.

Introduction

Orange doctors at Etsy appear to be challenge descriptions, but this document as an alternative represents compiled study, presented in this kind of technique that we as a developer neighborhood at Etsy can invent a resolution on which plot we would raise to tear when upgrading our model of React. Upgrading React from v15.6.2 brings most important performance and bundle dimension enhancements whereas also providing hundreds of contemporary aspects. Migrating from React v15 to React v16 is a first-rate endeavor. There are a assortment of backwards incompatible adjustments which must happen internal the codebase in yelp to enable these enhancements.

Whereas it would possibly perhaps perhaps perhaps be in actuality laborious to describe right here in element the total new aspects, the code structure enhancements enabled by the React v16.8 Hooks performance are so most important that this would possibly increasingly perhaps well in the end change into more sturdy and more sturdy to recruit developers in working in a pre-Hooks codebase. In yelp to retain with the remaining of the exchange and provide our contemporary developers with the most attention-grabbing tooling accessible, I imagine it’s going to be a excessive precedence to enable employ of Hooks and different neatly-liked React performance. This positively isn’t wherever shut to the acceptable new operate value upgrading for, but it does signify an especially reliable shift in how developers will be capable of put in writing React code.

On the identical time, there is a rising recognition internal the online style neighborhood that the reliable JS bundle dimension of React’s libraries negatively impacts users, for all that it would possibly perhaps perhaps invent code seriously more uncomplicated to perceive and protect over time. The FES staff has already adopted Preact, a limited drop-in alternative to React which is API like minded and would not involve a first-rate rewrite to adopt in our vendor instruments. They relish got sing documentation supposed to succor with the (somewhat few) variations between Preact and React. It can perhaps perhaps be learned right here.

Bundle dimension enhancements

Impart that these numbers exclude the dimensions of capabilities which would possibly perhaps well well be incorporated by all three libraries — issues like prop-varieties or get dangle of-react-class

Sizes are all specified as minified / minified+gzipped and attain from Bundlephobia

React v15.6.2:

  • react: 21.4KB / 7.1KB
  • react-dom: 121.4KB / 36.6KB
  • total: 142.8KB / 43.7KB

React v16.13.1:

  • react: 6.3KB / 2.6KB
  • react-dom: 114.6KB / 35.9KB
  • total: 120.9KB / 38.5KB

Preact v10.4.5

  • preact: 10.1KB / 4KB
  • preact/compat: unknown / 2KB (the minified dimension of preact/compat is not accessible wherever I’m in a position to acquire)
  • total: ? / 6KB

To summarize:

  • Updating from v15.6.2 to v16.13.1 would build us 5.2KB in gzipped dimension
  • Updating from v15.6.2 to Preact v10.4.5 would build us 37.KB in gzipped dimension
  • The usage of Preact v10.4.5 over React v16.13.1 would build us 32.5KB in gzipped dimension

Preact

Because React is the “usual” instrument and Preact is accountable for affirming its personal compatibility, lots of the discussion around Preact entails compatibility with present libraries and code. Typically hundreds of debate of “Preact compatibility” is rather complex. There are two styles of Preact style — one wherein the React compatibility layer is not added, and one wherein it’s far.

Preact v10 merged the compatibility layer into the main Preact stack, and hundreds of React-based libraries seem to relish gained most important enhancements in compatibility because of the of this.

Compatibility with Recent Code

Because Preact emphasizes compatibility with each the v15 and v16 React APIs, it would not seem that we would possibly must attain most important work to get dangle of this upgrade. As with React, we would favor to total the migrations from React.createClass to the get dangle of-react-class kit and React.PropTypes to the prop-varieties kit. Once that is total, nevertheless, it looks that we won’t must get dangle of most important adjustments to the codebase emigrate to Preact. We would are attempting to invent these adjustments in the end, but being in a position to invent them slowly and in a controlled style would invent the replace seriously safer.

The FES staff’s new challenge structure is already based on Preact, which introduces a first-rate compatibility procure by selecting Preact — making sure that we employ most attention-grabbing one Preact / React library for all of Etsy would deal cut again developer issue over time. Among different issues, having to toughen / test in React and Preact for instruments like the Web Toolkit would add hundreds of overhead for that staff and others working on shared tooling and structure. This would possibly perhaps be accomplished without if Preact were the neatly-liked across all of Etsy.

Compatibility with Recent Libraries

Redux v3.4.0 (perhaps no adjustments required)

No convey dependence on React or Preact. If we assemble not must upgrade our different libraries, we won’t must upgrade Redux (but we are going to must if we upgrade React-Redux)

React-Redux v4.4.5 (perhaps like minded as-is)

As of Preact v10, employ of preact-redux has been deprecated in prefer of corpulent toughen for React-Redux. Our model of React-Redux is moderately ragged, so it’s unclear whether or not we would favor to upgrade this library in yelp to enable that corpulent toughen.

React Router v2.3.0 (perhaps like minded as-is)

Preact documentation suggests corpulent toughen by react-router of Preact with the compatibility layer incorporated. That acknowledged, it’s far unclear whether or not Preact as-is would be like minded with our contemporary model of React Router, or whether or not we would must total a model replace to handle this. We can upgrade to as a minimum React-Router v5.2.0 without upgrading React, so this would possibly increasingly perhaps well well be accomplished as an autonomous put together of labor.

React Router Redux v4.0.5 (perhaps like minded as-is)

Use of this library has been deprecated and this would possibly increasingly perhaps well not work with neatly-liked versions of React Router. If now we must upgrade React-Router, we will must procure away this library. Unclear whether or not it would possibly perhaps perhaps perhaps be like minded with Preact as-is, but it looks seemingly that it would possibly perhaps perhaps perhaps be like minded.

It’s conceivable that we will replace this library with connected-react-router, which is one rapid alternative. Of value: connected-react-router v5.0.1 has a bundle dimension of 27.4KB / 4.6KB gzipped, whereas react-router-redux used to be 4.1KB / 1.4KB. It also depends on react-router as a minimum v4.3 and react-router v4.3. connected-react-router v6.8.0 has a bundle dimension of 9.5KB / 3.2KB, which is a principal better number. Sadly, connected-react-router v6+ requires React v16.4.0 and React Redux v6 or v7. To summarize, adopting this library would require a first-rate (although rather brief) extra bundle dimension hit and add extra complexity to the upgrade job. Additionally, connected-react-router looks to relish a laborious dependency on immutable, which would possibly perhaps well well within the long-term quit us from migrating far off from immutable.

Final Impart on Compatibility

As identified by the FES staff, Preact brings with it an extra overhead — every library we employ will need some extra making an are attempting out to substantiate compatibility. That acknowledged, Preact’s emphasis on compatibility ought to confidently cut again concerns right here.

Compatibility with Recent Tooling

Myth/NPM versioning

Because we would be the usage of Preact as an alternative of React, we would doubtlessly trigger warnings to be printed out by Myth for the length of installs if we exclusively eliminated React from our kit.json (unknown). This ought to not be a appropriate blocker, but semver compatibility is a in actuality edifying instrument for maintainers and it would possibly perhaps perhaps perhaps be lawful to relish ours working.

We would doubtlessly are attempting to relish each React and Preact listed in our kit.json file to aloof these points (unknown)

Jest / Enzyme

An adapter is accessible for Enzyme.

There are some limited variations, but attributable to our emphasis on mount-based making an are attempting out we ought to manual clear of lots of the complexities. There would possibly perhaps perhaps be some points with .simulate tests in Enzyme — because of the Preact dispatches valid DOM events, the making an are attempting out ground would possibly perhaps well even yelp points around effervescent which weren’t show conceal in React tests (but would in belief be show conceal in dwell code).

Jest / Unitcards

Unclear, extra making an are attempting out required. In belief, we would replace the jest-unitcard runner to put into effect the preact alias and verify that every little thing passes.

DevTools

Preact has its personal browser extension (which will not be the React one), but presents identical debugging performance and performance monitoring. It can perhaps perhaps be learned right here.

Compatibility with Future Plans

Would adopting Preact impact any future style plans we’ve mentioned for next 1/2/5 years?

Typescript toughen

No sing concerns learned, looks to relish identical compatibility to React’s toughen of Typescript.

Suspense?

Suspense is peaceable experimental for React, and reputedly extra experimental for Preact.

Accurate SSR Suspense toughen would doubtlessly involve pleasurable most important rewrites to a huge quantity of our codebase, attributable to the adjustments within the manner that elements work. Whereas it would possibly perhaps perhaps perhaps be conceivable to adopt Suspense on new style and within the toolkit, it be seemingly not feasible to adopt it in present code with out a reliable-scale effort.

GraphQL

@apollo/client looks to toughen Preact, but nothing sing is listed. urql explicitly helps Preact. As appropriate clientside GraphQL toughen is not accessible, it be not but conceivable to yelp definitively that we would possibly relish corpulent toughen. That acknowledged, having identified two libraries which would possibly perhaps well well provide what we would favor (looking on what route we in the end procure in phrases of the usage of GraphQL on the customer) looks ample to yelp that right here’s not a blocker.

Migrating to React within the long bustle

One arena with selecting a library is whether or not we will depend on it to remain supported. React is actively supported by Facebook, and that looks unlikely to exchange within the subsequent few years. Preact is actively supported by donations and maintained by a passionate community of developers, but if we procure Preact we also will relish to be bright emigrate from Preact to React if the library stops receiving new style or if we obtain a showstopping operate which requires React to put into effect.

  • Preact is API like minded with React, which manner that we’re not making any adjustments by the usage of Preact which would possibly perhaps well well be incompatible with migrating to React at a later date.
  • However, because of the of Preact’s emphasis on compatibility with React v15 as properly as React v16, there are a assortment of code updates we won’t relish to invent if we tear to Preact. We ought to achieve them anyways, however the formula will seemingly be slow and require refactoring an very capable quantity of code in rather tough areas.
  • If we total these refactors whereas on Preact and tear our codebase to an exclusively React v16-like minded employ of the APIs, then transferring to React v16 from Preact will seemingly be very straightforward — one or two computerized code adjustments which would possibly perhaps well well be accomplished via codemod, but in every other case minimal work.
  • One usual thought for essentially completing the React v16 upgrade in a technique that wasn’t so complex used to be emigrate your complete codebase to Preact, replace the total APIs in situation, and then tear to React. That ended up being hundreds of needless churn with low-level libraries, so we moved far off from that belief.

Migration Design

This work is currently blocked by the elimination of React.PropTypes and React.createClass, each of that are blocked by the Web Platform staff’s ESM syntax upgrade.

Impart that nearly all of this work is complex and even a single line / upgrade would possibly perhaps well even involve most important quantities of time. Among different issues, the react-router breaking adjustments will require some invasive adjustments to the seller instruments subapp structure to remodel how issues are loaded / routed. Placing off react-router-redux will also involve hundreds of labor, although every piece ought to confidently be somewhat autonomous.

Assuming that every person amongst our library compatibilities are as acknowledged above and Preact compatibility works as expected, our course to migration would be:

  1. Upgrade to Preact v10.4.5 within the serve of a switch, in order that we will switch between Preact and React powered rendering in yelp to validate
  2. Delivery making employ of codemods emigrate to “neatly-liked” React lifecycle recommendations (not a laborious requirement, attributable to corpulent toughen of every versions’ API in Preact, but a lawful long-term purpose)
  3. Fully procure away react-router-redux (would possibly perhaps perhaps be accomplished in parallel with the Preact migration)
  4. Upgrade react-redux to 7.2.0 (seemingly entails a redux upgrade as properly)
  5. Upgrade react-router to 5.2.0 (or perhaps v6.x, looking on whether or not it’s far sufficiently exact at that level)

React

Migrating from v15.6.2 to v16.13.1 would require a first-rate time commitment, attributable to the reliable assortment of breaking adjustments which relish occurred in intervening versions.

Compatibility with Recent Code

Whereas right here’s the safest alternative in phrases of making sure long-term compatibility, upgrading to React v16 comes with a first-rate tag. A assortment of lifecycle recommendations had been deprecated and renamed, that will require code-mods to be bustle in yelp to rename the now-deprecated recommendations. Whereas we would peaceable in the end are attempting to bustle these codemods if we procure Preact, within the case of Preact we will also migrate absolutely earlier than making the adjustments. The React upgrade would require the codemods as phase of the upgrade, which makes it more sturdy to undo or work on in pieces.

Thanks to heavy usage of the now-deprecated theseus/Element helper, the seller instruments relish somewhat shrimp usage of lifecycle recommendations that are deprecated in React v16. The Web Toolkit, on the different hand, makes employ of a reliable assortment of these deprecated lifecycle recommendations, and will require refactoring and regression making an are attempting out in yelp to securely be migrated.

Since the FES staff’s experimental structure is explicitly Preact based, and because of the there is a wish to fragment the Web Toolkit, selecting emigrate to React would impose extra complexity on working in any code which is shared between Preact and React elements of the codebase. Whereas this would within the beginning advantageous be the Web Toolkit, it would possibly perhaps perhaps perhaps also explicitly block our ability to investigate a new model of the seller instruments subapp structure which utilized the Preact SSR service.

Compatibilites with Recent Libraries

Brace your self.

Redux (upgrade required)

As we will must upgrade React-Redux, we will must upgrade Redux

React-Redux (upgrade required)

React-Redux v4.4.5 (our contemporary) would not toughen React v16. React-Redux v7.2.0 (the most neatly-liked) requires as a minimum React v16.8.3. In yelp to upgrade React to the most neatly-liked model, we will must:

  • Upgrade React-Redux to v5.1.2
  • Upgrade React from v15.6.2 to as a minimum v16.8.3
  • Upgrade React-Redux to v7.2.0

React Router (upgrade required)

React-Router v2.3.0 (our contemporary) would not toughen React v16. React-Router most attention-grabbing presents upgrade paths to v5 (exact free up) or v6 (currently in beta). React-Router v5.2 helps React v15+, so we will also get dangle of this upgrade earlier than upgrading to React v16.

React Router Redux (library not supported, alternative resolution required)

Use of this library has been deprecated and this would possibly increasingly perhaps well not work with neatly-liked versions of React Router. If now we must upgrade React-Router, we will must procure away this library.

It’s conceivable that we will replace this library with connected-react-router, which is one rapid alternative. Of value: connected-react-router v5.0.1 has a bundle dimension of 27.4KB / 4.6KB gzipped, whereas react-router-redux used to be 4.1KB / 1.4KB. It also depends on react-router as a minimum v4.3 and react-router v4.3. connected-react-router v6.8.0 has a bundle dimension of 9.5KB / 3.2KB, which is a principal better number. Sadly, connected-react-router v6+ requires React v16.4.0 and React Redux v6 or v7. To summarize, adopting this library would require a first-rate (although rather brief) extra bundle dimension hit and add extra complexity to the upgrade job. Additionally, connected-react-router looks to relish a laborious dependency on immutable, which would possibly perhaps well well within the long-term quit us from migrating far off from immutable.

Compatibility with Recent Tooling

Jest / Enzyme

A React v16 adapter for Enzyme is equipped, and no main compatibility / breaking adjustments seem to consequence as phase of that upgrade.

Jest / Unitcards

The unitcard framework itself would not seem to depend on performance which would possibly perhaps well well spoil within the upgrade, so doubtlessly minimal impact right here.

DevTools

Getting to React v16 would unlock most important new abilities for performance making an are attempting out within the React dev instruments extension.

Compatibility with Future Plans

Would adopting React impact any future style plans we’ve mentioned for next 1/2/5 years?

Typescript toughen

React staff has invested in enabling Typescript toughen.

Suspense?

Our subapp structure and lazyloading instruments for elements already solve lots of the concerns which Suspense looks to solve, although being in a position to tear to framework-supported tooling moderately than in-home tooling would be an serve. That acknowledged, the dimensions of our codebase would provide most important challenges to migrating exclusively to Suspense, and there is not any guarantee we would look many beneficial properties. The get dangle of as you render style impressed by Suspense has proven customarily to require complex convey administration code to manual clear of waterfall records fetching patterns and different performance concerns, and I’ve learned that reviewing and affirming these styles of elements can present to be extraordinarily demanding and time-intensive.

GraphQL

As we’re not in adopting Relay, the picks remain the identical as with Preact: doubtlessly both @apollo/client or urql, looking on the outcomes of experimenting with the following structure to identify what most productive suits.

Migration Design

This work is currently blocked by the elimination of React.PropTypes and React.createClass, each of that are blocked by the Web Platform staff’s ESM syntax upgrade.

Impart that nearly all of this work is complex and even a single line / upgrade would possibly perhaps well even involve most important quantities of time. Among different issues, the react-router breaking adjustments will require some invasive adjustments to the seller instruments subapp structure to remodel how issues are loaded / routed. Placing off react-router-redux will also involve hundreds of labor, although every piece ought to confidently be somewhat autonomous.

  • Fully procure away react-router-redux
    • In every other case now we must, as phase of a division, upgrade each react-router and react-router-redux on the identical time. react-router’s upgrade entails most important breaking adjustments, so this won’t be straightforward.
  • Upgrade react-router to v5.2.0
    • Quiet a reliable breaking exchange spicy a pair of branches, but as a minimum it would possibly perhaps perhaps perhaps be change into self reliant from react-router-redux
  • Upgrade react-redux to v5.1.2
  • Upgrade React to v16.X (witness for a exact React model)
    • Many breaking adjustments, most of which is ready to be resolved via codemod
    • Impart that this would possibly increasingly perhaps well well be demanding to roll out slowly and demanding to revert: because of the so lots of the adjustments will seemingly be “breaking”, we won’t be capable of as without concerns switch this on and off — we’ll must merge the division that takes us to v16 with a push protect and attain validation on the adjustments.
  • Upgrade React to v16.8.3
    • Codemods + different adjustments to replace lifecycle recommendations
    • We would possibly perhaps perhaps are attempting to search out doing v15.6.2 -> v16.8.3 straight away without an intermediate step, looking on the scale of the adjustments from running the total codemods
  • Upgrade react-redux to v7.2.0
  • Upgrade React to v16.13.1

In yelp to manual clear of mutilating the doc too principal by both casting off sing groups or attempting to level to context inline, some clarification right here.

FES

Etsy’s Front-Atomize Methods staff, who create and protect the systems which our frontend engineers employ to invent new products.

Buyer Facet

Buyer-going via pages at Etsy are currently server-rendered in PHP, with jQuery and different clientside code to stitch them together.

Orange Docs

Inside technical documentation with a rigid formula, shared internally in yelp to tear making an are attempting enter on sing structure or implementation tiny print.

Web Toolkit

Etsy’s internal element-based get dangle of machine. It is currently maintained by our Own Methods staff, who are splendidly affected person other folks, with implementations accessible each as React elements for our vendor instruments and and vanilla Javascript for our buyer-going via pages.

Unitcards

An internally-maintained (deprecated) React element making an are attempting out framework which predates our (extraordinarily contemporary) adoption of Jest and Enzyme. We have ~500 unitcard test files performing ~20okay assertions all acknowledged and accomplished, which invent up a first-rate quantity of our React codebase’s coverage. Something else which precipitated us to are attempting emigrate reliable numbers of tests from the Unitcard framework to Enzyme would be dramatically exchange the scope of an upgrade challenge.

Web Platform

My staff! The place FES is accountable for the instruments which energy Etsy’s frontend rendering and performance, Web Platfom is accountable for the tooling our developer trip — issues like story, eslint, Webpack, jest, polyfills, Javascript operate toughen, etc. There would possibly be ample overlap between the tasks of the 2 groups that we employ hundreds of time participating with FES, which is reliable — they’re improbable other folks.

ESM

Since January, Etsy has adopted Webpack, launched ES6+ toughen (we were on ES5 syntax with JSX compatibility hacked in to our ragged create instruments as gradual as March), migrated from Jasmine and Unitcards to Jest and Enzyme, and plenty alternative dev tooling upgrades. Definitely one of the last main sweeping updates to the codebase left to total is transferring from AMD syntax to ESM syntax. Whereas we’re in a position to employ codemods to handle principal of this, our codebase and these codemodes relish ample incompatiblities that getting to a level where we will experimentally present that migrating to ESM would not negatively impact users has been quite a distress. We’re nearly ready to begin this experiment!

Subapps

Etsy’s vendor instruments are divided into a central “shared” React app which boots up our sidebar/navigation and many mostly-autonomous “subapps”. We employ react-router within the main app to handle lazyloading every subapp’s elements, reducers, etc. Definitely one of the main targets of this structure used to be independence — developers working on the messaging subapp, as an instance, wished to relish the ability to work on their personal without demanding about impacting developers working on analytics tooling or yelp administration. There are some picks made right here which would possibly perhaps perhaps be sub-optimal for a space with a somewhat tiny assortment of lazyloaded sections, or where every slothful-loaded section used to be somewhat limited, but our structure is aimed at scaling out to N subapps (there are currently 32) with most important variations in complexity.

theseus/Element

theseus is an Etsy-internal framework conventional as phase of a migration to React from our earlier stack. Quiet carefully conventional as phase of our underlying living of helper capabilities / classes, but being deprecated in prefer of external libraries and tooling which will not be as reckoning on the present convey of our instruments.

The Element wrapper allowed us to put in writing elements which would possibly perhaps well even work in each Backbone and React for the length of the migration, allowing us to swap out elements whereas your complete remained the identical (hence, the title). Element most attention-grabbing allowed usage of a tiny living of React lifecycle recommendations and prevented the usage of React element native convey, amongst different issues.

NOW WITH OVER +8500 USERS. other folks can Join Knowasiak for free. Signal in on Knowasiak.com
Read More

Vanic
WRITTEN BY

Vanic

“Simplicity, patience, compassion.
These three are your greatest treasures.
Simple in actions and thoughts, you return to the source of being.
Patient with both friends and enemies,
you accord with the way things are.
Compassionate toward yourself,
you reconcile all beings in the world.”
― Lao Tzu, Tao Te Ching