Superpowers of Browser's Web API

Rate this content
Bookmark
Project website

When writing code for the Web, there are a lot of capabilities that are offered out of the box by our browsers. If you ever wrote a File Upload component, used timers and intervals, interacted with the DOM, or stored something in the Local/Session Storage, you had to go to the MDN’s Web API docs, to find some relevant information on how to implement that code.

In this session, we will delve into the exciting world of browser Web APIs that are not so commonly used (although they should be) and explore their incredible capabilities 🚀

All these features offer new opportunities for creating immersive web experiences that can help businesses grow and connect with customers.

So if you are the kind of an engineer who wants to stay ahead of the curve when it comes to web development, learn how Intersection Observer, Background Sync API, Screen Wake Lock API (and many more) can help you create better web applications that will keep users engaged and coming back for more!

Nikola Mitrovic
Nikola Mitrovic
25 min
15 Nov, 2023

Comments

Sign in or register to post your comment.

Video Summary and Transcription

Today's talk covers Web API, including exotic and not commonly used APIs that can enhance web applications. The IntersectionObserver API allows for lazy loading images and infinite scroll lists. The Screen Wake Lock API prevents devices from locking, ensuring uninterrupted usage. The Wake Lock API is useful for various scenarios, but browser and device support varies. Background Sync and Broadcast Channel APIs enable offline functionality and communication between contexts. Lastly, the impact of code on the planet is highlighted, emphasizing the need for more efficient web development.

1. Introduction to Web API

Short description:

Today's talk will cover Web API, which is a collection of built-in interfaces that allow developers to interact with web pages and provide fancy functionality. JavaScript's single-threaded nature and the event loop play a role in how setTimeout behaves. WebAPIs, provided by browsers, offer a wide range of APIs and interfaces for web development. Let's explore some exotic and not commonly used APIs that can give our web applications superpowers. I'm Nikola Mitrović, and these are my Web APIs highlights for today.

Hello React Summit US! I wish you a warm welcome to today's talk. Today we're going to talk about something called Web API. But before we do that, we will need to take a small step back and go through some JavaScript basics.

If we see this example, and I ask you the question, what the order of the logs would be in the console, probably most of you would say 1, 3, 2. And that is correct. But how do we know that? How do we know how setTimeout behaves?

As we know, JavaScript is a single thread language. Let's visualize this a bit. There is a call stack, we execute the first function which is synchronous, and then we log the 1 as a value immediately. But then the second one is asynchronous, and it needs to go somewhere else, not to block that single thread. And it goes to the queue once the timer is finished. In the meantime, the call stack continues with executing the functions, then event loop checks if the call stack is empty, and pushes back the callback with the value of 2. But the main question is, where does the setTimeOut wait while we continue with the execution of our code?

If we Google this function, the first result we get is this one. So there is MDN documentation where we can see some stuff about setTimeOut function. And here we can see that it's under a tab WebAPIs. If we click that, we can see that basically WebAPIs is a collection, a list of APIs and interfaces which we may use right out of the box when developing web applications. Basically, it's a functionality provided by a runtime environments. In this case, our browsers. And if we scroll a bit, we can see that there are a lot of APIs, a really, really long list of APIs. And here we can maybe observe that there are some quite commonly known and used APIs, like WebRTC, session storage, local storage, file upload API, a DOM API, and so on. But looking through this list, I was wondering, what are the some of the exotic and not so commonly used APIs that we may use which can give us our web application certain superpowers in certain use cases? So basically, we're here to find out that today. And like we established, the answer to this question about set timeout is Web API, which is basically a collection of built-in interfaces that allow developers to interact with our pages and provide some fancy functionality.

I'm going to quickly introduce myself. When I was in high school, I always wanted to study psychology. Now I work my dream job at Wega IT based in Noišac, Serbia, where I work as a development which means that I teach people soft skills, how to communicate with the clients, how to deal with stress, how to do presentations like this, why not, and so on. My name is Nikola Mitrović and these are my Web APIs highlights for you for today. OK, now we go to our first example for today. Let's say we have a page like this and there is nothing there but the background, but once we scroll a bit down we can start observing that there is something in the bottom left corner. It's a small astronaut, which when fully visible says, hello world. Once we scroll a bit up and the astronaut is not fully visible, the message goes away. We scroll again a bit down and again, once the astronaut is fully visible again he says hello world.

2. Using the IntersectionObserverAPI

Short description:

The IntersectionObserverAPI is an API that determines if an element is visible on a page. In React, we can create a hook called useVisible to observe elements. The hook accepts the reference of the object and configuration options like the root and threshold. By using this API, we can lazy load images, create infinite scroll lists, and defer animations. It's important to consider browser support, but this API is fully supported by major vendors.

