VOOZH about

URL: https://thenewstack.io/doordash-building-isomorphic-javascript-libraries/

⇱ Doordash Building Isomorphic JavaScript Libraries - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2023-01-02 06:00:56
Doordash Building Isomorphic JavaScript Libraries
Software Development

Doordash Building Isomorphic JavaScript Libraries

Isomorphic libraries make server-side and client-side code execution more efficient if you can get past the hassle of building them.
Jan 2nd, 2023 6:00am by Jessica Wachtel
👁 Featued image for: Doordash Building Isomorphic JavaScript Libraries

Isomorphic JavaScript libraries are helpful when running code both client-side and server-side. Building them comes with many challenges that include adding the right dependencies, creating a function declaration identical in both environments, and testing. The successful creation or abandonment of any isomorphic library includes constant obstacle solving or final decision on when the complexities of the build outweigh later efficiencies.

Online delivery service DoorDash has found that its code logic works well across multiple environments through isomorphics. The web platform team writes code in several JavaScript environments, including React and Node.js. To further blurr the lines, several pages are also undergoing a migration from client-side to server-side rendering as discussed in the blog post written by Software Engineer Nick Fahrenkrog.

The tradeoff with isomorphic libraries is how complex they are to build vs their efficiency. The best way to illustrate these challenges and techniques employed at scale is to follow along as Fahrenkrog builds a fictitious isomorphic library.

Functional Requirements

The following example is written in TypeScript.

What makes building an isomorphic library challenging? Relying on APIs native in one environment but not in another, ie. “fetch” or “document.cookies”. Other challenges include:

  • Working with dependencies that aren’t isomorphic.
  • Functions that behave differently client-side vs server-side.
  • Exposing parameters that aren’t needed in all environments.

As to include as many challenges in DoorDash’s example, the fictitious library build will export a function to check if a coffee order is ready via a network request and send a unique id.

The scope:

— Export an async function named “isCoffeeOrderReady” which optionally takes “deviceId” as a parameter and returns a Boolean.

  • Send an http POST request with the request body “{deviceID:<deviceID>}” to a hard-coded endpoint.
  • Run in Node.js or browser.
  • Browser will read “deviceID” directly from cookies.
  • Use keep-alive connections.

With the details scoped, it’s time to dive into the five primary challenges with implementing isomorphics, and how to overcome them.

Challenge 1: Choosing the Right Dependencies

For illustration purposes. This is Node <= 16 and it doesn’t use any experimental flags. Node 18 has native fetch support.

Browsers can support fetch but here’s a decision to make: an isomorphic library or a Node.js library? Choose the Node.js library and it might be scrutinized for its impact on the bundle size in the browser. Choose the isomorphic and it needs to fulfill every environment’s requirements for dependences…

DoorDash chose isomorphic-fetch — a combination of node-fetch in Node.js and GitHub fetch polypill in browsers. The code below is an example of the fetch request.

👁 Image

Note: For the sake of brevity, many important details such as retry and error handling will be ignored.

The next divergence is handling “deviceId”. “deviceId” is a parameter sent via the fetch request in the Node.js environment but in the browser, it’s read directly from a cookie.

The window should always be defined in the browser, not globally in Node.js. So document.cookie is defined in the window. The code now looks like the image below.

👁 Image

This isn’t the only way of detecting whether code is on the server or client though it’s a popular way. More information can be found here.

This new code file added another challenge.

Challenge 2: Designing a Unified API Between Environments

The “deviceId” parameter is a required argument in Node.js but not in the browser. Before moving any further, the function declaration must be identical in both environments. Two possible solutions are:

Write the API as shown, requiring “deviceId”. Caution this action could be misleading to adopters because that value must be passed in the browser even though it will be ignored.

Make “deviceId” optional. This allows the function invocation in the browser without the argument and in Node.js with the argument. The caution here is that it can also be called in Node.js without the argument. TypeScript will not prevent misuse of the API.

The second approach is the better choice but it’s not fail-proof.

Challenge 3: Ensuring Dependencies Only Affect Intended Environments

The “document.cookie” code change introduced another issue. “Cookie”, installed in Node.js, isn’t used in the code path. It’s better to have unnecessary dependences in Node.js than the browser but best to avoid them altogether when possible and aim for minimal bundle size.

There’s a two-step process for this: separate the code files, one for Node.js and one for browser, then use a bundler (such as webpack) that supports tree shaking.

The importance of choosing the right dependencies. node-fetch implements a different spec than the native browser fetch and one of the areas of divergence are the keep-alive connections. Add the following flag to use a keep-alive connection in browser fetch:

fetch(url, { ..., keepalive: true })

In node-fetch create “http” and/or “https” http.Agent instances and pass that as an agent argument to the fetch request as shown here:

👁 Image

In Node.js environments, keep-alive connections can’t be set up correctly with isomorphic fetch because isomorphic-fetch utilizes node-fetch internally but doesn’t expose the agent options. In order to set up the keep-alive connections correctly, the native fetch and node-fetch libraries will both need to be used.

This can be done by splitting the entry points by environment-specific code paths.

👁 Image

This is the webpack set up with TypeScript.

Separate entry points are acceptable but only one TypeScript types declaration file can be used in keeping with goal of an isomorphic library, to expose the same API regardless of the environment.

Here’s a list of other isomorphic JavaScript libraries using this pattern for purposes of isomorphic fetch.

The final code paths look like this, as documented in the index.ts files.

👁 Image

…And in the browser.ts file:

👁 Image

And with that, the library’s functional requirements are complete. The full benefit of isomorphism isn’t showcased as there is little shared code but in larger projects there is more opportunity for shared code. It’s also important to be certain that everything exported has the exact same API because only one list of type declaration files is published.

Challenge 4: Testing Every Environment

Testing each environment is very much the vegetables of coding: it’s good for you even though you don’t want to do it. One key factor to consider when testing isomorphic libraries is that most test will need to be written twice to make sure identical functionality exists in both environments. Since isomorphism couples logic across all environments, changes in one environment needs testing in another.

Challenge 5: Observability and Metrics

The infrastructure for observability looks very different in both environments. Node.js may be extended to capture latency, error rate, and log warnings and errors with context to help trace across microservices while the browser might only expand to capture errors. The same problem-solving patterns detailed earlier can resolve these differences.

Final Thoughts

Several challenges arose throughout the build of the fictitious library but they were all solvable. The tradeoffs vs the benefits were considered, and solutions were met. This is one example of one approach and a quick google search shows endless information and depth on this isomorphism.

TRENDING STORIES
Jessica Wachtel is a developer marketing writer at InfluxData where she creates content that helps make the world of time series data more understandable and accessible. Jessica has a background in software development and technical journalism.
Read more from Jessica Wachtel
SHARE THIS STORY
TRENDING STORIES
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.