Skip to content

Commit a8f0672

Browse files
authored
Document post-commit handler usage in README (#294)
Added example of using a controller to manage post-commit handlers in navigation.
1 parent c558fb0 commit a8f0672

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,24 @@ This is simpler than the alternative of canceling the original navigation and st
731731
732732
It's possible in the future we could contemplate allowing something similar for `{ commit: "immediate" }` navigations as well. There, we would not be able to hide the intermediate state perfectly, as code would still be able to observe the intermediate `location.href` values and such. But we could treat such post-commit redirects as special types of replace navigations, which "take over" any promises returned from `navigation.navigate()`, delay `navigatesuccess`/`navigateerror` events, etc.
733733

734+
The controller can also be used to add a post-commit handler from the precommit handler:
735+
736+
```js
737+
navigation.addEventListener("navigate", e => {
738+
e.intercept({
739+
async precommitHandler(controller) {
740+
if (await some_original_operation_that_can_fail()) {
741+
controller.addHandler(async () => {
742+
do_some_post_processing();
743+
});
744+
}
745+
}
746+
});
747+
});
748+
```
749+
750+
This allows a more dynamic control flow between precommit operations and post-commit operations.
751+
734752
### Transitional time after navigation interception
735753

736754
As part of calling `event.intercept()` to [intercept a navigation](#navigation-monitoring-and-interception) and convert it into a single-page navigation, the handlers passed to `intercept()` can return promises that might not settle for a while. During this transitional time, before the promise settles and the `navigatesuccess` or `navigateerror` events fire, an additional API is available, `navigation.transition`. It has the following properties:

0 commit comments

Comments
 (0)