Merge pull request #370 from philipphutterer/pwa

Fix PWA manifest for relative paths
pull/374/head
Alex 2024-01-05 09:13:46 +02:00 committed by GitHub
commit 4a3a27e71c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 6 deletions

View File

@ -1,5 +1,5 @@
const URL_PATTERN = /* RegExp pattern to match URLs in the shared target text (apps often share additional text, not only URLs) */
/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/gi; const URL_PATTERN = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/gi;
self.addEventListener("fetch", (event) => { self.addEventListener("fetch", (event) => {
if (event.request.method === "GET") { if (event.request.method === "GET") {
@ -9,12 +9,13 @@ self.addEventListener("fetch", (event) => {
const urlRegExp = new RegExp(URL_PATTERN); const urlRegExp = new RegExp(URL_PATTERN);
const sharedText = url.searchParams.get("text"); const sharedText = url.searchParams.get("text");
const matches = [...sharedText.matchAll(urlRegExp)].map((m) => m[0]); const matches = [...sharedText.matchAll(urlRegExp)].map((m) => m[0]);
const basePath = url.pathname.split("/").slice(0, -1).join("/");
event.respondWith( event.respondWith(
(async () => { (async () => {
await Promise.all( await Promise.all(
matches.map((url) => { matches.map((url) => {
return fetch("/add", { return fetch(`${basePath}/add`, {
method: "POST", method: "POST",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
@ -28,7 +29,7 @@ self.addEventListener("fetch", (event) => {
}); });
}) })
); );
return Response.redirect("/", 303); return Response.redirect(basePath, 303);
})() })()
); );
} }

View File

@ -5,7 +5,7 @@
"background_color": "#fafafa", "background_color": "#fafafa",
"display": "standalone", "display": "standalone",
"scope": "./", "scope": "./",
"start_url": "/?utm_source=homescreen", "start_url": "./?utm_source=homescreen",
"icons": [ "icons": [
{ {
"src": "assets/icons/android-chrome-192x192.png", "src": "assets/icons/android-chrome-192x192.png",
@ -19,7 +19,7 @@
} }
], ],
"share_target": { "share_target": {
"action": "/share-target", "action": "./share-target",
"method": "GET", "method": "GET",
"params": { "params": {
"text": "text" "text": "text"