Giter Site home page Giter Site logo

Comments (3)

rishit5 avatar rishit5 commented on June 29, 2024

I am facing the same issue, how exactly did you import the styles? Can you elaborate on the workaround you used and also the correct solution, if you have been able to find it?

from react-ssr-lambda.

christopher-caldwell avatar christopher-caldwell commented on June 29, 2024

@rishit5 Hey, sorry I missed this. I ended up using styled-components to inject the CSS into the head. It was a very long and painful process to get it right.

Here's a watered down version

import ReactDOMServer from 'react-dom/server'
import { ServerStyleSheet } from 'styled-components'
import MetaTagsServer from 'react-meta-tags/server'
import { MetaTagsContext } from 'react-meta-tags'

import App from './App'
/**
 * <html lang="en">
  <head>
    <meta charset="utf-8" />
    ... other head tags
    META
    STYLED_COMPONENTS
  </head>
  <body >
    <div id="root"></div>
  </body>
</html>
 */
import { HtmlTemplate } from './html'

export const renderApp = () => {
  const Sheets = new ServerStyleSheet()
  const metaTagsInstance = MetaTagsServer()

  const props = {...}

  const app = ReactDOMServer.renderToStaticMarkup(
    Sheets.collectStyles(
      <MetaTagsContext extract={metaTagsInstance.extract}>
        <App {...props} />
      </MetaTagsContext>,
    ),
  )

  const meta = metaTagsInstance.renderToString()
  const styledComponentsTag = Sheets.getStyleTags()

  const html = HtmlTemplate.replace('<div id="root"></div>', `<div id="root">${app}</div>`)
    .replace('STYLED_COMPONENTS', styledComponentsTag)
    .replace('META', meta)
  Sheets.seal()
  return html
}

from react-ssr-lambda.

roman-boiko avatar roman-boiko commented on June 29, 2024

Webpack 4 could not do it correctly, I am working on a new sample and will address this issue there.

from react-ssr-lambda.

Related Issues (10)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.