Working with Web Standards

Aurelia's philosophy is simple: enhance the web platform, don't replace it. This guide demonstrates how to leverage modern web standards and APIs within Aurelia applications, showing how the framework's features—dependency injection, reactivity, and lifecycle management—make working with native browser APIs even more powerful.

Why Web Standards Matter

The web platform provides a rich set of APIs that solve real problems: fetching data, managing state, observing elements, handling files, and much more. These APIs are:

  • Battle-tested by millions of developers

  • Well-documented with extensive resources

  • Forward-compatible as browsers evolve

  • Framework-agnostic so your knowledge transfers

When you master web standards, you're not just learning Aurelia—you're becoming a better web developer, period. Your skills compound rather than expire when frameworks change.

The Aurelia Advantage

While you can use any web API directly in Aurelia, the framework provides enhancements that make common patterns easier:

  • Dependency Injection: Create reusable services that wrap web APIs

  • Reactive Binding: Connect web API state directly to your templates

  • Lifecycle Hooks: Initialize and cleanup web APIs at the right time

  • Observable Properties: Automatically update UI when web API state changes

Let's explore how to use various web standards effectively within Aurelia applications.

Fetch API

The Fetch API provides a modern interface for making HTTP requests. While Aurelia provides the @aurelia/fetch-client package with additional features, understanding native fetch is valuable.

Basic Usage

Creating a Fetch Service

Wrap fetch in a DI service for better reusability and testing:

Register and use it:

History API

The History API lets you manipulate browser history and URLs. While Aurelia's router handles most navigation, you can use the History API directly for custom scenarios.

Using History API Directly

Custom Events & EventTarget

Custom events enable component communication without tight coupling. Aurelia's binding system works seamlessly with standard DOM events.

Dispatching Custom Events

Listening to Custom Events

EventTarget for Service Communication

Use EventTarget for services that need to emit events:

FormData API

The FormData API simplifies working with forms, especially for file uploads and multipart data.

Basic FormData Usage

Web Storage (localStorage & sessionStorage)

Web Storage provides simple key-value storage in the browser. Wrap it in a service for type safety and reactivity.

Storage Service with Type Safety

Usage Example: Persisting User Preferences

Intersection Observer API

The Intersection Observer API efficiently detects when elements enter or leave the viewport, perfect for lazy loading, infinite scroll, and animations.

Lazy Loading Images

Infinite Scroll

Resize Observer API

The Resize Observer API detects when elements change size, useful for responsive components and dynamic layouts.

Responsive Chart Component

URL & URLSearchParams

The URL and URLSearchParams APIs make parsing and manipulating URLs straightforward.

Query String Service

Filtering with URL Parameters

Geolocation API

The Geolocation API provides access to the user's location (with permission).

Location Service

Store Locator Example

Page Visibility API

The Page Visibility API detects when a page is visible or hidden, useful for pausing activities when the user switches tabs.

Auto-Pause Video Player

Analytics Service

Clipboard API

The Clipboard API provides secure access to the clipboard for copying and pasting.

Copy to Clipboard Component

Paste Handler

File API & Drag and Drop

The File API combined with drag-and-drop provides excellent file upload experiences.

Drag and Drop File Uploader

Canvas API

The Canvas API provides a powerful 2D drawing surface. Aurelia's lifecycle hooks make it easy to manage canvas rendering.

Signature Pad

Notification API

The Notification API displays system notifications to users (with permission).

Notification Service

Usage in Components

Mutation Observer API

The Mutation Observer API watches for changes to the DOM tree, useful for advanced scenarios like tracking third-party library changes.

Content Change Detector

Web Animations API

The Web Animations API provides powerful, performant animations with JavaScript control.

Animated List Item

Direct DOM Access: No Virtual DOM, No Conflicts

One of Aurelia's most powerful advantages is direct DOM manipulation. Unlike frameworks with virtual DOM abstractions, Aurelia works directly with the actual browser DOM. This means:

  • Zero conflicts when using DOM APIs directly

  • No bridges or wrappers needed for third-party libraries

  • No reconciliation overhead or timing issues

  • Direct access to elements without special refs or hooks

  • Performance benefits from eliminating the virtual DOM layer

