astro-google-fonts-optimizer icon indicating copy to clipboard operation
astro-google-fonts-optimizer copied to clipboard

Added cache and retry attemps on fail

Open Herob527 opened this issue 11 months ago • 1 comments

Partially fixes this issue: https://github.com/sebholstein/astro-google-fonts-optimizer/issues/1 with in-memory cache

Also sometimes I had crashes for unknown reasons during dev and build so I added that it'll retry refetching url in case of failure three times by default

npm package for testing: https://www.npmjs.com/package/@herob191/astro-google-fonts-optimizer

Also I wonder if this

const contents = await Promise.all(
    urls.map(async (url) => {
        try {
            const css = await downloadFontCSS(url);
            return { css }
        } catch (err) {
            return { url }
        }
    })
)

Can be replaced with this

const contents = await Promise.all(
    urls.map(async (url) => {
        
            const css = await downloadFontCSS(url);
            return { css }
    })
)

And then this

{contents.map(content => {
    if (content.css) {
        return (
            <>
                <style set:html={content.css} is:inline>
                </style>
            </>
        )
    }

    return <link href={content.url} rel="stylesheet" />
})}

with this:

{contents.map(content => (
            <>
                <style set:html={content.css} is:inline>
                </style>
            </>
        
)}

Or is there more purpouse to it I don't see yet?

Herob527 avatar Mar 21 '24 17:03 Herob527

Or is there more purpouse to it I don't see yet?

The general idea was: when the network fails, we don't want to fail the build and simple load the fonts via the given URL.

sebholstein avatar Apr 12 '24 20:04 sebholstein