Skip to content

Commit

Permalink
fix(rsc): Load all css links to support css with rsc (#10544)
Browse files Browse the repository at this point in the history
**Problem**
Currently we don't have good css support with RSC. Therefore the pages
are unstyled and look ugly.

**Changes**
1. We parse the build manifests and collect all css assets that could be
loaded. We then pass these through to the `Document` component which
ultimately causes them to be inserted into the HTML stream as link tags.
2. Removes the vite plugin which used to add `preinit` calls. It's no
longer used and can be revived from the git history if needed.

**Notes**
1. This link was helpful in confirming that this isn't a crazy road to
be going down:
https://vitejs.dev/guide/backend-integration.html#backend-integration
3. We can of course optimise in the future. We can go back to trying to
get `preinit` working or we can stop loading some of these css assets by
determining if they need to be loaded or not based on the url we're
responding to.

---------

Co-authored-by: Tobbe Lundberg <tobbe@tlundberg.com>
  • Loading branch information
Josh-Walker-GM and Tobbe committed May 11, 2024
1 parent 747ce41 commit 464ee74
Show file tree
Hide file tree
Showing 13 changed files with 87 additions and 1,592 deletions.
4 changes: 1 addition & 3 deletions packages/vite/src/buildRscClientAndServer.ts
Expand Up @@ -7,8 +7,7 @@ import { rscBuildRwEnvVars } from './rsc/rscBuildRwEnvVars.js'

export const buildRscClientAndServer = async () => {
// Analyze all files and generate a list of RSCs and RSFs
const { clientEntryFiles, serverEntryFiles, componentImportMap } =
await rscBuildAnalyze()
const { clientEntryFiles, serverEntryFiles } = await rscBuildAnalyze()

// Generate the client bundle
const clientBuildOutput = await rscBuildClient(clientEntryFiles)
Expand All @@ -18,7 +17,6 @@ export const buildRscClientAndServer = async () => {
clientEntryFiles,
serverEntryFiles,
{},
componentImportMap,
)

// Copy CSS assets from server to client
Expand Down
8 changes: 7 additions & 1 deletion packages/vite/src/clientSsr.ts
Expand Up @@ -8,6 +8,7 @@ import type { default as RSDWServerModule } from 'react-server-dom-webpack/serve
import { getPaths } from '@redwoodjs/project-config'

import { StatusError } from './lib/StatusError.js'
import { getRscStylesheetLinkGenerator } from './rsc/rscCss.js'
import { moduleMap } from './streaming/ssrModuleMap.js'
import { importModule } from './streaming/streamHelpers.js'
import { makeFilePath } from './utils.js'
Expand Down Expand Up @@ -97,6 +98,8 @@ export function renderFromDist<TProps extends Record<string, any>>(
) {
console.log('renderFromDist rscId', rscId)

const cssLinks = getRscStylesheetLinkGenerator()()

// Create temporary client component that wraps the component (Page, most
// likely) returned by the `createFromReadableStream` call.
const SsrComponent = async (props: TProps) => {
Expand Down Expand Up @@ -153,7 +156,10 @@ export function renderFromDist<TProps extends Record<string, any>>(
const stream = renderToReadableStream(
// createElement(layout, undefined, createElement(page, props)),
// @ts-expect-error - WIP
createElement(ServerEntry, { location: { pathname: rscId } }),
createElement(ServerEntry, {
location: { pathname: rscId },
css: cssLinks,
}),
bundlerConfig,
)

Expand Down

0 comments on commit 464ee74

Please sign in to comment.