files/en-us/web/api/launch_handler_api/index.md
{{SeeCompatTable}}{{DefaultAPISidebar("Launch Handler API")}}
The Launch Handler API allows developers to control how a progressive web app (PWA) is launched — for example if it uses an existing window or creates a new one, and how the app's target launch URL is handled.
You can specify launch behavior for your app by adding the launch_handler field to your web app manifest file. This has one sub-field, client_mode, which contains a string value specifying how the app should be launched and navigated to. For example:
{
"launch_handler": {
"client_mode": "focus-existing"
}
}
If not specified, the default client_mode value is auto. Available values are:
focus-existing
navigate-existing
navigate-new
auto
When focus-existing is used, you can include code inside the {{domxref("LaunchQueue.setConsumer", "window.launchQueue.setConsumer()")}}'s callback function to provide custom handling of the {{domxref("LaunchParams.targetURL", "targetURL")}}
window.launchQueue.setConsumer((launchParams) => {
// Do something with launchParams.targetURL
});
[!NOTE] {{domxref("LaunchParams")}} also has a {{domxref("LaunchParams.files")}} property, which returns a read-only array of {{domxref("FileSystemHandle")}} objects representing any files passed along with the launch navigation via the
POSTmethod. This allows custom file handling to be implemented.
setConsumer() is passed a LaunchParams object instance.launch_handler client_mode value of focus-existing, navigate-new, or navigate-existing, LaunchQueue provides access to functionality that allows custom launch navigation handling to be implemented in the PWA. This functionality is controlled by the properties of the {{domxref("LaunchParams")}} object passed into the {{domxref("LaunchQueue.setConsumer", "setConsumer()")}} callback function.launch_handler manifest field client_mode value.if ("launchQueue" in window) {
window.launchQueue.setConsumer((launchParams) => {
if (launchParams.targetURL) {
const params = new URL(launchParams.targetURL).searchParams;
// Assuming a music player app that gets a track passed to it to be played
const track = params.get("track");
if (track) {
audio.src = track;
title.textContent = new URL(track).pathname.slice(1);
audio.play();
}
}
});
}
This code is included in the PWA, and executed when the app loads, upon launch. The {{domxref("LaunchQueue.setConsumer", "window.launchQueue.setConsumer()")}}'s callback function extracts the search param out of the {{domxref("LaunchParams.targetURL")}} and, if it finds a track param, uses it to populate an {{htmlelement("audio")}} element's src and play the audio track that this points to.
See the Musicr 2.0 demo app for full working code.
{{Specifications}}
{{Compat}}