|
| 1 | +// Copied from |
| 2 | +// https://github.com/nksaraf/fully-react/blob/4f738132a17d94486c8da19d8729044c3998fc54/packages/fully-react/src/shared/assets.tsx |
| 3 | +// And then modified to work with our codebase |
| 4 | + |
| 5 | +import React, { use } from 'react' |
| 6 | + |
| 7 | +const linkProps = [ |
| 8 | + ['js', { rel: 'modulepreload', crossOrigin: '' }], |
| 9 | + ['jsx', { rel: 'modulepreload', crossOrigin: '' }], |
| 10 | + ['ts', { rel: 'modulepreload', crossOrigin: '' }], |
| 11 | + ['tsx', { rel: 'modulepreload', crossOrigin: '' }], |
| 12 | + ['css', { rel: 'stylesheet', precedence: 'high' }], |
| 13 | + ['woff', { rel: 'preload', as: 'font', type: 'font/woff', crossOrigin: '' }], |
| 14 | + [ |
| 15 | + 'woff2', |
| 16 | + { rel: 'preload', as: 'font', type: 'font/woff2', crossOrigin: '' }, |
| 17 | + ], |
| 18 | + ['gif', { rel: 'preload', as: 'image', type: 'image/gif' }], |
| 19 | + ['jpg', { rel: 'preload', as: 'image', type: 'image/jpeg' }], |
| 20 | + ['jpeg', { rel: 'preload', as: 'image', type: 'image/jpeg' }], |
| 21 | + ['png', { rel: 'preload', as: 'image', type: 'image/png' }], |
| 22 | + ['webp', { rel: 'preload', as: 'image', type: 'image/webp' }], |
| 23 | + ['svg', { rel: 'preload', as: 'image', type: 'image/svg+xml' }], |
| 24 | + ['ico', { rel: 'preload', as: 'image', type: 'image/x-icon' }], |
| 25 | + ['avif', { rel: 'preload', as: 'image', type: 'image/avif' }], |
| 26 | + ['mp4', { rel: 'preload', as: 'video', type: 'video/mp4' }], |
| 27 | + ['webm', { rel: 'preload', as: 'video', type: 'video/webm' }], |
| 28 | +] as const |
| 29 | + |
| 30 | +type Linkprop = (typeof linkProps)[number][1] |
| 31 | + |
| 32 | +const linkPropsMap = new Map<string, Linkprop>(linkProps) |
| 33 | + |
| 34 | +/** |
| 35 | + * Generates a link tag for a given file. This will load stylesheets and preload |
| 36 | + * everything else. It uses the file extension to determine the type. |
| 37 | + */ |
| 38 | +export const Asset = ({ file }: { file: string }) => { |
| 39 | + const ext = file.split('.').pop() |
| 40 | + const props = ext ? linkPropsMap.get(ext) : null |
| 41 | + |
| 42 | + if (!props) { |
| 43 | + return null |
| 44 | + } |
| 45 | + |
| 46 | + return <link href={file} {...props} /> |
| 47 | +} |
| 48 | + |
| 49 | +export function Assets() { |
| 50 | + // TODO (RSC) Currently we only handle server assets. |
| 51 | + // Will probably need to handle client assets as well. |
| 52 | + // Do we also need special code for SSR? |
| 53 | + // if (isClient) return <ClientAssets /> |
| 54 | + |
| 55 | + // @ts-expect-error Need experimental types here for this to work |
| 56 | + return <ServerAssets /> |
| 57 | +} |
| 58 | + |
| 59 | +const findAssets = async () => { |
| 60 | + return [...new Set([...(await rwRscGlobal.findAssets(''))]).values()] |
| 61 | +} |
| 62 | + |
| 63 | +const AssetList = ({ assets }: { assets: string[] }) => { |
| 64 | + return ( |
| 65 | + <> |
| 66 | + {assets.map((asset) => { |
| 67 | + return <Asset file={asset} key={asset} /> |
| 68 | + })} |
| 69 | + </> |
| 70 | + ) |
| 71 | +} |
| 72 | + |
| 73 | +async function ServerAssets() { |
| 74 | + const allAssets = await findAssets() |
| 75 | + |
| 76 | + return <AssetList assets={allAssets} /> |
| 77 | +} |
| 78 | + |
| 79 | +export function ClientAssets() { |
| 80 | + const allAssets = use(findAssets()) |
| 81 | + |
| 82 | + return <AssetList assets={allAssets} /> |
| 83 | +} |
0 commit comments