swup / fragment-plugin

A swup plugin for dynamically replacing containers based on rules 🧩
https://swup-fragment-plugin.netlify.app
MIT License
15 stars 1 forks source link

feat: modify rules on the fly #58

Closed hirasso closed 10 months ago

hirasso commented 10 months ago

Description

On large websites or in complex cases it could come in handy to be able to modify fragment rules on the fly. This PR is adding a few new API functions:


/** Keep a reference to fragment plugin during instanciation */
const fragmentPlugin = new SwupFragmentPlugin({ rules });
new Swup({ plugins: [fragmentPlugin] });

/** Higher-Level methods: */

/** Prepend a fragment rule */
fragmentPlugin.prependRule(rule);

/** Append a fragment rule */
fragmentPlugin.appendRule(rule);

/** Lower-Level methods: */

/** Get a structured clone of the current rules */
fragmentPlugin.getRules();

/** Update the fragment rules */
fragmentPlugin.setRules(rules: Rule[]);

Questions

Is structuredClone ready to be used?

Checks

hirasso commented 10 months ago

@daun ready for review :)

daun commented 10 months ago

API looks great! Is there an alternative for structuredClone that has been supported for a little longer? When creating sites with a two-year browser support window, it will require a polyfill that adds a few kilobytes. Or maybe the plugin could check if structuredClone is supported and only use it if found, otherwise do a shallow copy?

hirasso commented 10 months ago

Hmm, right... I think I'll go with a detection for structuredClone and otherwise return a shallow copy, combined with a warning if options.debug is true

hirasso commented 10 months ago

...turns out cloning the rules by myself wasn't so hard, as it's structure is well known. structuredClone dependency is gone!