From bdf17fafff02733c76b8977b2c19bb87db0864d1 Mon Sep 17 00:00:00 2001 From: "jianjian.ma" Date: Mon, 27 Mar 2023 17:41:44 +0800 Subject: [PATCH] feat: add PWA support --- app/layout.tsx | 1 + public/serviceWorker.js | 35 +++++++++++++++++++++++++++++++++++ public/site.webmanifest | 22 +++++++++++++++++++++- 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 public/serviceWorker.js diff --git a/app/layout.tsx b/app/layout.tsx index 095d3a184..3a6315f7f 100644 --- a/app/layout.tsx +++ b/app/layout.tsx @@ -50,6 +50,7 @@ export default function RootLayout({ href="https://fonts.googleapis.com/css2?family=Noto+Sans+SC:wght@300;400;700;900&display=swap" rel="stylesheet" > + {children} diff --git a/public/serviceWorker.js b/public/serviceWorker.js new file mode 100644 index 000000000..dff7af9a5 --- /dev/null +++ b/public/serviceWorker.js @@ -0,0 +1,35 @@ +const CHATGPT_NEXT_WEB_CACHE = "chatgpt-next-web-cache"; + +self.addEventListener('activate', function (event) { + console.log('ServiceWorker activated.'); +}); + +self.addEventListener('install', function (event) { + event.waitUntil( + caches.open(CHATGPT_NEXT_WEB_CACHE) + .then(function (cache) { + return cache.addAll([ + '/', + ]); + }) + ); +}); + +self.addEventListener('fetch', function (event) { + event.respondWith( + caches.match(event.request) + .then(function (response) { + return response || fetch(event.request); + }) + ); +}); + +if ('serviceWorker' in navigator) { + window.addEventListener('load', function () { + navigator.serviceWorker.register('/serviceWorker.js').then(function (registration) { + console.log('ServiceWorker registration successful with scope: ', registration.scope); + }, function (err) { + console.error('ServiceWorker registration failed: ', err); + }); + }); +} \ No newline at end of file diff --git a/public/site.webmanifest b/public/site.webmanifest index 45dc8a206..117f33b86 100644 --- a/public/site.webmanifest +++ b/public/site.webmanifest @@ -1 +1,21 @@ -{"name":"","short_name":"","icons":[{"src":"/android-chrome-192x192.png","sizes":"192x192","type":"image/png"},{"src":"/android-chrome-512x512.png","sizes":"512x512","type":"image/png"}],"theme_color":"#ffffff","background_color":"#ffffff","display":"standalone"} \ No newline at end of file +{ + "name": "ChatGPT Next Web", + "short_name": "ChatGPT", + "icons": [ + { + "src": "/android-chrome-192x192.png", + "sizes": "192x192", + "type": "image/png" + }, + { + "src": "/android-chrome-512x512.png", + "sizes": "512x512", + "type": "image/png" + } + ], + "start_url": "/", + "theme_color": "#ffffff", + "background_color": "#ffffff", + "display": "standalone" + } + \ No newline at end of file