Skip to content

✨ Master CSS React Stream #173

Closed
Closed

Description

@1aron

Description

This Stream API is currently only used as a pipeline for SSR. We recommend you asynchronously load the JIT engine for CSS hydration after the page is initialized.

class CSSStream extends Transform {
    constructor(config?: Config) {
        let inserted = false
        let content = ''

        super({
            transform(chunk: Buffer, _, callback) {
                content += chunk.toString()
                if (inserted ? content.endsWith('</script>') : content.endsWith('</body></html>')) {
                    this._flush(callback)
                } else {
                    callback()
                }
            },
            flush(callback) {
                if (content) {
                    if (!inserted) {
                        inserted = true
                        content = renderIntoHTML(content, config)
                    }
                    this.push(content)
                    content = ''
                }
                callback()
            },
        })
    }
}

For Remix:

// app/entry.server.tsx
import { CSSStream } from '@master/css.react'

function handleBrowserRequest(
    request: Request,
    responseStatusCode: number,
    responseHeaders: Headers,
    remixContext: EntryContext
) {
    return new Promise((resolve, reject) => {
        let didError = false

        const { pipe, abort } = renderToPipeableStream(
            <RemixServer context={remixContext} url={request.url} />,
            {
                onShellReady() {
                    const body = new CSSStream(config) // SSR
                    responseHeaders.set('Content-Type', 'text/html')
                    resolve(
                        new Response(body, {
                            headers: responseHeaders,
                            status: didError ? 500 : responseStatusCode,
                        })
                    )
                    pipe(body)
                 },
                 ...
            }
        )
        ...
    })
}

Activity

  1. added this to the Master CSS 2.0 milestone on Mar 2, 2023
  2. added
    undocumentedNot yet written into official documentation
    on May 8, 2023
  3. removed their assignment
    on May 15, 2023
  4. assigned and unassigned on May 29, 2023
  5. removed this from the 2.0 RC milestone on Dec 14, 2023
  6. added theissue type on Mar 24, 2025
  7. locked and limited conversation to collaborators on Apr 11, 2025
  8. converted this issue into a discussion #392 on Apr 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

undocumentedNot yet written into official documentation

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions