Skip to content

Commit 7602853

Browse files
authored
fix icon cache behavior breaking sw updates (#117)
* fix icon cache behavior breaking sw updates * icon fallback
1 parent 2ec9a2f commit 7602853

File tree

3 files changed

+33
-12
lines changed

3 files changed

+33
-12
lines changed

app/src/serviceWorker.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,23 @@ const fallbackToOfflineCachePlugin: WorkboxPlugin = {
4040
},
4141
};
4242

43+
const iconPathMatch = /\/assets\/icon-\w+\.js/;
44+
// Cache icon files as they are fetched. we don't precache all icons
45+
// since there are hundreds of them and they are not all used in the app.
46+
// We just cache the ones that are used.
47+
registerRoute(
48+
// Add in any other file extensions or routing criteria as needed.
49+
({ url }) => url.origin === import.meta.env.VITE_PUBLIC_API_ORIGIN && iconPathMatch.test(url.pathname),
50+
new StaleWhileRevalidate({
51+
cacheName: 'icons',
52+
plugins: [
53+
// Ensure that once this runtime cache reaches a maximum size the
54+
// least-recently used files are removed.
55+
new ExpirationPlugin({ maxEntries: 200 }),
56+
],
57+
})
58+
);
59+
4360
// Cache model files as they are fetched
4461
const modelPathMatch = /\/furniture\/.+\/model$/;
4562
registerRoute(

app/vite.config.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,10 @@ export default defineConfig({
111111
output: {
112112
chunkFileNames(chunkInfo) {
113113
// mark dynamically imported icon chunks with a filename prefix so they can be handled differently
114-
// by the PWA precache -- there are a lot of them.
114+
// by the PWA precache -- there are a lot of them. No hashes so that they remain resolvable across builds
115+
// even if they weren't cached, since we are not precaching all of them.
115116
if (chunkInfo.moduleIds.some((id) => id.includes('lucide-react/dist/esm/icons/'))) {
116-
return 'assets/icon-[name]-[hash].js';
117+
return 'assets/icon-[name].js';
117118
}
118119
return 'assets/[name]-[hash].js';
119120
},

packages/sys/src/components/icon/Icon.tsx

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
import { forwardRef, lazy, Suspense, SVGAttributes, useContext } from 'react';
2-
import { IconName, LucideIconName } from './iconNames.js';
3-
import { CustomIconName, customIcons } from './customGlyphs/index.js';
4-
import dynamicIconImports from 'lucide-react/dynamicIconImports.js';
5-
import clsx from 'clsx';
6-
import { ICON_CLASS_NAME } from './constants.js';
71
import { Slot } from '@radix-ui/react-slot';
8-
import cls from './Icon.module.css';
2+
import clsx from 'clsx';
3+
import dynamicIconImports from 'lucide-react/dynamicIconImports.js';
4+
import { forwardRef, lazy, Suspense, SVGAttributes, useContext } from 'react';
95
import { ButtonContext } from '../button/ButtonContext.js';
6+
import { ErrorBoundary } from '../errorBoundary/ErrorBoundary.js';
107
import { Spinner } from '../spinner/Spinner.js';
8+
import { ICON_CLASS_NAME } from './constants.js';
9+
import { CustomIconName, customIcons } from './customGlyphs/index.js';
10+
import cls from './Icon.module.css';
11+
import { IconName, LucideIconName } from './iconNames.js';
1112

1213
export type { IconName } from './iconNames.js';
1314

@@ -55,9 +56,11 @@ export const Icon = forwardRef<SVGSVGElement, IconProps>(function Icon({ name, c
5556
const LucideIcon = lazy(dynamicIconImports[name as LucideIconName]);
5657

5758
return (
58-
<Suspense fallback={<IconFallback />}>
59-
<LucideIcon {...rest} absoluteStrokeWidth={absoluteStrokeWidth} className={clsx(ICON_CLASS_NAME, cls.icon, className)} ref={ref} />
60-
</Suspense>
59+
<ErrorBoundary fallback={<IconFallback ref={ref} />}>
60+
<Suspense fallback={<IconFallback ref={ref} />}>
61+
<LucideIcon {...rest} absoluteStrokeWidth={absoluteStrokeWidth} className={clsx(ICON_CLASS_NAME, cls.icon, className)} ref={ref} />
62+
</Suspense>
63+
</ErrorBoundary>
6164
);
6265
});
6366

0 commit comments

Comments
 (0)