Stolz / Assets

An ultra-simple-to-use assets management library for PHP

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unable to use Google Fonts API

Joshua-F opened this issue · comments

When trying to autoload a Google Fonts font I noticed it wasn't being included into the css array. I found that the reason for this is that Assets uses the file extension to determine if it's a css file or javascript file, which doesn't work with a Google Fonts API link since it doesn't even have a file extension.

Example: These two fonts will not autoload due to the link not having an actual file extension.

$config = array(
    ...
    'collections' => array(
        'fonts' => [
            '//fonts.googleapis.com/css?family=Roboto:100,300,500',
            '//fonts.googleapis.com/css?family=Lato:900'
        ]
    ),
    'autoload' => array(
        'fonts'
    )
    ...
)
commented

Hi @Joshua-F.

Simply add to the asset URL one last attribute with the extension to trick the autodetecting function. Google APIs will just ignore it.

'fonts' => [
    '//fonts.googleapis.com/css?family=Roboto:100,300,500&ext=.css',
    '//fonts.googleapis.com/css?family=Lato:900&ext=.css'
]

Another workaround would be to create a local CSS file with @import url("//fonts.googleapis.com/css?family=Roboto:100,300,500"); and include your local file in the configuration although this would imply the overhead of one extra HTTP request.

commented

@Joshua-F I just changed how the asset type is determined to a much more flexible approach.

Now instead of using the extension the filename/url is matched against a regex pattern than can be customized via config options. So now you decide what is a JS/CSS asset, not me or the extension.

You can see the default values for the regexs in the API docs.