Open mrmurphy opened 1 year ago
To be able to use the blocks package on the server, you'll also need JSDOM:
https://github.com/ellatrix/patterns/blob/main/data.js#L1-L21
So it seems like this ticket may just apply to the usage of window
instead of globalThis
, so that the code can be imported in non-window contexts, whether or not it will be run. And so that any functions that don't have DOM dependencies can be used.
Outside of that, perhaps there could be a guide in the blocks
or block-editor
README about compatibility with SSR?
I don't think these packages support SSR and that it's ever a priority. Given that react now recommends starting with a server side framework, I think we ultimately should support SSR or at least add notes in the READMEs. If we agree on that, a tracking issue might help!
Description
When I try to import modules from various block-related gutenberg components in non-standard browser environments, I get a bundle failure because the
window
object is not defined. Ideally, I'd like to pre-render block-editor content on the server. Minimally, I'd like to be able to have imports from block-editor related files without breaking my bundler.Here's a list of patches that I found in my experimentation that contain possible fixes:
@wordpress/block-editor
@wordpress/blocks
I'm not sure those are the only places, because I'm running into plenty of other problems from my own code 😄 , but I know at least those are problematic.
This isn't only a problem for SSR, but for Web Workers as well. We currently have to be careful not to import any files that import block-related code into our worker process, or we'll get a runtime error there on initial load.
Step-by-step reproduction instructions
Try to set up a basic block editor in Next.js, or write code in a web worker that uses functions from the
@wordpress/blocks
library, specifically these imports are an example of what we're using:Screenshots, screen recording, code snippet
No response
Environment info
Please confirm that you have searched existing issues in the repo.
Yes
Please confirm that you have tested with all plugins deactivated except Gutenberg.
Yes