Giter Site home page Giter Site logo

Comments (3)

Deckstar avatar Deckstar commented on June 19, 2024 2

Hello again!

I described my solution for this problem in this blog post. Here's a summary in case someone is reading it here 😄


TLDR:

  • copy paste the two files below
  • make sure your <CacheProvider /> component imports your emotion cache from the new place
  • remove gatsby-plugin-material-ui from your gatsby-node.js file if you had it

./src/theme/cache.js:

import createCache from '@emotion/cache';

/** @type {import('@emotion/cache').Options} */
export const cacheProps = {
	key: 'mui',
	prepend: true,
};

/** @type {import("@emotion/cache").EmotionCache | undefined} */
export let muiCache;

export const makeMuiCache = () => {
	if (!muiCache) {
	muiCache = createCache(cacheProps);
	}

	return muiCache;
};

./gatsby-srr.js:

import { CacheProvider } from '@emotion/react';
import createEmotionServer from '@emotion/server/create-instance';
import React from 'react';
import { renderToString } from 'react-dom/server';
import { getTssDefaultEmotionCache } from 'tss-react';

import { makeMuiCache } from './src/theme/cache';

/** @param {import('gatsby').ReplaceRendererArgs} args */
export const replaceRenderer = (args) => {
  const { bodyComponent, replaceBodyHTMLString, setHeadComponents } = args;

  const muiCache = makeMuiCache();
  const { extractCriticalToChunks } = createEmotionServer(muiCache);

  const emotionStyles = extractCriticalToChunks(
    renderToString(
      <CacheProvider value={muiCache}>{bodyComponent}</CacheProvider>
    )
  );

  const muiStyleTags = emotionStyles.styles.map((style) => {
    const { css, key, ids } = style || {};
    return (
      <style
        key={key}
        data-emotion={`${key} ${ids.join(` `)}`}
        dangerouslySetInnerHTML={{ __html: css }}
      />
    );
  });

  const tssCache = getTssDefaultEmotionCache();
  const { extractCritical } = createEmotionServer(tssCache);
  const { css, ids } = extractCritical(renderToString(bodyComponent));

  const tssStyleTag = (
    <style
      key="tss-styles"
      data-emotion={`css ${ids.join(' ')}`}
      dangerouslySetInnerHTML={{ __html: css }}
    />
  );

  const combinedStyleTags = [...muiStyleTags, tssStyleTag];
  setHeadComponents(combinedStyleTags);

  // render the result from `extractCritical`
  replaceBodyHTMLString(emotionStyles.html);
};

Don't forget to delete the Gatsby Material UI plugin if you were using it!

./gatsby-config.js:

module.exports = {
 /* Your site config here */
 plugins: [
   `gatsby-plugin-material-ui`, // <-- delete me!
	// ...rest plugins
 ],
 // ...rest config
};

And that's it 😄

Thanks again for the help!

from tss-react.

Deckstar avatar Deckstar commented on June 19, 2024 1

Hey! Just wanted to say thanks a lot for the very quick help! :)

It took me a while but I finally solved the issue by combining the code snippet & blog post you posted with some code from the Gatsby Material UI plugin. Basically I had to copy paste most of that plugin into my own gatsby-ssr.js file.

I will try to post my full solution here this weekend and then I think we can close this issue 😄

from tss-react.

garronej avatar garronej commented on June 19, 2024

Hi @Deckstar,

Maybe this can help:

import { getTssDefaultEmotionCache } from "tss-react";
const cache = getTssDefaultEmotionCache();

https://ntsim.uk/posts/how-to-add-vanilla-emotion-ssr-to-gatsby

Let me know if it help. Otherwise please share a sample project with me so I can test.

Best regards

from tss-react.

Related Issues (20)

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.