So how did we manage to do this? There is an API called IntersectionObserverAPI which basically figures out if the element is visible on a page or not. If we would build a hook in React for this, we would probably call it useVisible and that hook would accept two parameters.

It would accept the reference of the object that we are trying to observe and we have certain configuration options. One of those options is root. If we pass null, then we assume that we are observing the element according to the whole document. Basically it means we can have smaller containers with smaller scrollable areas and we can observe some elements there, but if we pass null we look according to the whole document. We can have a margin around that root, so we can catch that intersection a little bit earlier if needed. And we have a certain threshold, meaning that if we pass this value we need 100% of visibility for the element, so we can adjust this accordingly. Then we would have a state called isVisible, and then we instantiate the IntersectionObserver object. In the callback we get the Entry object, which has a property called isIntersecting. This property holds the information about the visibility of an element, and then we can set that to our state. So now what's left to be done is just to call observe method on that reference and return that state. And that would be it.

The example with the astronaut was nice and cute, but you're probably now wondering what are some real-life use cases where we can use this API. We could lazy load images, right? So we load the initial content for the page and everything below the fold, what is not needed initially, we can defer and lazily load. And once we scroll to a certain section, then we start downloading images and other assets. We could create infinite scroll lists. For example, we don't need library for this. We could have a list out of 10 elements. We could have a separator at the end of that list. And once we scroll back and that separator is visible, then we trigger the next request. We could defer animations. So we don't need to run animations if something is not... If the certain element is not visible on that page. And by doing so, we can save some computer power. So we don't need to run some JavaScript if the element is not visible in the viewport. One thing to always consider when using some of these APIs is browser support. If we look at the major vendors, we can see that this API is fully supported. So great news, we can safely use this in all browsers. OK.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

Don't Solve Problems, Eliminate Them
React Advanced Conference 2021React Advanced Conference 2021
39 min
Don't Solve Problems, Eliminate Them
Top Content
Humans are natural problem solvers and we're good enough at it that we've survived over the centuries and become the dominant species of the planet. Because we're so good at it, we sometimes become problem seekers too–looking for problems we can solve. Those who most successfully accomplish their goals are the problem eliminators. Let's talk about the distinction between solving and eliminating problems with examples from inside and outside the coding world.
Jotai Atoms Are Just Functions
React Day Berlin 2022React Day Berlin 2022
22 min
Jotai Atoms Are Just Functions
Top Content
Jotai is a state management library. We have been developing it primarily for React, but it's conceptually not tied to React. It this talk, we will see how Jotai atoms work and learn about the mental model we should have. Atoms are framework-agnostic abstraction to represent states, and they are basically just functions. Understanding the atom abstraction will help designing and implementing states in your applications with Jotai
Debugging JS
React Summit 2023React Summit 2023
24 min
Debugging JS
Top Content
As developers, we spend much of our time debugging apps - often code we didn't even write. Sadly, few developers have ever been taught how to approach debugging - it's something most of us learn through painful experience.  The good news is you _can_ learn how to debug effectively, and there's several key techniques and tools you can use for debugging JS and React apps.
Fighting Technical Debt With Continuous Refactoring
React Day Berlin 2022React Day Berlin 2022
29 min
Fighting Technical Debt With Continuous Refactoring
Top Content
Let’s face it: technical debt is inevitable and rewriting your code every 6 months is not an option. Refactoring is a complex topic that doesn't have a one-size-fits-all solution. Frontend applications are particularly sensitive because of frequent requirements and user flows changes. New abstractions, updated patterns and cleaning up those old functions - it all sounds great on paper, but it often fails in practice: todos accumulate, tickets end up rotting in the backlog and legacy code crops up in every corner of your codebase. So a process of continuous refactoring is the only weapon you have against tech debt. In the past three years, I’ve been exploring different strategies and processes for refactoring code. In this talk I will describe the key components of a framework for tackling refactoring and I will share some of the learnings accumulated along the way. Hopefully, this will help you in your quest of improving the code quality of your codebases.
The Epic Stack
React Summit US 2023React Summit US 2023
21 min
The Epic Stack
Top Content
Modern web development is fantastic. There are so many great tools available! Modern web development is exhausting. There are so many great tools available! Each of these sentiments is true. What's great is that most of the time, it's hard to make a choice that is wrong. Seriously. The trade-offs of most of the frameworks and tools you could use to build your application fit within the constraints of the vast majority of apps. Despite this, engineers consistently struggle with analysis paralysis.Let's talk about this, and a solution I am working on for it.
AHA Programming
React Summit Remote Edition 2020React Summit Remote Edition 2020
32 min
AHA Programming
Top Content
Are you the kind of programmer who prefers to never see the same code in two places, or do you make liberal use of copy/paste? Many developers swear the Don't Repeat Yourself (DRY) philosophy while others prefer to Write Everything Twice (WET). But which of these produces more maintainable codebases? I've seen both of these approaches lay waste to codebases and I have a new ideology I would like to propose to you: Avoid Hasty Abstractions (AHA). In this keynote, we'll talk about abstraction and how you can improve a codebase applying and creating abstractions more thoughtfully as well as how to get yourself out of a mess of over or under-abstraction.

