-
-
Notifications
You must be signed in to change notification settings - Fork 9
Server rendering/setting initial markdown state #16
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
Comments
What are you using for serverside rendering?
Could you share a runnable example of this? for example in https://codesandbox.io
Could you expand why this is an problem?
an
FYI, react-markdown v6 will likely be built on top of react-remark. |
We are server rendering in .NET Core via ReactJS.NET
Below example that crashes: https://codesandbox.io/s/objective-montalcini-cixpt?
We are using base react in a polyfilled Chakra core runtime on the server. AFAIK there is no way in base react to set an initial state via async in useState constructor (calling setState in a useEffect is not an option as that does not get run in react-dom/server's renderToString method). Perhaps a solution is to have an option to use processSync with process as the default? e.g a |
https://codesandbox.io/s/objective-montalcini-cixpt this is the equivalent of: function InfiniteLoop () {
const [state, setState] = useState();
setState({});
return null;
} each render a
Oof, isomorphic react on a non-V8 runtime, with lightly supported language bindings, that doesn't sound fun, sorry.
It is an option 🤔 Taking a step back from the problem, from a moment, it sounds like you're looking more for a pure static content generator? |
Unfortunately not as the content is stored in a headless CMS. Currently it handles blog posts in markdown, but we are in the process of extending it out to handle custom pages as well (which may contain JSX and/or HTML), which will be handled by a content team and occasional developer for more complicated markup/layouts. I'm also not sure if you'd be able to server render on nextjs either, though you're able to set initial component props in EDIT: Result window: |
Not currently, I'm waiting for React Server Components and Serverside Suspense to shake out some more to allow for more flexible async components on the serverside.
What would you think of export interface UseRemarkOptions {
remarkParseOptions?: Partial<RemarkParseOptions>;
remarkToRehypeOptions?: RemarkRehypeOptions;
rehypeReactOptions?: Partial<RehypeReactOptions<typeof createElement>>;
remarkPlugins?: PluggableList;
rehypePlugins?: PluggableList;
}
export const useRemarkSync = (
source: string,
{
remarkParseOptions,
remarkToRehypeOptions,
rehypeReactOptions,
remarkPlugins = [],
rehypePlugins = [],
}: UseRemarkOptions = {}
): ReactElement =>
unified()
.use(remarkParse, remarkParseOptions)
.use(remarkPlugins)
.use(remarkToRehype, remarkToRehypeOptions)
.use(rehypePlugins)
.use(rehypeReact, { createElement, Fragment, ...rehypeReactOptions })
.processSync(source).result as ReactElement; ? |
Looks good to me! Would it make sense to wrap it in a useMemo and have source as the dependency? Looks like it could potentially get expensive and block the main thread if the containing parent component gets re-rendered |
Popping my head in just to say that this is a blocker for me. I'm using Next. |
I just had same issue with SSG on Next.JS.
|
added in #18 This change will be part of the next release |
has this been released yet? |
It is ready for release, but hasn't been yet. |
@ChristianMurphy your last comment said “This change will be part of the next release”. Sounds like there is more work to do? |
There is, cutting a minor release. |
Released. |
Uh oh!
There was an error while loading. Please reload this page.
Subject of the feature
Server rendering react-remark components by passing through an initial state value to useRemark, or using the component's
children
.Describe your issue here.
Problem
The useRemark hook's reactContent initial state is null. It looks like the only way to update this state is by using the exposed setMarkdownSource method.
The component sets the state of reactContent in a useEffect calling setMarkdownSource(children) which does not get executed on the server (by calling react-dom/server renderToString).
Alternatives
What are the alternative solutions? Please describe what else you have considered?
Have tried using the first example in README.md, which creates an infinite loop.
Looking through the source code, potential solutions:
setMarkdownSource uses unified.process which is async. We could create a synchronous function which uses unified.processSync to set the initial state based off a new prop in the useRemark hook e.g initialContent, though as I understand, if a provided remark plugin is async, this will throw an error.
I haven't tested this in a server rendered environment, however running this projects storybook seems to work ok with the following.
For now, using react-markdown works fine (which seems to be synchronous) for our use case, however this project seems to align closer to our preference for parsing architecture).
Being able to server render initial content would be a useful feature.
The text was updated successfully, but these errors were encountered: