How to use SmidgeHelper.RequiresCss
s
I'm trying to inject a css file from a partial using Smidge, but I can't seem to get it to work. In my master template I have
@await SmidgeHelper.CssHereAsync()
and in my
Footer.cshtml
partial I have:
Copy code
SmidgeHelper.RequiresCss("~/css/footer.css");
This does not inject the css in the master template as I expected. If I add the RequiresCss code in the master template itself it works, but not in the partial. I'm new to Umbraco (and .net) and what I'm trying to achieve is to only import the used CSS files on each page, I have understood that this is the preferred method for this?
a
Hmm, that should work, I have a similar setup Master pages contains:
Copy code
csharp
  SmidgeHelper.RequiresCss("~/css/main.min.css");
  @await SmidgeHelper.CssHereAsync()
View contains
Copy code
csharp
SmidgeHelper.RequiresCss("~/css/external/splide.min.css");
And my css for the slider works perfectly.. Could you double check paths ?
m
You can't inject from a partial view, only in a view (aka template)
a
Ah, but you are able in a view from a viewComponent
m
@Ambert ... Interesting 🤔 , I would have thought that has the same adverse considerations as partials that @shazwazza. mentions re when in the page lifecycle viewComponent is included (aren't viewcomponents rendered with a underlying partial)?? infact
If you use child actions or view components, it breaks (generally speaking)
is mentioned by Shannon in issue 145? @Ambert Perhaps you can share your particular magic? At a client request we did implement something around
HttpContext.Items
~ https://stackoverflow.com/questions/56918542/how-to-use-section-scripts-in-a-partial-view-mvc-core/72933590#72933590 with inspiration from @CodeSharePaul resource https://codeshare.co.uk/blog/how-to-include-scripts-from-partial-views-in-mvc-and-umbraco/
a
Currently dont have the capacity to understand the issue fully, but I havent had any issues so far with the css/js not loading for my slider
m
👍 The other issue we had with the unamed on the fly smidgeHelper.requires.. is that you can't prioritise scripts or indeed have a header and a footer include location for above the fold/render blocking considerations.
s
Hi, Due to the way that views are rendered and due to to potentially caching viewcomponents server side if someone were to do that, using inline declarations in partials, etc... might not work. It probably won't work in many scenarios. This worked in ClientDependency due to some extreme hacks in MVC but that had perf issues and also never supported stream loading in a page (not possible in old aspnet). The use of Smidge recommends creating pre-defined bundles - it is faster, and less problematic. Having partial views declare dependencies, and then trying to render the full dynamic bundle in a master view (for example) could have several problems. I don't plan on implementing work arounds in Smidge because there are too many factors that will make the work arounds not work either.
16 Views