react-tweet icon indicating copy to clipboard operation
react-tweet copied to clipboard

Gatsby Partial Hydration

Open lfades opened this issue 3 years ago • 8 comments

In the process of trying out if next-tweet (soon to be renamed to react-tweet) could work with Gatsby's Partial Hydration (the feature that implements React Server Components), multiple issues were found and the on progress app was removed in https://github.com/vercel-labs/next-tweet/pull/44

Here are the issues I've found so far:

CSS Modules

CSS Modules in Next.js, Vite, CRA, Remix and similars import the default export:

import styles from './app.module.css'

In Gatsby the above does not work unless it's changed to:

import * as styles from './app.module.css'

Changing the imports to support Gatsby can break other libraries so instead I went with this change to its webpack config: apps/gatsby-app/gatsby-node.js

Global CSS

react-tweet has an import for a global CSS file that implements all the CSS variables that the tweet uses. This worked well elsewhere but in Gatsby those styles were not being included, so the following had to be done in order to use the tweet components:

import s from 'next-tweet/theme.css'
console.log(s) // This line can't be removed

It's a workaround for this issue: https://github.com/gatsbyjs/gatsby/issues/19446

React Server Components (RSC)

After the CSS issues are handled with the workarounds, react-tweet has can be used client-side in Gatsby, just like in Vite and CRA. However, after enabling Gatsby's Partial Hydration with the following flag:

flags: {
  PARTIAL_HYDRATION: true,
},

All I could see was a blank screen and the following error in the browser console:

image

The react version was set to experimental and pnpm was used with the gatsby-plugin-pnpm plugin installed.

lfades avatar Mar 09 '23 18:03 lfades

Hey @lfades , Would be glad to look into this!

rielAsh24 avatar Jun 26 '23 05:06 rielAsh24

@lfades can you share the gatsby-node.js files for the gatsby-app? I'm facing the same issue with CSS Modules while setting up the test app for gatsby in react-tweets/apps.

rielAsh24 avatar Jun 27 '23 12:06 rielAsh24

It's this one: https://github.com/vercel-labs/react-tweet/pull/44/files#diff-3c08e5022c3c58b6f607c199b3cdccd2d22584e5e51b20f983102b3d4b2547a2

const path = require(`path`)

exports.onCreateWebpackConfig = ({ stage, actions, getConfig }) => {
  const config = getConfig()

  config.module.rules.forEach((rule) => {
    rule.oneOf?.forEach((rule) => {
      rule.use?.forEach((plugin) => {
        if (
          plugin.loader.includes('/css-loader') ||
          plugin.loader.includes('/mini-css-extract-plugin')
        ) {
          if (plugin.options.modules?.namedExport) {
            plugin.options.modules.namedExport = false
          }
        }
      })
    })
  })
  actions.replaceWebpackConfig(config)
}

lfades avatar Jul 04 '23 17:07 lfades

Hey @lfades! I managed adding a new gatsby-app to the apps/ directory. Following is the output:

gatsby-app

Observations:

  1. The browser logs show preload warnings:

gatsby-browser-logs

  1. The styles applied in other test apps are not functioning in gatsby-app

rielAsh24 avatar Jul 09 '23 11:07 rielAsh24

Hey @lfades! I managed adding a new gatsby-app to the apps/ directory. Following is the output:

gatsby-app

Observations:

  1. The browser logs show preload warnings:

gatsby-browser-logs

  1. The styles applied in other test apps are not functioning in gatsby-app

Hey @lfades, requesting a confirmation on this, and if I can open a PR for the same.

rielAsh24 avatar Jul 15 '23 14:07 rielAsh24

@rielAsh24 If the CSS is not working then that means CSS Modules are still an issue for the package to be used in Gatsby and we would need a fix for that before a PR.

lfades avatar Jul 27 '23 22:07 lfades

@rielAsh24 If the CSS is not working then that means CSS Modules are still an issue for the package to be used in Gatsby and we would need a fix for that before a PR.

Got you; I'll try working on it and post updates.

rielAsh24 avatar Jul 29 '23 04:07 rielAsh24

Hey @lfades ! Apologies it took so long but I finally made it work :)

chrome

Note: Except Safari, all browsers give a perfect output when react is set to experimental or canary. Stable releases of react work wonderfuly on all browsers.

rielAsh24 avatar Sep 14 '23 04:09 rielAsh24