New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Nextjs] cssinjs breaks by streaming mode #45955
Comments
Screen.Recording.2023-11-18.at.21.09.32.movHere is quick demo of both issues |
Also i've found kinda a workaround for both bugs: 'use client';
import { useRef, useState } from 'react';
import { useServerInsertedHTML } from 'next/navigation';
import { createCache, extractStyle, StyleProvider } from '@ant-design/cssinjs';
export function AntdRegistry({ children }: React.PropsWithChildren) {
const [cache] = useState(() => createCache());
const isServerInserted = useRef(false);
useServerInsertedHTML(() => {
if (isServerInserted.current) {
// We would like to avoid duplicated css insertion
// But it seems to break the style when streaming
// return;
}
isServerInserted.current = true;
const attributes = htmlToAttributes(extractStyle(cache));
return <style {...attributes} />;
});
return (
<StyleProvider cache={cache} hashPriority='high'>
{children}
</StyleProvider>
);
}
const startTagRegex = /^<(.+?)>/;
function htmlToAttributes(html: string) {
const rootTagContentMatch = startTagRegex.exec(html);
if (!rootTagContentMatch) {
throw new Error('htmlEjectAttributes: invalid html');
}
const [fullMatch, content] = rootTagContentMatch;
const htmlAttrsRegex = /\s(\w+)="(.+?)"/g;
let match;
const argPairs = [];
while ((match = htmlAttrsRegex.exec(content))) {
argPairs.push([match[1], match[2]]);
}
const [rootTag] = content.split(' ');
const __html = html.slice(fullMatch.length, html.lastIndexOf(`</${rootTag}>`));
return argPairs.reduce((acc, [name, value]) => ({ ...acc, [name]: value }), {
dangerouslySetInnerHTML: { __html },
});
} It seems to me that the first bug is cause by the fact that My solution works in general, but I wish to have an api to get attributes from |
Seems same issue as #45894 |
Indeed, partly. The first bug is caused by a different reason. |
Reproduction link
https://github.com/infodusha/antd-next-streaming-issue
Steps to reproduce
There are actually two bugs with different reasons:
What is expected?
Both bugs are expected not to happen:
What is actually happening?
The text was updated successfully, but these errors were encountered: