Skip to content
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

What's the recommended approach for updating styles when client-side route updates #41

Closed
grgur opened this issue May 19, 2016 · 6 comments

Comments

@grgur
Copy link

grgur commented May 19, 2016

Ok so let's say that our isomorphic app changes route and the client side router changed the view based on JavaScript. Is there an official approach to retrieving the CSS that was not already inserted in <head>?

In other words:

  1. User navigates to the web site.
  2. Server side renderer returns HTML + CSS with help from isomorphic-style-loader
  3. Server also returns the JS bundle used to render views on the client side (a-la SPA style)
  4. User wants to go to the Account page. Account link is clicked.
  5. Client side router changes view to Account.
  6. The Account page is rendered on the client side and shown

ok, here's my question
7. How should I retrieve the CSS needed for the Account page to show?

One approach could be to use the ExtractTextPlugin, but that doesn't seem to work (because ExtractTextPlugin loader doesn't know how to call _getCss().

Any ideas?

Thanks a bunch

@grgur grgur changed the title wjat What's the recommended approach for updating styles when client-side route updates May 19, 2016
@frenzzy
Copy link
Member

frenzzy commented May 19, 2016

With React.js you can use the convenient decorator function withStyles(styles)(MyReactComponent), take a look at the docs and source code.

In your own framework, you must call the _insertCss and _getCss functions manually, example:

// frontend
import s from './PageStyles.css'
const removeCss = s._insertCss() // mount (create <style> DOM node)
removeCss()                      // unmount (remove <style> node from DOM)

// backend
import s from './PageStyles.css'
const response = s._getCss()     // css string

@grgur
Copy link
Author

grgur commented May 19, 2016

@frenzzy thanks a bunch for a quick answer.

Ok, so the way i see it - insertCss function basically gets CSS from the CSS module object (import s from './PageStyles.css'). That's great when it's on the server side, but how exactly do I fetch that CSS object if I'm already on the front-end? Obviously loader's css objects are kept on the server side, no?
Does my question make sense?

@frenzzy
Copy link
Member

frenzzy commented May 19, 2016

You should use isomorphic-style-loader for both client-side and server-side bundles. Frontend uses _insertCss to mount styles to the DOM, backend uses _getCss to prepare html string which will be responded to the client-side. You can find a working example in React Starter Kit:

@grgur
Copy link
Author

grgur commented Jun 10, 2016

Ok so basically the thing i was interested in is how the rest of the css (besides critical path) is transferred to the client. Looks like it's bundled in JS.

Thanks

@grgur grgur closed this as completed Jun 10, 2016
@koistya
Copy link
Member

koistya commented Jun 10, 2016

@grgur yep, it's bundled into JavaScript bundle (or chunks) and it's usually a good idea to load that bundle asynchronously, for example, in the <head> section of your HTML page:

<script async src="/bundle.js"></script>

@grgur
Copy link
Author

grgur commented Jun 10, 2016

Yes, exactly how i had it set up.

Thank you for open sourcing this. It's top notch!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants