New theming system adds inline styles to every page, preventing caching of common CSS
Describe the bug
The new theming system in v18 seems to be based solely on inline style tags. This is fine (I guess) for apps which are entirely CSR.
However with SSG and/or SSR these inline styles will be repeated on every pre-rendered page and on every SSR response. This prevents these styles from being cached separately or at all (in the case of SSR). In the case of SSG all you can do is cache the page as a whole, which has negative side-effects, too (you might not want your HTML to be cached, to be able to issue updates to it without users having to do a hard refresh).
In the case of SSR you can't cache the styles at all, because they are sent inline with the server side rendered HTML.
The styles should be a separate CSS file, so they can be cached by normal means. The Angular Material theming system is an example of how to achieve this.
Environment
n/a
Reproducer
https://stackblitz.com/~/github.com/diesieben07/primeng-theming-repro
Angular version
19.0.4
PrimeNG version
Build / Runtime
Angular CLI App
Language
TypeScript
Node version (for AoT issues node --version)
22.12.0
Browser(s)
No response
Steps to reproduce the behavior
- Use PrimeNG with SSR and/or SSG
Expected behavior
PrimeNG's CSS should be an external CSS file so it can be cached by browsers separate from the HTML.
Thanks a lot for your feedback! The main reason is to establish an unstyled structure while keeping style loading under our control. This approach will allow us to implement various dynamic features. Perhaps we can consider adding an option for this in the future. We'll revisit the topic in PrimeNG 20.
Please consider this request.
Right now by injecting styles into the page via a style tag, means that css overrides that we craft are ignored.
For example we whitelabel our application. So we have custom brands that are applied to the site via separate stylesheets. So we have to resort to liberal use of !important on every single css override that we write. This isn't a great practice in CSS as the specitivity starts getting hard to manage when we start doing that to override styles dropped into the style tag
Agreed. Having an option to generate css files to make use of caching would be helpful.
The current style architecture also makes it hard to reuse Primeng's CSS variables or styles in our own code. For example we have SCSS or CSS already, and that references the same styles either via variables or extends. By using primeng variables in our code so we could keep our code DRY and let Primeng be the source of truth on some core style definitions.
By injecting CSS into the <head> we lose visibly into that code and reuse of it.