You can freely mix Aurelia's reactive system with direct DOM manipulation, web APIs, and third-party DOM libraries without worrying about conflicts or "fighting the framework." The DOM you see in DevTools is the DOM Aurelia uses—what you see is what you get.

Advanced Integration with Aurelia Features

Now let's explore how to combine web standards with Aurelia's advanced internal features for even more powerful patterns.

Using task queue functions for Coordinated DOM Updates

Aurelia's task queue system (from @aurelia/runtime) lets you schedule work to run after the current rendering cycle completes. This is perfect for coordinating web API operations with Aurelia's update cycle.

Coordinating Multiple Async Operations

Use tasksSettled() to wait for all Aurelia tasks to complete before proceeding:

IEventAggregator for Web API Event Coordination

Use Aurelia's event aggregator to create a centralized event system that coordinates web API events across your application.

Now components can subscribe to network events in a type-safe, decoupled way:

IObservation for Reactive Web API Integration

Use Aurelia's observation system to create reactive connections to web APIs:

Creating Custom Attributes for Web APIs

Custom attributes are perfect for encapsulating web API functionality in a reusable, declarative way:

Usage in templates:

Custom Attribute for IntersectionObserver

Create a reusable intersection observer attribute:

Usage:

Platform Abstraction for Testable Web APIs

Use Aurelia's Platform abstraction to make web API code testable:

Combining TaskQueue, EventAggregator, and Web APIs

Here's a sophisticated example that combines multiple Aurelia features with web standards:

Direct DOM Manipulation with Aurelia's Observation

Because Aurelia has no virtual DOM, you can freely manipulate the DOM and Aurelia's observation system will track your changes:

Additional Web Standards

Aurelia works seamlessly with many other web standards:

Already Documented

Other Standards to Explore

  • IndexedDB: For client-side database storage

  • WebRTC: For real-time communication

  • Web Audio API: For audio processing and synthesis

  • WebGL: For 3D graphics

  • Pointer Events: For unified touch/mouse/pen input

  • Performance API: For measuring application performance

  • Request Idle Callback: For scheduling non-critical work

  • Broadcast Channel API: For cross-tab communication

Best Practices

When working with web standards in Aurelia:

Essential Practices

  1. Use Lifecycle Hooks: Initialize observers and APIs in attached(), clean up in detaching()

  2. Leverage DI: Wrap web APIs in services for reusability and testability

  3. Embrace Reactivity: Use @observable to automatically update UI when web API state changes

  4. Handle Errors: Many web APIs require permissions or can fail—always handle errors gracefully

  5. Clean Up: Always disconnect observers, remove event listeners, and cancel animations in detaching()

  6. Feature Detection: Check for API support before using newer features

  7. Progressive Enhancement: Provide fallbacks for unsupported features

Advanced Patterns

  1. Use TaskQueue for Coordination: When you need to wait for Aurelia's DOM updates before using web APIs, use queueTask() or await tasksSettled()

  2. Create Custom Attributes: Encapsulate reusable web API patterns (like intersection observers or auto-save) as custom attributes

  3. Use IEventAggregator for Decoupling: Coordinate web API events across your application with type-safe event classes

  4. Use IObservation for Reactivity: Bridge web API state to Aurelia's reactive system with observation.watch()

  5. Platform Abstraction for Testing: Use IPlatform to access global objects, making your web API code fully testable

  6. Direct DOM Manipulation: Take advantage of Aurelia's lack of virtual DOM—manipulate the DOM directly without conflicts

  7. Combine Features: Mix TaskQueue, EventAggregator, observation, and web APIs for sophisticated patterns

Conclusion

Aurelia's philosophy of enhancing web standards rather than replacing them means you can leverage the full power of the web platform. By combining web APIs with Aurelia's features like dependency injection, reactivity, and lifecycle management, you build applications that are both powerful and maintainable.

Your knowledge of web standards compounds over time, making you a better developer regardless of framework. That's the Aurelia way: build on the web, not around it.

Last updated

Was this helpful?