Skip to content

Commit b132dfa

Browse files
author
tung-hpzbook
committed
.
1 parent cf659f8 commit b132dfa

1 file changed

Lines changed: 60 additions & 49 deletions

File tree

src/serviceworker.js

Lines changed: 60 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,18 @@ const urlsToCache = [
1111
'/index.css',
1212
'/helpers.js',
1313
'/s3.js',
14+
'/git.js',
1415
'/nostr.js',
1516
'/gdrive.js',
1617
'/manifest.json',
1718
'/favicon.ico',
1819
'/favicon.png',
19-
'/libs/diff_match_patch.js',
2020
'/icons/icons.json',
21+
'/libs/diff_match_patch.js',
2122
'/libs/aws-sdk-2.1692.0.min.js',
23+
'/libs/isomorphic-git.min.js',
24+
'/libs/lightning-fs.min.js',
25+
'/libs/http.min.js',
2226
'https://maxcdn.bootstrapcdn.com/font-awesome/latest/css/font-awesome.min.css',
2327
'https://maxcdn.bootstrapcdn.com/font-awesome/latest/fonts/fontawesome-webfont.woff2?v=4.7.0',
2428
'https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4',
@@ -122,64 +126,71 @@ self.addEventListener('fetch', (event) => {
122126
if (event.request.method === 'POST' && url.pathname === '/share') {
123127
event.respondWith(
124128
(async () => {
125-
try {
126-
const TITLE_SHARED = 'Shared Inbox';
127-
128-
const formData = await event.request.formData();
129-
const text = formData.get('text') || '';
130-
const sharedUrl = formData.get('url') || '';
131-
const title = formData.get('title') || '';
132-
133-
let newItem = '- [ ] ';
134-
if (title && sharedUrl) {
135-
newItem += `[${title}](${sharedUrl})`;
136-
} else if (title) {
137-
newItem += title;
138-
} else if (sharedUrl) {
139-
newItem += `[${sharedUrl}](${sharedUrl})`;
140-
}
129+
const formDataPromise = event.request.formData();
141130

142-
if (text) {
143-
if (title || sharedUrl) {
144-
newItem += ` > ${text}`;
145-
} else {
146-
newItem += text;
131+
event.waitUntil((async () => {
132+
try {
133+
const TITLE_SHARED = 'Shared Inbox';
134+
const formData = await formDataPromise;
135+
136+
const text = formData.get('text') || '';
137+
const sharedUrl = formData.get('url') || '';
138+
const title = formData.get('title') || '';
139+
140+
let newItem = '- [ ] ';
141+
if (title && sharedUrl) {
142+
newItem += `[${title}](${sharedUrl})`;
143+
} else if (title) {
144+
newItem += title;
145+
} else if (sharedUrl) {
146+
newItem += `[${sharedUrl}](${sharedUrl})`;
147147
}
148-
}
149148

150-
if (newItem.trim() !== '*') {
151-
const allNotes = await getNotesDB();
152-
let inboxNote = allNotes.find(note => note.title === TITLE_SHARED);
149+
if (text) {
150+
if (title || sharedUrl) {
151+
newItem += ` > ${text}`;
152+
} else {
153+
newItem += text;
154+
}
155+
}
156+
157+
if (newItem.trim() !== '*') {
158+
const allNotes = await getNotesDB();
159+
let inboxNote = allNotes.find(note => note.title === TITLE_SHARED);
153160

154-
if (inboxNote) {
155-
if (inboxNote.content) {
156-
inboxNote.content = newItem + '\n' + inboxNote.content;
161+
if (inboxNote) {
162+
if (inboxNote.content) {
163+
inboxNote.content = newItem + '\n' + inboxNote.content;
164+
} else {
165+
inboxNote.content = newItem;
166+
}
167+
inboxNote.updatedAt = new Date().toISOString();
168+
await updateNoteDB(inboxNote);
157169
} else {
158-
inboxNote.content = newItem;
170+
// Create a new inbox note
171+
const newNote = {
172+
id: 'shared-inbox-' + generateUniqueId(),
173+
title: TITLE_SHARED,
174+
content: newItem,
175+
createdAt: new Date().toISOString(),
176+
updatedAt: new Date().toISOString(),
177+
tags: ['shared', 'inbox'],
178+
};
179+
await addNoteDB(newNote);
159180
}
160-
inboxNote.updatedAt = new Date().toISOString();
161-
await updateNoteDB(inboxNote);
162-
} else {
163-
// Create a new inbox note
164-
const newNote = {
165-
id: 'shared-inbox-' + generateUniqueId(),
166-
title: TITLE_SHARED,
167-
content: newItem,
168-
createdAt: new Date().toISOString(),
169-
updatedAt: new Date().toISOString(),
170-
tags: ['shared', 'inbox'],
171-
};
172-
await addNoteDB(newNote);
173181
}
182+
} catch (criticalError) {
183+
console.error('A critical error occurred in the /share handler:', criticalError);
174184
}
185+
})());
175186

176-
// Redirect to the home page after sharing
177-
return Response.redirect('/', 303);
178-
} catch (criticalError) {
179-
console.error('A critical error occurred in the /share handler:', criticalError);
180-
// Still attempt to redirect the user back to the app to prevent a hanging screen.
181-
return Response.redirect('/', 303);
187+
// Redirect immediately after ensuring we have the form data
188+
try {
189+
await formDataPromise;
190+
} catch (e) {
191+
// Proceed to redirect even if parsing fails, to avoid hanging
182192
}
193+
return Response.redirect('/', 303);
183194
})()
184195
);
185196
return;

0 commit comments

Comments
 (0)