OleVik / grav-plugin-static-generator

Indexing and static generation of Page(s) for Grav.
MIT License
22 stars 1 forks source link

Handle external assets #4

Closed SteveMcArthur closed 4 years ago

SteveMcArthur commented 4 years ago

I have some external assets in my project such as:

https://fonts.googleapis.com/css?family=Lato:300,300i,400,400i,700,700i
https://unpkg.com/leaflet@1.4.0/dist/leaflet.js

I think there needs to be a bit of code to check for "http" at the beginning of the string in the filename.

I've handled it at the moment by editing the Assets.php module and modifying that catch clause:

            } catch (\Exception $e) {
                $exceptionClass = get_class($e);

                if($exceptionClass == "Symfony\Component\Filesystem\Exception\FileNotFoundException"){
                    if(substr($asset,0,4) == "http"){
                        $result->addRow(
                            [
                            '<magenta> External Asset: '. $asset . '<magenta>'
                            ]
                        );

                    }else {
                        $result->addRow(
                            [
                            '<magenta> !! NOT FOUND '. GRAV_ROOT. $asset . '<magenta>'
                            ]
                        );
                    }
                }else {
                    throw new \Exception($e);
                }

            }

Basically if the string begins with "http" do nothing and just continue iterating.

I also think that an exception should not be thrown if a file doesn't exist - just a message to the console window. It's just a bit of a pain if the whole generation process fails just because of a missing js or css file.

OleVik commented 4 years ago

The upcoming v2.0.0-refactor will support this, as well as the option to download and store the asset locally.

OleVik commented 4 years ago

Implemented for v2.0.0-beta.1.

OleVik commented 4 years ago

Just to note: The plugin will only treat assets added to Grav's Asset Manager (ie., addJs(), addCss(), etc), as accounting for other varieties would leave too many edge-cases. Assets added manually with a script-element or indirectly via a CSS-file are thus ignored.

From v2.0.0 the --offline parameter will ignore remote assets entirely, but otherwise download and store them.