@@ -133,9 +133,32 @@ self.addEventListener('fetch', (event) => {
133133 const TITLE_SHARED = 'Shared Inbox' ;
134134 const formData = await formDataPromise ;
135135
136- const text = formData . get ( 'text' ) || '' ;
137- const sharedUrl = formData . get ( 'url' ) || '' ;
138- const title = formData . get ( 'title' ) || '' ;
136+ let text = formData . get ( 'text' ) || '' ;
137+ let title = formData . get ( 'title' ) || '' ;
138+ let sharedUrl = formData . get ( 'url' ) || '' ;
139+
140+ title = title . replace ( / \n / g, ' ' ) ;
141+
142+ const urlRegex = / ( h t t p s ? : \/ \/ [ ^ \s ] + ) / g;
143+
144+ if ( ! sharedUrl ) {
145+ // Try to find URL in text or title if not explicitly provided
146+ const textUrlMatch = text . match ( urlRegex ) ;
147+ const titleUrlMatch = title . match ( urlRegex ) ;
148+
149+ if ( textUrlMatch ) {
150+ sharedUrl = textUrlMatch [ 0 ] ;
151+ } else if ( titleUrlMatch ) {
152+ sharedUrl = titleUrlMatch [ 0 ] ;
153+ } else {
154+ // Fallback: try decoding if it looks like an encoded URL
155+ try {
156+ const decodedText = decodeURIComponent ( text ) ;
157+ const decodedMatch = decodedText . match ( urlRegex ) ;
158+ if ( decodedMatch ) sharedUrl = decodedMatch [ 0 ] ;
159+ } catch ( e ) { }
160+ }
161+ }
139162
140163 let newItem = '- [ ] ' ;
141164 if ( title && sharedUrl ) {
@@ -147,6 +170,10 @@ self.addEventListener('fetch', (event) => {
147170 }
148171
149172 if ( text ) {
173+ if ( text . includes ( '\n' ) ) {
174+ text = '\n```\n' + text + '\n```\n' ;
175+ }
176+
150177 if ( title || sharedUrl ) {
151178 newItem += ` > ${ text } ` ;
152179 } else {
@@ -197,28 +224,30 @@ self.addEventListener('fetch', (event) => {
197224 }
198225 }
199226
200- // For all other requests, use the network-first strategy.
227+ // For all other requests, use Stale-While-Revalidate strategy.
201228 event . respondWith (
202- fetch ( event . request )
203- . then ( ( networkResponse ) => {
204- if (
205- networkResponse &&
206- networkResponse . status === 200 &&
207- event . request . method === 'GET' &&
208- ( event . request . url . startsWith ( 'http' ) || event . request . url . startsWith ( 'https' ) )
209- ) {
210- const responseToCache = networkResponse . clone ( ) ;
211- caches . open ( CACHE_NAME ) . then ( ( cache ) => {
212- cache . put ( event . request , responseToCache ) . catch ( err => {
213- console . warn ( `Failed to cache ${ event . request . url } :` , err ) ;
214- } ) ;
215- } ) ;
229+ caches . open ( CACHE_NAME ) . then ( ( cache ) => {
230+ return cache . match ( event . request ) . then ( ( cachedResponse ) => {
231+ const fetchPromise = fetch ( event . request ) . then ( ( networkResponse ) => {
232+ if (
233+ networkResponse &&
234+ networkResponse . status === 200 &&
235+ event . request . method === 'GET' &&
236+ ( event . request . url . startsWith ( 'http' ) || event . request . url . startsWith ( 'https' ) )
237+ ) {
238+ cache . put ( event . request , networkResponse . clone ( ) ) ;
239+ }
240+ return networkResponse ;
241+ } ) ;
242+
243+ if ( cachedResponse ) {
244+ event . waitUntil ( fetchPromise . catch ( ( ) => { } ) ) ;
245+ return cachedResponse ;
216246 }
217- return networkResponse ;
218- } )
219- . catch ( ( ) => {
220- return caches . match ( event . request ) ;
221- } )
247+
248+ return fetchPromise ;
249+ } ) ;
250+ } )
222251 ) ;
223252} ) ;
224253
0 commit comments