1 of 24

2 of 24

Custom plugins for Scully The Static Site Generator for Angular

Dutch Angular Group�25th of June, 2020

Sam Vloeberghs�@samvloeberghs

3 of 24

Sam Vloeberghs

  • Freelance Software Engineer @ Kwerri / Innovatus
  • Organiser of NG-BE & Angular Belgium
  • GDE for Angular

https://samvloeberghs.be

4 of 24

???

5 of 24

Scully - The Quick Overview

  1. Static Site Generator (SSG) for Angular
  2. Easy configuration
  3. Automated Route Discovery
    1. for static routes ( using guess-parser )
    2. for dynamic routes ( using Scully / custom plugins )
  4. Plugin system

6 of 24

Dynamic routes

Static Routes

  • Based on the Angular routing config
  • Auto extracted using guess-parser library included in Scully

Route Discovery Process

/ /about �/contact /users�/news /404

  • Based on the Scully routes config
  • Auto extracted / expanded using build-in and custom Router plugins

/news/update-1 /users/1�/news/update-2 /users/2

/news/update-x /users/y

7 of 24

Plugin System

  • New ways / strategies to prerender pages
  • Different types:

Router

Render

FileHandler

Route Discovery Done

AllDone

Content plugins

Helper plugins

8 of 24

Router Plugins

Examples:

  • /blog/:year/:month/:day/:slug
  • /news/:id/:slug
  • /users/:id
  • ...
  • Define how to handle dynamic router parameters
  • Where to get the data to expand those router parameters?

9 of 24

Router plugins

Handling the dynamic route /users/:id can be done via the Scully RouteTypes.json plugin

/users/1 /users/2 /users/3�/users/4 /users/5 /users/6

/users/7 /users/8 /users/..

This will return a list of all users that are available via their dynamic url:

10 of 24

Custom Router plugins

1

2

3

Handling a more complex route like /news/:id/:slug can be done via a custom Scully router plugin:

  1. Our route plugin gets the route and its config
  2. Read data from the given url
  3. Use the data to format the urls
  4. Collect and return the full list of urls

4

11 of 24

Registering plugin

1

2

3

Before we can use our custom plugin we need to register it with Scully:

  • Import registerPlugin from Scully
  • Register the plugin with its type, identifier and pluginFunction
  • Export the identifier for reference

12 of 24

Configuring plugin

  • Importing the News constant will execute the registerPlugin in the plugin file
  • Associate the News plugin with the configured dynamic route
  • The only configuration our plugin needs is the url / data source to retrieve the news data from.

1

2

3

13 of 24

Render plugins

Examples:

  • Inject 3rd party scripts ( HotJar, Google Analytics, .. )
  • Minify HTML
  • Generate Table of Contents
  • ...
  • Execute additional post rendering processes
  • Transform HTML output

14 of 24

Custom Render plugins

1

2

3

Execute your own post-processing logic on the HTML output

  • Define a function that transforms the HTML output
  • Register the plugin with its type, in this case a render plugin, identifier and pluginFunction
  • Export the identifier for reference

15 of 24

Configuring plugin

  • Importing the DemoBanner constant will execute the registerPlugin in the plugin file
  • Configure the DemoBanner plugin as a defaultPostRenderer, meaning it will execute for all discovered routes
  • This simple plugin requires no configuration

1

2

16 of 24

FileHandler plugins

  • Used by the internal Scully contentFolder plugin
  • Defines how to read specific files (and transform into HTML)
  • Two build-in fileHandler plugins
    • Markdown files
    • ASCII code files
  • “Easily” define your own

17 of 24

Generated from Markdown

18 of 24

RouteDiscoveryDone plugins

AllDone plugins

  • Called automatically after
    • all routes have been collected
    • all router plugins have been handled
  • Receive shallow copy of all discovered routes
  • Called after all Scully processes have finished

19 of 24

Community Plugins ( 1/2)

  • A growing set of community plugins is available
  • Use the “scully-plugin-” prefix for naming your plugin
    • Makes it easy discoverable

20 of 24

Community Plugins ( 2/2)

  • scully-plugin-minify-html (render)
  • scully-plugin-disable-angular (render)
  • scully-plugin-sitemap (routeDiscoveryDone)
  • scully-plugin-toc (render)
  • scully-plugin-google-gtag (render)
  • scully-plugin-html-regex (render)
  • ..

21 of 24

DEMO

22 of 24

Conclusion

  • Scully is a great tool that is still evolving
  • A big part of its flexibility is the plugin system
    • Custom ways of discovering dynamic urls
    • Post rendering / transformations of HTML
    • Handle different content file types
    • Post processing hooks for cleanup / additional work
  • Make use of community plugins
    • Share your own plugin

23 of 24

More reading

24 of 24

Custom plugins for Scully

The Static Site Generator for Angular �

@ Dutch Angular Group�25th of June, 2020

Thank you for your attention. Feel free to contact me at sam.vloeberghs@gmail.com or directly on Twitter @samvloeberghs

QUESTIONS?