Skip to content

Discuss SSR API and methods #386

Closed
Closed

Description

@chiefjester

I just want to kick off a discussion to finalise our SSR API.

So API came from the comments on the #214

const { css, html } = StyleSheet.collect(() =>
  ReactDOMServer.renderToString(<App />)
);

Please feel free to suggest what the API will look like, (let the bikeshedding begin!)

Also if we're already doing de-duping in SSR level, do we even need reset()? (related issue #378) Or would that just be a convenience function since we don't need it anymore?

cc @mxstbr / @geelen

Activity

  1. acjay commented on Jan 20, 2017

    @acjay
    Member

    One thing I would throw out there is that it would be very useful to be able to have separate bundles of CSS. That way, you could target styles to a style tag or to a separate stylesheet. When I worked for a publisher, we used this type of optimization to achieve very fast initial renders, with nonessential styles deferred until later.

    I can ticket this separately, if that would be helpful. It would require some sort of ability to annotate at the component or CSS level where rulesets would be targeted, and an API for collecting the separate bundles (which might be a collect variant).

  2. mxstbr commented on Jan 21, 2017

    @mxstbr
    Member

    This happens either way @acjay. With styled-components, only the styles of components that are actually used on the initial render are injected 👍

    Generating .css files for server-side rendered apps would not be complicated to add, but should not live in core. (it's more or less just a fs.write(css) away) We're going to work on a babel or webpack plugin to make this possible for non-ssr apps but that's a different issue. (see #59)

  3. dfrankland commented on Jan 24, 2017

    @dfrankland

    @ThisGuyChris, reset() is great for doing testing. Considering that tagged templates have logic in them, that logic may need to be tested.

  4. mxstbr commented on Jan 24, 2017

    @mxstbr
    Member

    That's a great point actually @dfrankland, I hadn't even considered that use case!

  5. camsjams commented on Jan 30, 2017

    @camsjams

    Looks good to me

  6. nicksrandall commented on Jan 30, 2017

    @nicksrandall

    Do we also want to be able to export class names here ( in addition to CSS and HTML) like Aphrodite and Glamor does so that we can consume them on the front end to make sure things stay in sync or are just going to rely on the babel plugin to ensure deterministic class names?

  7. xcoderzach commented on Jan 31, 2017

    @xcoderzach
    Contributor

    Even if the names are deterministic, we still want a list of the class names, so we don't inject every rule twice. Either that, or maybe we could have a class attribute on style and links like __styled-component-sheet__, and then just build the list from those sheets.

  8. wmertens commented on Feb 1, 2017

    @wmertens
    Contributor

    I don't think we need to worry about duplicate classnames after SSR; just remove the server-provided style block after the app code arrived and let S-C rebuild the styles.

  9. kukagg commented on Feb 1, 2017

    @kukagg

    I like what Aphrodite does. Just pass the classnames along with the initial state and rehydrate without rebuilding client-side. Does anyone see any cons with this approach?

  10. wmertens commented on Feb 1, 2017

    @wmertens
    Contributor
  11. ForbesLindesay commented on Feb 2, 2017

    @ForbesLindesay

    @wmertens I found that in order to get the names to match, I had to make sure each file was transpiled via babel once, and then the result re-used on client and server. I was initially applying babel twice (once for the server side and once for the client side). This was resulting in different names.

    It would be cool if in the babel plugin, we could make the names deterministic (e.g. use an md5 hash of the file's source)

  12. wmertens commented on Feb 2, 2017

    @wmertens
    Contributor
  13. GGAlanSmithee commented on Feb 2, 2017

    @GGAlanSmithee
    Member

    I am experiencing the same @wmertens

  14. ForbesLindesay commented on Feb 2, 2017

    @ForbesLindesay

    I think possibly the simplest option might be some kind of .styled-components-cache.json file that could map sha hashes of files + index of styled component to generated class name. That way we could ensure they were always deterministic, without having to make them long enough to avoid collisions.

    On the other hand, if they're currently just random IDs, maybe just converting them to the first few letters of the sha has would be good enough.

  15. 76 remaining items

  16. satya164 commented on Apr 13, 2017

    @satya164

    And, this is about SSR, so there are no browser APIs available

    Sure, you can just use a string if it's truly global, no APIs needed, but that's not what I want

    If you have components right down to the document root, why do you need global styles?

    Because the styles can be global styles for a single page, not global styles for the all pages. Different pages reuse the same document component. I can't just set global styles on the document because that will insert the styles to all the pages, and not specific pages

    The problem with singleton is that it'll insert styles into all pages. I can have a docs page, and an about page with different fonts and such. And inserting same font declarations to both pages is wasteful.

  17. satya164 commented on Apr 13, 2017

    @satya164

    Every other React component only deals with itself and its children

    Portals don't, they exist outside of the current component tree.

    It's not a new concept really. There are actually several components like StatusBar in React Native which do this.

  18. geelen commented on Apr 16, 2017

    @geelen
    Member

    Yeah I think the best way around that then is to really just render a <style> tag inline in your component hierarchy somewhere. It has the correct semantics (it has no effect once unmounted) and will be handled by React's rehydration just fine.

  19. mxstbr commented on Apr 28, 2017

    @mxstbr
    Member

    @Ismay npm i --save styled-components@rc, use the API from #679 and you're good. I doubt we'll change it again!

  20. mxstbr commented on Apr 28, 2017

    @mxstbr
    Member

    I'll thusly also close this issue. Thanks for the lively discussion everybody!

  21. kitten commented on Apr 28, 2017

    @kitten
    Member

    For anyone who ends up here and is searching for some documentation. There's some text explaining it briefly on the upcoming docs: https://sc-next-docs.philpl.com/docs/advanced#server-side-rendering

    Edit: As @ChingxWando says below, the website is now live. So the new link is: https://www.styled-components.com/docs/advanced#server-side-rendering

  22. AxolotlxWando commented on Jun 24, 2017

    @AxolotlxWando

    I believe the documentation was shortly updated afterwards and was live on the main site:
    https://www.styled-components.com/docs/advanced#server-side-rendering

    p.s. Hiii phillll & been wanting for ssr for so long & it's so exciting & thanks for creating styled component

  23. oyeanuj commented on Sep 30, 2017

    @oyeanuj

    @philpl @wmertens Wanted to check about the FoUC part mentioned in the above discussion - is that to be expected in development mode? Or is there a way around that?

  24. GGAlanSmithee commented on Sep 30, 2017

    @GGAlanSmithee
    Member

    @oyeanuj as long as you render your styles server side, there won't be any FoUC, in development or production.

  25. mxstbr commented on Sep 30, 2017

    @mxstbr
    Member

    @oyeanuj no, there shouldn't be any FOUC. Mind opening a new issue about that?

  26. oyeanuj commented on Oct 1, 2017

    @oyeanuj

    @mxstbr @GGAlanSmithee thanks for the quick response. It turned out to be an issue in which I had set up SC, so false alarm. Sorry for the noise!

  27. wilfredonoyola commented on Nov 23, 2017

    @wilfredonoyola

    Hey :) , I have integrated this with (https://github.com/reactjs/react-rails). Actually is not working.

    Can you provider some solution?

    Thanks,

  28. kitten commented on Nov 23, 2017

    @kitten
    Member

    @wilfredonoyola can you please start a thread on Spectrum or open a new issue, as this one is unrelated, stale, and closed 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions