-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
Server side rendering doesn't work. #870
Comments
I also really need server-side rendering to work (with next.js) |
@antherkiv can you paste an example or a stacktrace of the error you're getting, so people tackling this issue have a starting point? It's not immediately clear how this would break, since window methods tend to be used in response to user interaction. |
If you need a quick workarond. You can import your slate editor with dynamic imports. import React from 'react'
import dynamic from 'next/dynamic'
const DynamicLoadedEditor = dynamic(
import('./MyAwesomeSlateEditor'),
{
loading: () => (<p>loading...</p>),
ssr: false
}
)
export default DynamicLoadedEditor |
@HaNdTriX Thanks for the idea. |
Just to clarify. I am using slate with next.js and it works like a charm. |
@HaNdTriX Did you have to do anything special to get it to render server side w/ Next.js? I keep getting an error: |
@rkrueger11 yes you need to reset the key generating function. Next.js-ExampleSSR-Exampleimport React from 'react'
import Plain from 'slate-plain-serializer'
import { Editor } from 'slate-react'
import { KeyUtils } from 'slate'
class Index extends React.Component {
constructor (props) {
super(props)
// In order to allow ssr we need to reset the key
// generating function to its initial state.
KeyUtils.resetGenerator()
// Deserialize the initial editor value.
this.state = {
value: Plain.deserialize(
'This is editable plain text, just like a <textarea>!'
)
}
}
render () {
return (
<Editor
placeholder='Enter some plain text...'
value={this.state.value}
onChange={this.onChange}
/>
)
}
onChange = ({ value }) => {
this.setState({ value })
}
}
export default Index @ianstormtaylor I think this issue can be closed. |
Thanks @HaNdTriX! |
There is now an offical next.js example: https://github.com/zeit/next.js/tree/canary/examples/with-slate |
@HaNdTriX It works but when you clear the inputbox and type something on the field again it gives the error: EDIT: Only happens in express custom server.EDIT 2: Fixed! for some weird reason it works when you close chrome dev tools. |
I've got multiple instances of slate on a page, and @HaNdTriX's fix won't work for me. Whenever I try and focus one of the slate instances, it will always focus the first one. https://codesandbox.io/s/zkzx818ryx. I presume this is because the keys are being reused across instances. I'm not sure of a fix that will work with SSR and multiple instances. The global nature of KeyUtils isn't ideal. Any ideas? |
thank, this's really works |
Yeah I'll have to do this for now I think, but it's not really a fix. @ianstormtaylor should I raise another issue for this? |
Just tried the quick fix disabling SSR for the editor component in React, it works! But still waiting for a proper fix that works with SSR @ianstormtaylor The Here is how I quick fix it for now:
|
@HaNdTriX any tips on getting this to work with multiple slate instances? |
@juhaelee the issue is that Slate provides a In the parent that imports this component, pass a different ['first-editor', 'second-editor'].map((name, idx) => <PlainText idFromParentIteration={name + idx} />) And here's a complete example with a custom key generator. import React from "react";
import Plain from "slate-plain-serializer";
import { KeyUtils } from 'slate';
import { Editor } from "slate-react";
const initialValue = Plain.deserialize(
"This is editable plain text, just like a <textarea>!"
);
class PlainText extends React.Component {
constructor(props) {
super(props);
let key = 0;
const keygen = () => {
key += 1;
return props.idFromParentIteration + key; // custom keys
};
KeyUtils.setGenerator(keygen);
}
render() {
return (
<Editor
placeholder="Enter some plain text..."
defaultValue={initialValue}
/>
);
}
}
export default PlainText; |
@rklhui I'm facing the same issue and have to disable SSR to have the editor not crash in editing mode. Did you figure out a different solution? |
What is the solution in Slate 0.50+ now that |
## Changelog - Updated slate.js to version 0.58.4 - Removed unused packages (immutable & slate-plain-serializer) - Simplified example (we don’t need to demonstrate the multi editor case anymore since this issue is now handled by slate internally) - Remove deprecated `KeyUtils` - Removed deprecated Components ## Related: - ianstormtaylor/slate#870
## Changelog - Updated slate.js to version 0.58.4 - Removed unused packages (immutable & slate-plain-serializer) - Simplified example (we don’t need to demonstrate the multi editor case anymore, since this issue is now handled by slate internally) - Remove deprecated `KeyUtils` - Removed deprecated Components ## Related: - ianstormtaylor/slate#870
## Changelog - Updated slate.js to version 0.58.4 - Removed unused packages (immutable & slate-plain-serializer) - Simplified example (we don’t need to demonstrate the multi editor case anymore, since this issue is now handled by slate internally) - Remove deprecated `KeyUtils` - Removed deprecated Components ## Related: - ianstormtaylor/slate#870
Hello, thanks for the great work you are doing here. I have a minor issue, due to get-window (https://github.com/webmodules/get-window) library using in several places in the core (plugins, models, etc.), slate doesn't work in the server-side, get-window using browser only variables like window and document, I understand is using to detect if the window is an iframe.
Best regards.
The text was updated successfully, but these errors were encountered: