Remove global resource

I have an Aurelia app and with some webpack trickery so we can develop entire routes of the app independently to support a stitched UI, somewhat similar to what singla-spa provides. We typically write out aurelia plugins as separate consumable libraries that are loaded into the shell, but I’m writing some resources that only needs to be used for my route at the moment, and I have it registering by injecting by creating new FrameworkConfiguration(injectedAurelia).globalResources([...]).apply() within a view engine hook resource in my entry route module. My question is, is there a way to remove those added global resources with an afterUnbind hook?

1 Like

There’s no afterUnbind in v1, so I suppose you meant unbind. All global resources are registered with the global ViewResources, which you can access by injecting Aurelia instance, then get ViewResources from the container of that Aurelia instance:

@inject(Aurelia)

...

aurelia.container.get(ViewResources).getElement...

What we don’t have is an official API to remove those registration. But you can hack-around this for now by manually deleting them:

delete aurelia['resources']['my-custom-element']

It would be great if you could describe your scenario a bit more, so maybe we can be aware of the needed APIs to make it easier, maybe.

Yes sorry I assumed there was an afterUnbind view engine hook but checked the interface and didn’t see one, so maybe beforeUnbind or unbind|detached on the view model.

This is exactly what I’m looking for though, it looks like there is an additional level depending on the resource type in order to delete it is that correct? i.e. if my resource is an element named ‘my-custom-element’ I’d have to delete aurelia['resources']['elements']['my-custom-element'] or for an attribute delete aurelia['resources']['attributes']['my-custom-attribute'].

My scenario is a bit unique I think from majority of aurelia apps out there because each route in my app is built like a “microservice”, in which its within its developed by a separate team, has its own repository, and registers a manifest of itself in the shell app with details such as it’s route, required js/css, etc. I’m chunking the route’s entry using PLATFORM.moduleName('./my-entry', 'my-chunk') and loading the webpack chunk from the aurelia shell app with a route navigationStrategy. My main concern is someone visiting my route, my route adds global resources (some of which may not be very unique i.e. ‘svg-icon’), and then navigating to other routes with elements/resources that may have the same identifier which I’m assuming would result in a conflict, even if they are using <require from="../resources/svg-icon"></require> in their template, potentially requiring a user to refresh the page to resolve.

1 Like