-
Notifications
You must be signed in to change notification settings - Fork 59.8k
using cache storage store image data #5013 #5061
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Dogtiti
merged 7 commits into
ChatGPTNextWeb:main
from
ConnectAI-E:feature-cache-storage
Jul 19, 2024
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
287fa0a
feat: 1. using cache storage store image data; 2. get base64image befβ¦
lloydzhou 1610b48
remove console.log
lloydzhou 7237d33
fix: ts type
Dogtiti ac470a6
Merge remote-tracking branch 'connectai/feature-cache-storage' into fβ¦
lloydzhou a765237
reload page when sw installed.
lloydzhou 052004d
using compressImage when serviceWorker register error
lloydzhou 862c2e8
hotfix for code review
lloydzhou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,59 @@ | ||
| const CHATGPT_NEXT_WEB_CACHE = "chatgpt-next-web-cache"; | ||
| const CHATGPT_NEXT_WEB_FILE_CACHE = "chatgpt-next-web-file"; | ||
| let a="useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";let nanoid=(e=21)=>{let t="",r=crypto.getRandomValues(new Uint8Array(e));for(let n=0;n<e;n++)t+=a[63&r[n]];return t}; | ||
|
|
||
| self.addEventListener("activate", function (event) { | ||
| console.log("ServiceWorker activated."); | ||
| }); | ||
|
|
||
| self.addEventListener("install", function (event) { | ||
| self.skipWaiting(); // enable new version | ||
| event.waitUntil( | ||
| caches.open(CHATGPT_NEXT_WEB_CACHE).then(function (cache) { | ||
| return cache.addAll([]); | ||
| }), | ||
| ); | ||
| }); | ||
|
|
||
| self.addEventListener("fetch", (e) => {}); | ||
| async function upload(request, url) { | ||
| const formData = await request.formData() | ||
| const file = formData.getAll('file')[0] | ||
| let ext = file.name.split('.').pop() | ||
| if (ext === 'blob') { | ||
| ext = file.type.split('/').pop() | ||
| } | ||
| const fileUrl = `${url.origin}/api/cache/${nanoid()}.${ext}` | ||
| // console.debug('file', file, fileUrl, request) | ||
| const cache = await caches.open(CHATGPT_NEXT_WEB_FILE_CACHE) | ||
| await cache.put(new Request(fileUrl), new Response(file, { | ||
| headers: { | ||
| 'content-type': file.type, | ||
| 'content-length': file.size, | ||
| 'cache-control': 'no-cache', // file already store in disk | ||
| 'server': 'ServiceWorker', | ||
| } | ||
| })) | ||
| return Response.json({ code: 0, data: fileUrl }) | ||
| } | ||
|
lloydzhou marked this conversation as resolved.
|
||
|
|
||
| async function remove(request, url) { | ||
| const cache = await caches.open(CHATGPT_NEXT_WEB_FILE_CACHE) | ||
| const res = await cache.delete(request.url) | ||
| return Response.json({ code: 0 }) | ||
| } | ||
|
|
||
| self.addEventListener("fetch", (e) => { | ||
| const url = new URL(e.request.url); | ||
| if (/^\/api\/cache/.test(url.pathname)) { | ||
| if ('GET' == e.request.method) { | ||
| e.respondWith(caches.match(e.request)) | ||
| } | ||
| if ('POST' == e.request.method) { | ||
| e.respondWith(upload(e.request, url)) | ||
| } | ||
| if ('DELETE' == e.request.method) { | ||
| e.respondWith(remove(e.request, url)) | ||
| } | ||
| } | ||
| }); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,27 @@ | ||
| if ('serviceWorker' in navigator) { | ||
| window.addEventListener('load', function () { | ||
| window.addEventListener('DOMContentLoaded', function () { | ||
| navigator.serviceWorker.register('/serviceWorker.js').then(function (registration) { | ||
| console.log('ServiceWorker registration successful with scope: ', registration.scope); | ||
| const sw = registration.installing || registration.waiting | ||
| if (sw) { | ||
| sw.onstatechange = function() { | ||
| if (sw.state === 'installed') { | ||
| // SW installed. Reload for SW intercept serving SW-enabled page. | ||
| console.log('ServiceWorker installed reload page'); | ||
| window.location.reload(); | ||
| } | ||
| } | ||
| } | ||
| registration.update().then(res => { | ||
| console.log('ServiceWorker registration update: ', res); | ||
| }); | ||
| window._SW_ENABLED = true | ||
| }, function (err) { | ||
| console.error('ServiceWorker registration failed: ', err); | ||
| }); | ||
| navigator.serviceWorker.addEventListener('controllerchange', function() { | ||
| console.log('ServiceWorker controllerchange '); | ||
| window.location.reload(true); | ||
| }); | ||
| }); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.