-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bundle css on server and return to client #128
Comments
Currently this is not a feature of my package, but it may be possible to add it. How are you performing SSR and how do you envision sending the CSS? |
There could be 2 ways in my opinion. 1 All css files which are imported could be bundled when the Meteor Server restarts and put all together in one file which can be added to the 2 Like in your example it could be possible to extract the css of the file and add it to a self written CSS which has the context of all CSS code on Serverside and can append a <style /> tag to the head - then only the needed CSS for the SSR will be returned to client. I dont know which one would be the best and how to implement this because I have not rly a clue how css-modules works and how I am able to extract the generated css. |
The way Meteor handles CSS, we have a few options:
There may be another way to do this by building a custom minifier plugin, but I'm not sure what all that would entail. |
But is there a way to export the generated CSS when importing the file? If so it would be easy to append the exported css to the head on server side. I do not know much about css-modules so I am not sure how this could be solved. Tomorrow I will do a little research |
Could you give me a hint how we could do this? Of course I would help when its possible for me to solve this case :) |
For option 3 (the export), I think it should be fairly straightforward (spoken as someone who is intimately familiar with the package, so our perspectives on straightforward may differ 🙂 ): If when An example of exporting is here, where we do the export for the class names: https://github.com/nathantreid/meteor-css-modules/blob/master/css-modules-build-plugin.js#L247 |
Thanks for your input. Not 100% sure if I understand you correct that I should now change the plugin custom for my own case or if you are interested to implement something to solve it in general? I think the best solution would be something like this:
Would be this something which could be possible? If so and you are not interested in doing it I would try it by myself. Would need some time to understand your plugin in full but I would give it a try. |
I'm interested in doing it, but it may take me a little while to get around to doing so. If you'd like to, feel free to look into it.
but with the export change you'd be able to do something like the following: import { css } from './foo.css';
import React from 'react';
export default class HelloWorld extends React.Component {
render() {
return (
<style>{css}</style>
);
}
} Is that what you had in mind? |
Your example would be an option but then you need to add your css on any file you are including it. The only reason to do what I want is because of SSR. If you are using SSR with Meteor you are working with https://docs.meteor.com/packages/server-render.html The sink object comes with the onPageLoad callback and holds some information of the request the user has made. So my idea would be to append the css information to the sink because this object is unique for every request. If it is possible to access the sink object on your package it would be easy to append the bundled css to the sink and then I can append it to the HTML on Server. The other option would be to use it like in your comment. Then it would be more handy to create a React Provider which gets all the bundled css and outputting it. Maybe it could also be possible to "auto-fetch" on any file which is including a css file? But I dont think so. I am not 100% sure which would be the best solution to solve this but in my case I would think that the sink method would be the easiest because then I am not in the duty to handle outputing the bunbled css on every file which is including style files. |
Hey @nathantreid - I just got your package wrong. I thought that on every request only the needed css will get generated on server - but thats not true. The css which is needed will be generated on the server start (which makes sense - of course). I was able to clone your package to my local packages directory and will try to do a update which would be make it able to return the needed css on server. My goal would to be to make it possible to export the css like in your example but also to add the generated css to the sink on every request, so it would be possible to send only the needed css of every request back to the client. Will let you know when I had success to do so. |
We are using Serverside rendering so we would like to send the generated css classes from the server to the client. Is there any way to solve this with your package?
If we do not send the css directly to the client from server the first render - which the user sees - does not have the styles. A second later the styles get appended to the elements which is a bit ugly on the first render.
The text was updated successfully, but these errors were encountered: