swup / fragment-plugin

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

feat: validate location of fragment elements in DOM #55

Closed hirasso closed 1 year ago

hirasso commented 1 year ago

Description

I recently got bitten by the fact that I had fragments defined that were outside of swup's options.containers. This lead to hard-to-understand weirdness between normal and fragment visits.

This PR adds validation so that each fragment for a visit must meet one of these two rules:

  1. It matches one of swup's default containers
  2. It's a descendant of one of swup's default containers

Otherwise the fragment rule will be ignored for the current visit.

How to test:

Markup:

<body>
  <div id="my-test-fragment"></div>
  <div id="swup"><!-- main container --></div>
</body>

JS:

new SwupFragmentPlugin({
    rules: [
        {
            from: '(.*)',
            to: '(.*)',
            containers: ['#my-test-fragment'],
        },
        // ... your other rules
    ],
    debug: true,
});

Checks

daun commented 1 year ago

I'm assuming I'm not supposed to look at this yet? Ping me when it's finished πŸ™ƒ

hirasso commented 1 year ago

pong πŸ“ πŸ™‚

daun commented 1 year ago

Just to understand β€” what's the weirdness you've been seeing? Buggy behaviour or difficulties with logging/debugging?

I agree that warning about fragments outside of swup containers is useful, but is there a benefit to absolutely ignoring those fragments instead of merely warning about them? I'm mostly thinking of modals/popups that need to live outside a swup container for styling/accessibility reasons.

hirasso commented 1 year ago

@daun Thanks for asking! :)

The thing I was seeing:

I'm almost 100% percent sure that there will never be a fragment outside of swup's default containers necessary. But I'm happy to be convinced otherwise. Due to the complexity of the subject I'd love to get together in a call and talk it through.

daun commented 1 year ago

Sure, let's talk it through in a call soon! If you're seeing the issue in the exact case I'm imagining as a counter-point, I'm probably not seeing the whole picture yet.