Skip to content

Commit

Permalink
🚀 feat: simplify form data handling in service worker and update shar…
Browse files Browse the repository at this point in the history
…e target enctype to multipart/form-data
  • Loading branch information
vaayne committed Dec 29, 2024
1 parent 52ac82f commit 1b05ebb
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 15 deletions.
16 changes: 2 additions & 14 deletions web/src/sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,7 @@ self.addEventListener("fetch", (event) => {
) {
event.respondWith(
(async () => {
let formData;
// Handle both form encodings
if (
event.request.headers
.get("content-type")
?.includes("application/x-www-form-urlencoded")
) {
const formText = await event.request.text();
const params = new URLSearchParams(formText);
formData = params;
} else {
formData = await event.request.formData();
}

const formData = await event.request.formData();
const link = formData.get("url") || "";
const title = formData.get("title") || "";
const text = formData.get("text") || "";
Expand All @@ -40,6 +27,7 @@ self.addEventListener("fetch", (event) => {

try {
const resp = await saveBookmark(link);
console.log("Bookmark saved:", resp);
return Response.redirect("/bookmarks", 303);
} catch (error) {
return new Response(error.message, {
Expand Down
3 changes: 2 additions & 1 deletion web/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default defineConfig({
manifest: {
name: "Vibrain",
short_name: "Vibrain",
display: "fullscreen",
description: "Save what matters, recall when it counts.",
theme_color: "#ffffff",
icons: [
Expand Down Expand Up @@ -51,7 +52,7 @@ export default defineConfig({
share_target: {
action: "/save-bookmark",
method: "POST",
enctype: "application/x-www-form-urlencoded",
enctype: "multipart/form-data",
params: {
title: "title",
text: "text",
Expand Down

0 comments on commit 1b05ebb

Please sign in to comment.