Skip to content

Support sharing to the app via the Web Share Target API#9929

Open
bdolgov wants to merge 2 commits into
TriliumNext:mainfrom
bdolgov:feat/pwa-share-target
Open

Support sharing to the app via the Web Share Target API#9929
bdolgov wants to merge 2 commits into
TriliumNext:mainfrom
bdolgov:feat/pwa-share-target

Conversation

@bdolgov

@bdolgov bdolgov commented May 24, 2026

Copy link
Copy Markdown

Lets the installed PWA receive content from the OS share sheet and turn it into a note in the inbox.

Also includes improvements to test PWA locally via Cloudflare Tunnel or similar setup that exploses devserver under a different host.

Two dev-environment fixes that surfaced while testing the PWA locally:

- assets.ts: allow extra Vite hosts via the TRILIUM_DEV_ALLOWED_HOSTS env
  var (comma-separated). Vite 5+ rejects any Host other than localhost, so
  reaching the dev server through a reverse proxy or LAN hostname otherwise
  fails with "Blocked request" / dynamic-import errors. Inert when unset.

- app.ts: serve manifest.webmanifest, icon.png and robots.txt from the
  dev/prod-aware getClientDir() instead of dist/public. In dev, dist/public
  is never built, so these 404'd (the PWA manifest could not be fetched).
  Prod behaviour is unchanged (getClientDir() resolves to the built public
  dir there).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@dosubot dosubot Bot added the size:L This PR changes 100-499 lines, ignoring generated files. label May 24, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request implements the PWA Web Share Target API, enabling users to share text, URLs, and files directly to Trilium. Key changes include updating the web manifest, adding a new server-side route handler with unit tests, and refactoring the upload middleware to support multi-file uploads. Feedback highlights an inconsistency in the isProtected flag for images, a logic issue where text/URLs are ignored if files are present, and a typo in a file size error message.

Comment thread apps/server/src/routes/api/share_target.ts Outdated
Comment thread apps/server/src/routes/api/share_target.ts Outdated
Comment thread apps/server/src/routes/route_api.ts Outdated
@bdolgov bdolgov force-pushed the feat/pwa-share-target branch 2 times, most recently from 8829162 to 1c3ff3e Compare May 24, 2026 07:49
Lets the installed PWA receive content from the OS share sheet and turn it
into a note in the inbox.

- manifest.webmanifest: declare a POST/multipart share_target at
  /share-target accepting title, text, url and a files array.
- routes/api/share_target.ts: new handler. Shared text becomes an HTML text
  note (escaped, newlines -> <br>); a shared url becomes a linked text note
  with a pageUrl label; shared files become image or file notes. Notes are
  created in the inbox and tagged with a sharedToTrilium label; responds with
  a 303 redirect to the new note. Pure helpers (deriveTitle, buildBody,
  getOptionalString) are unit-tested in share_target.spec.ts.
- route_api.ts: add createMultiUploadMiddleware(WithErrorHandling) using
  multer .array() so the share target can receive multiple files, factoring
  the shared multer setup out of createUploadMiddleware.
- routes.ts: register POST /share-target behind session auth (no CSRF token
  is possible from the browser's share UI; SameSite=Lax cookies prevent
  cross-site POSTs from carrying credentials).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge-conflicts size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants