Receive shared content (share target)
Published Updated
The share_target manifest member lets an installed PWA receive content from other apps through the operating system’s share sheet. A shared photo arrives as a File that you can preview and import. The Web App Manifest declares the receiving URL; the browser sends a GET or POST to it as a form submission.
You need an installable PWA with a service worker (see
Make it installable); the share sheet lists a web app only after
the user has installed it, on every platform. One share_target is allowed per manifest,
so route different kinds of shares on the landing page.
1. Declare share_target in the manifest
Section titled “1. Declare share_target in the manifest”action (the receiving URL, inside the manifest scope) and params (the mapping from
share fields to request parameter names) are required. method defaults to GET and
enctype to application/x-www-form-urlencoded. Receiving files requires POST with
multipart/form-data and a files array whose entries name the field and the accepted
MIME types or extensions; list both forms, because operating systems differ in which
they match.
{ "name": "Scrapbook", "start_url": "/", "display": "standalone", "share_target": { "action": "/share", "method": "POST", "enctype": "multipart/form-data", "params": { "title": "title", "text": "text", "url": "url", "files": [{ "name": "media", "accept": ["image/*", ".png", ".jpg", "video/*"] }] } }}A text-only target can keep the GET default: the browser opens action with
?title=…&text=…&url=… and the page reads new URL(location).searchParams. GET is
simpler to debug but leaks the shared text into history and server logs; POST keeps it
in the request body and is the only way to receive files.
2. Handle the POST in the service worker
Section titled “2. Handle the POST in the service worker”A page cannot read a POST body, so the service worker intercepts the request in fetch,
reads formData(), stores the files, and answers with a 303 See Other redirect to a page
that displays them. The redirect stops a refresh from re-submitting the share.
The redirect target carries a marker (?shared=1) so the page knows to look in the cache.
self.addEventListener('fetch', (event) => { const url = new URL(event.request.url); if (event.request.method !== 'POST' || url.pathname !== '/share') return;
event.respondWith( (async () => { const formData = await event.request.formData(); const files = formData.getAll('media'); const cache = await caches.open('shared-content'); await Promise.all(files.map((file, i) => cache.put(`/shared/${i}`, new Response(file)))); const text = formData.get('text') || formData.get('url') || ''; return Response.redirect(`/share/view?shared=1&text=${encodeURIComponent(text)}`, 303); })(), );});Validate what arrives as you would any form submission: other apps place content in unexpected parameters, and the body comes from software you do not control.
3. Read the shared files in the page
Section titled “3. Read the shared files in the page”The redirected page checks the marker, reads the stored responses back as Blobs, shows
them for confirmation, and then deletes them so a second share does not pick up the first
share’s files. Opening /share/view directly, without the marker, renders the normal view.
function isSharedLaunch() { return new URLSearchParams(location.search).has('shared');}
async function takeSharedFiles() { if (!('caches' in window)) return []; const cache = await caches.open('shared-content'); const requests = await cache.keys(); const files = await Promise.all(requests.map((request) => cache.match(request).then((r) => r.blob()))); await Promise.all(requests.map((request) => cache.delete(request))); return files;}
if (isSharedLaunch()) { takeSharedFiles().then(renderSharedFiles);} else { renderDefaultView();}4. Install, then share into the app
Section titled “4. Install, then share into the app”Install the PWA from the browser’s Install entry, open any photo app, choose Share,
and pick your app’s name from the system sheet. The redirect lands on /share/view with
the previews rendered from the cached files. Share a URL from the browser as well to see
the Android text fallback in action. The browsers that offer an installed web app in the
share sheet are listed in the table:
- Legend
- Yes
- Partial
- Flag
- No
- Unknown
| Browser / Platform | Support | Versions | Confidence | Source | Notes |
|---|---|---|---|---|---|
| Chrome (Desktop)# | Yes | 89 | high | source | — |
| Chrome (Android)# | Yes | 76 | high | source | — |
| Edge (Desktop)# | Yes | 89 | high | source | 1 |
| Firefox (Desktop)# | No | — | high | source | 2 |
| Firefox (Android)# | No | — | high | source | 3 |
| Safari (macOS)# | No | — | high | source | 4 |
| Safari (iOS)# | No | — | high | source | 56 |
| Samsung Internet# | Yes | 12.0 | high | source | 7 |
| WebView (Android)# | No | — | high | source | 8 |
See also
Section titled “See also”- Manifest share_target
- Web Share API: native share sheet from the browser
- manifest: share_target support
- Receiving shared data with the Web Share Target API (developer.chrome.com)
- share_target (developer.mozilla.org)
← Back to the Guides overview.