Skip to content

Commit

Permalink
feat: sw and manifest
Browse files Browse the repository at this point in the history
  • Loading branch information
davay42 committed Jan 18, 2025
1 parent ce38e58 commit 9317e01
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 0 deletions.
Binary file added public/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
99 changes: 99 additions & 0 deletions public/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions public/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"id": "chromatone-pitch-table",
"name": "Pitch Table",
"short_name": "Pitch Table",
"description": "An expandable table of all possible notes to make sounds and rhythms",
"start_url": "/",
"background_color": "#999",
"theme_color": "#999",
"display": "standalone",
"orientation": "any",
"display_override": ["window-controls-overlay", "minimal-ui", "standalone"],
"handle_links": "preferred",
"prefer_related_applications": false,
"categories": ["music", "audio", "tools"],
"icons": [
{
"src": "./logo.svg",
"sizes": "any",
"type": "image/svg+xml",
"purpose": "any"
},
{
"src": "./logo.png",
"sizes": "1600x1600",
"type": "image/png",
"purpose": "any"
}
],

"launch_handler": {
"client_mode": ["focus-existing", "auto"]
}
}
43 changes: 43 additions & 0 deletions public/sw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const CACHE_NAME = 'chromatone-random-jam-v.0.2.0';
const ASSETS_TO_CACHE = [
'/',
'/index.html',
'/logo.svg',
'102_logo.svg'
];

// Install Service Worker
self.addEventListener('install', (event) => {
self.skipWaiting()
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => {
return cache.addAll(ASSETS_TO_CACHE);
})
);
});

// Activate Service Worker
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((cacheNames) => {
return Promise.all(
cacheNames.map((cacheName) => {
if (cacheName !== CACHE_NAME) {
return caches.delete(cacheName); // Clean up old caches
}
})
);
})
);
self.clients.claim();
});

// Fetch assets (serve from cache or network)
self.addEventListener('fetch', (event) => {
event.respondWith(
caches.match(event.request).then((cachedResponse) => {
// Return cached response if available, otherwise fetch from network
return cachedResponse || fetch(event.request);
})
);
});
27 changes: 27 additions & 0 deletions vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,30 @@ export default defineConfig({
}),
],
})


function viteBuildScript() {
return {
name: 'vite-build-script',
transformIndexHtml(html) {
if (process.env.NODE_ENV === 'production') {
return html.replace(/<!-- Stats production build insert -->/, `<script async defer src="https://stats.chromatone.center/script.js" data-website-id="828fbb1d-0f47-4a05-886a-4b50cfe8ba02"></script>
<script>
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
navigator.serviceWorker.register('/sw.js')
.then((registration) => {
console.log('Service Worker registered with scope: ', registration.scope);
})
.catch((error) => {
console.error('Service Worker registration failed: ', error);
});
});
}
</script>`);
}
return html;
},
};
}

0 comments on commit 9317e01

Please sign in to comment.