Skip to content

Commit

Permalink
feat: intercept iconify api requests (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
userquin committed Apr 21, 2023
1 parent 4ef5448 commit 221f4d8
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ collections-info.json
collections-meta.json

dist-electron
dev-dist
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"postbuild": "esno scripts/postbuild.ts",
"lint": "eslint .",
"dev": "vite --port 3333 --open",
"dev-pwa": "SW_DEV=true vite --port 3333",
"typecheck": "vue-tsc --noEmit",
"dev:electron": "npm -C ./electron run dev",
"build": "NODE_ENV=production vite build",
Expand Down
7 changes: 6 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import { createApp } from 'vue'
import { createRouter, createWebHashHistory, createWebHistory } from 'vue-router'
import { disableCache } from 'iconify-icon'
import App from './App.vue'
import '@unocss/reset/tailwind.css'
import './utils/electron'
import './main.css'
import 'uno.css'
import 'iconify-icon'

import { basePath, isElectron } from './env'
import routes from '~pages'

// disable local storage cache when there is PWA:
// we need to keep local storage when running dev server without PWA
if (!isElectron && PWA)
disableCache('all')

const app = createApp(App)

const router = createRouter({
Expand Down
1 change: 1 addition & 0 deletions src/shims.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ interface Window {

declare const vscode: any
declare const __BUILD_TIME__: string
declare const PWA: boolean

declare module '*.vue' {
import type { defineComponent } from './vue'
Expand Down
42 changes: 42 additions & 0 deletions src/sw.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { cleanupOutdatedCaches, createHandlerBoundToURL, precacheAndRoute } from 'workbox-precaching'
import { clientsClaim } from 'workbox-core'
import { NavigationRoute, registerRoute } from 'workbox-routing'
import { getIcons } from '@iconify/utils'

declare let self: ServiceWorkerGlobalScope

// self.__WB_MANIFEST is default injection point
precacheAndRoute(self.__WB_MANIFEST)

// clean old assets
cleanupOutdatedCaches()

// to allow work offline
registerRoute(new NavigationRoute(
createHandlerBoundToURL('index.html'),
))

self.skipWaiting()
clientsClaim()

self.addEventListener('fetch', (e) => {
const url = e.request.url
const match = url.match(/^https:\/\/(api\.iconify\.design|api\.simplesvg\.com|api\.unisvg\.com)\/(.*)\.json\?icons=(.*)?/)
if (match) {
e.respondWith(
fetch(`/collections/${match[2]}-raw.json`)
.then((response) => {
return response.json()
}).then((collection) => {
return new Response(JSON.stringify(getIcons(
collection,
match[3].replaceAll('%2C', ',').split(','),
)), {
headers: {
'Content-Type': 'application/json',
},
})
}),
)
}
})
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"compilerOptions": {
"module": "esnext",
"target": "esnext",
"lib": ["DOM", "ESNext"],
"lib": ["DOM", "ESNext", "WebWorker"],
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
Expand Down
11 changes: 11 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ export default defineConfig(({ mode }) => {
dts: 'src/auto-imports.d.ts',
}),
!isElectron && VitePWA({
strategies: 'injectManifest',
srcDir: 'src',
filename: 'sw.ts',
registerType: 'autoUpdate',
manifest: {
name: 'Icônes',
short_name: 'Icônes',
Expand All @@ -84,11 +88,18 @@ export default defineConfig(({ mode }) => {
options.includeAssets = fg.sync('**/*.*', { cwd: join(process.cwd(), 'public'), onlyFiles: true })
},
},
devOptions: {
enabled: process.env.SW_DEV === 'true',
/* when using generateSW the PWA plugin will switch to classic */
type: 'module',
navigateFallback: 'index.html',
},
}),
UnoCSS(),
],
define: {
__BUILD_TIME__: JSON.stringify(dayjs().format('YYYY/MM/DD HH:mm')),
PWA: !isElectron && (process.env.NODE_ENV === 'production' || process.env.SW_DEV === 'true'),
},
resolve: {
alias: {
Expand Down

0 comments on commit 221f4d8

Please sign in to comment.