Workshops on related topic

React, TypeScript, and TDD
React Advanced Conference 2021React Advanced Conference 2021
174 min
React, TypeScript, and TDD
Top Content
Featured WorkshopFree
Paul Everitt
Paul Everitt
ReactJS is wildly popular and thus wildly supported. TypeScript is increasingly popular, and thus increasingly supported.

The two together? Not as much. Given that they both change quickly, it's hard to find accurate learning materials.

React+TypeScript, with JetBrains IDEs? That three-part combination is the topic of this series. We'll show a little about a lot. Meaning, the key steps to getting productive, in the IDE, for React projects using TypeScript. Along the way we'll show test-driven development and emphasize tips-and-tricks in the IDE.
Web3 Workshop - Building Your First Dapp
React Advanced Conference 2021React Advanced Conference 2021
145 min
Web3 Workshop - Building Your First Dapp
Top Content
Featured WorkshopFree
Nader Dabit
Nader Dabit
In this workshop, you'll learn how to build your first full stack dapp on the Ethereum blockchain, reading and writing data to the network, and connecting a front end application to the contract you've deployed. By the end of the workshop, you'll understand how to set up a full stack development environment, run a local node, and interact with any smart contract using React, HardHat, and Ethers.js.
Remix Fundamentals
React Summit 2022React Summit 2022
136 min
Remix Fundamentals
Top Content
Featured WorkshopFree
Kent C. Dodds
Kent C. Dodds
Building modern web applications is riddled with complexity And that's only if you bother to deal with the problems
Tired of wiring up onSubmit to backend APIs and making sure your client-side cache stays up-to-date? Wouldn't it be cool to be able to use the global nature of CSS to your benefit, rather than find tools or conventions to avoid or work around it? And how would you like nested layouts with intelligent and performance optimized data management that just works™?
Remix solves some of these problems, and completely eliminates the rest. You don't even have to think about server cache management or global CSS namespace clashes. It's not that Remix has APIs to avoid these problems, they simply don't exist when you're using Remix. Oh, and you don't need that huge complex graphql client when you're using Remix. They've got you covered. Ready to build faster apps faster?
At the end of this workshop, you'll know how to:- Create Remix Routes- Style Remix applications- Load data in Remix loaders- Mutate data with forms and actions
Vue3: Modern Frontend App Development
Vue.js London Live 2021Vue.js London Live 2021
169 min
Vue3: Modern Frontend App Development
Top Content
Featured WorkshopFree
Mikhail Kuznetcov
Mikhail Kuznetcov
The Vue3 has been released in mid-2020. Besides many improvements and optimizations, the main feature of Vue3 brings is the Composition API – a new way to write and reuse reactive code. Let's learn more about how to use Composition API efficiently.

Besides core Vue3 features we'll explain examples of how to use popular libraries with Vue3.

Table of contents:
- Introduction to Vue3
- Composition API
- Core libraries
- Vue3 ecosystem

Prerequisites:
IDE of choice (Inellij or VSC) installed
Nodejs + NPM
Developing Dynamic Blogs with SvelteKit & Storyblok: A Hands-on Workshop
JSNation 2023JSNation 2023
174 min
Developing Dynamic Blogs with SvelteKit & Storyblok: A Hands-on Workshop
Top Content
Featured WorkshopFree
Alba Silvente Fuentes
Roberto Butti
2 authors
This SvelteKit workshop explores the integration of 3rd party services, such as Storyblok, in a SvelteKit project. Participants will learn how to create a SvelteKit project, leverage Svelte components, and connect to external APIs. The workshop covers important concepts including SSR, CSR, static site generation, and deploying the application using adapters. By the end of the workshop, attendees will have a solid understanding of building SvelteKit applications with API integrations and be prepared for deployment.
Back to the Roots With Remix
React Summit 2023React Summit 2023
106 min
Back to the Roots With Remix
Featured Workshop
Alex Korzhikov
Pavlik Kiselev
2 authors
The modern web would be different without rich client-side applications supported by powerful frameworks: React, Angular, Vue, Lit, and many others. These frameworks rely on client-side JavaScript, which is their core. However, there are other approaches to rendering. One of them (quite old, by the way) is server-side rendering entirely without JavaScript. Let's find out if this is a good idea and how Remix can help us with it?
Prerequisites- Good understanding of JavaScript or TypeScript- It would help to have experience with React, Redux, Node.js and writing FrontEnd and BackEnd applications- Preinstall Node.js, npm- We prefer to use VSCode, but also cloud IDEs such as codesandbox (other IDEs are also ok)