-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor(utils): emoji encoded svg loading * chore(env): added launch.json for debugging * fix(api): prefetch image content to avoid satori inflightRequests cache memory leak
- Loading branch information
Showing
7 changed files
with
131 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
{ | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Next.js: debug server-side", | ||
"type": "node-terminal", | ||
"request": "launch", | ||
"command": "npm run dev" | ||
}, | ||
{ | ||
"name": "Next.js: debug client-side", | ||
"type": "chrome", | ||
"request": "launch", | ||
"url": "http://localhost:3000" | ||
}, | ||
{ | ||
"name": "Next.js: debug full stack", | ||
"type": "node", | ||
"request": "launch", | ||
"program": "${workspaceFolder}/node_modules/.bin/next", | ||
"runtimeArgs": [ | ||
"--inspect" | ||
], | ||
"skipFiles": [ | ||
"<node_internals>/**" | ||
], | ||
"serverReadyAction": { | ||
"action": "debugWithChrome", | ||
"killOnServerStop": true, | ||
"pattern": "- Local:.+(https?://.+)", | ||
"uriFormat": "%s", | ||
"webRoot": "${workspaceFolder}" | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,46 +33,53 @@ export function getIconCode(char: string) { | |
return toCodePoint(!char.includes(U200D) ? char.replace(UFE0Fg, '') : char); | ||
} | ||
|
||
export const apis = { | ||
twemoji: (code: string) => | ||
`https://cdnjs.cloudflare.com/ajax/libs/twemoji/14.0.2/svg/${code.toLowerCase()}.svg`, | ||
openmoji: 'https://cdn.jsdelivr.net/npm/@svgmoji/[email protected]/svg/', | ||
blobmoji: 'https://cdn.jsdelivr.net/npm/@svgmoji/[email protected]/svg/', | ||
noto: 'https://cdn.jsdelivr.net/gh/svgmoji/svgmoji/packages/svgmoji__noto/svg/', | ||
fluent: (code: string) => | ||
`https://cdn.jsdelivr.net/gh/shuding/fluentui-emoji-unicode/assets/${code.toLowerCase()}_color.svg`, | ||
fluentFlat: (code: string) => | ||
`https://cdn.jsdelivr.net/gh/shuding/fluentui-emoji-unicode/assets/${code.toLowerCase()}_flat.svg`, | ||
}; | ||
|
||
const emojiCache: Record<string, Promise<string>> = {}; | ||
|
||
export async function loadEmoji(type: keyof typeof apis, code: string) { | ||
const key = `${type}:${code}`; | ||
|
||
if (key in emojiCache) { | ||
return emojiCache[key]!; | ||
} | ||
|
||
let selectedType = type; | ||
export enum EmojiType { | ||
TWEMOJI = 'twemoji', | ||
OPENOJI = 'openmoji', | ||
BLOBMOJI = 'blobmoji', | ||
NOTO = 'noto', | ||
FLUENT = 'fluent', | ||
FLUENT_FLAT = 'fluentFlat', | ||
} | ||
|
||
if (!type || !apis[type]) { | ||
selectedType = 'twemoji'; | ||
function getEmojiImageUrl(code: string, type: EmojiType): string { | ||
switch (type) { | ||
case 'twemoji': | ||
return `https://cdnjs.cloudflare.com/ajax/libs/twemoji/14.0.2/svg/${code.toLowerCase()}.svg`; | ||
case 'openmoji': | ||
return `https://cdn.jsdelivr.net/npm/@svgmoji/[email protected]/svg/${code.toUpperCase()}.svg`; | ||
case 'blobmoji': | ||
return `https://cdn.jsdelivr.net/npm/@svgmoji/[email protected]/svg/${code.toUpperCase()}.svg`; | ||
case 'noto': | ||
return `https://cdn.jsdelivr.net/gh/svgmoji/svgmoji/packages/svgmoji__noto/svg/${code.toUpperCase()}.svg`; | ||
case 'fluent': | ||
return `https://cdn.jsdelivr.net/gh/shuding/fluentui-emoji-unicode/assets/${code.toLowerCase()}_color.svg`; | ||
case 'fluentFlat': | ||
return `https://cdn.jsdelivr.net/gh/shuding/fluentui-emoji-unicode/assets/${code.toLowerCase()}_flat.svg`; | ||
default: | ||
return `https://cdnjs.cloudflare.com/ajax/libs/twemoji/14.0.2/svg/${code.toLowerCase()}.svg`; | ||
} | ||
} | ||
|
||
const api = apis[selectedType]; | ||
const cachedEmojis = new Map<string, string>(); | ||
|
||
let emojiPromise: Promise<string>; | ||
export async function loadEmojiSVGEncoded( | ||
type: EmojiType, | ||
code: string, | ||
): Promise<string> { | ||
const key = `${type}:${code}`; | ||
|
||
if (typeof api === 'function') { | ||
emojiPromise = fetch(api(code)).then(async (r) => r.text()); | ||
} else { | ||
emojiPromise = fetch(`${api}${code.toUpperCase()}.svg`).then(async (r) => | ||
r.text(), | ||
); | ||
if (cachedEmojis.has(key)) { | ||
return cachedEmojis.get(key)!; | ||
} | ||
|
||
emojiCache[key] = emojiPromise; // Storing the promise in the cache | ||
const emojiImageURL = getEmojiImageUrl(code, type); | ||
const emojiSVGEncoded = await fetch(emojiImageURL) | ||
.then((res) => res.arrayBuffer()) | ||
.then((content) => Buffer.from(content)) | ||
.then((buff) => `data:image/svg+xml;base64,${buff.toString('base64')}`); | ||
|
||
cachedEmojis.set(key, emojiSVGEncoded); | ||
|
||
return emojiPromise; // Returning the promise | ||
return emojiSVGEncoded; | ||
} |