Sub topic 9
Feature detection and graceful fallback
This chapter assumes you can write basic JavaScript with promises, know CSS pseudo-elements, and already wrap DOM updates in document.startViewTransition(). Not every browser supports the API. A page that calls it without checking will throw an error in those browsers, and the DOM update inside the callback will never run.
Why a check is required
document.startViewTransition is simply missing in unsupported browsers. Calling undefined as a function raises a TypeError. The view then fails to update, which breaks the app instead of just losing an animation.
The rule is that the DOM update must work with or without the transition. The transition only decorates the update.
Detecting support in JavaScript
Test for the method itself before calling it. If it is missing, run the update directly.
function updateView(nextId) {
document.querySelector('#content').innerHTML = renderItem(nextId);
}
function navigate(nextId) {
if (!document.startViewTransition) {
updateView(nextId);
return;
}
document.startViewTransition(() => updateView(nextId));
}Both branches call the same updateView function. Keep all state changes in that function, so the two paths cannot drift apart.
Detecting support in CSS
Style rules for ::view-transition-* pseudo-elements are ignored by browsers that do not know them. In most cases that is harmless, so you do not need a CSS check. If you want to scope other styles to supporting browsers, use @supports:
@supports (view-transition-name: none) {
.card {
view-transition-name: card;
}
}Be careful here. A browser may support view-transition-name without the same-document JavaScript API, so the JavaScript check is still the one that decides whether to start a transition.
Wrapping the check once
Repeating the if in every handler is easy to forget. Put it in one helper and call it everywhere.
function withTransition(update) {
if (!document.startViewTransition) {
update();
return null;
}
return document.startViewTransition(update);
}
withTransition(() => updateView(3));The helper returns null when no transition exists. Code that uses the returned ViewTransition object, such as its promises, must handle that value.
Common mistakes
- Calling
startViewTransitionwithout a check, which throws in unsupported browsers. - Putting state changes outside the update function, so the fallback path skips them.
- Using a CSS
@supportstest as the only guard and then calling the JavaScript API anyway. - Testing only in a supporting browser. Test the fallback by temporarily deleting the method:
document.startViewTransition = undefined;in the console, then run your navigation.
Exercise
Why does the fallback branch call the same updateView function that the transition callback uses, and what goes wrong if the update logic is duplicated in two places?
Show answer
Using one function guarantees both paths produce the same DOM state. If the logic is duplicated, a later change can be made in one copy only. Users of unsupported browsers would then see a different or broken view.