Skip to content

fix: 1373 storyblokrichtext blok resolver not working #1388

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

alvarosabu
Copy link
Contributor

  • Introduced new components for server-side rich text rendering.
  • Updated existing components and pages to utilize the new rich text features.
  • Enhanced the layout and styling of the playground for better user experience.
  • Update Next.js playground with Tailwind CSS and rich text enhancements
  • Added Tailwind CSS dependencies for styling.

- Added new custom blok to test
- Added tailwindcss for styling
- Introduced new components for server-side rich text rendering.
- Updated existing components and pages to utilize the new rich text features.
- Enhanced the layout and styling of the playground for better user experience.
- Update Next.js playground with Tailwind CSS and rich text enhancements
- Added Tailwind CSS dependencies for styling.
- Added a missing semicolon to the `@apply m-0` rule for emoji images in `globals.css` and `styles.css` to ensure proper CSS parsing.
- Removed an unused import of `EmojiRandomizer` in `page.tsx` to clean up the codebase.
@alvarosabu alvarosabu added the bugfix [PR] Fixes a bug label Mar 27, 2025
@alvarosabu alvarosabu requested a review from edodusi March 27, 2025 08:42
Copy link

pkg-pr-new bot commented Mar 27, 2025

Open in StackBlitz

npm i https://pkg.pr.new/@storyblok/react@1388

commit: f747adf

<StoryblokServerRichText doc={story.content.richText} />
</div>
);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On this example we are using the rsc module which is meant for Next.js App Router, but the code does not reflect the changes in the DX (they are documented in the README).

So either we change the example detailing that this is for App Router and reuse the same code, or we document that this is a non-strict example that only details how to use the StoryblokServerRichText component but everything else needs to be adapted to the user's context.

Or you can have more control by using the `useStoryblokServerRichText` hook:

```ts
import { useStoryblokServerRichText, convertAttributesInElement } from '@storyblok/react';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are them not exported from the rsc module?

export function componentResolver(node: StoryblokRichTextNode<React.ReactElement>): React.ReactElement {
const body = node?.attrs?.body;
return React.createElement(StoryblokServerComponent, {
blok: Array.isArray(body) ? body[0] : undefined,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could still be an empty array

Suggested change
blok: Array.isArray(body) ? body[0] : undefined,
blok: Array.isArray(body) && body.length > 0 ? body[0] : undefined,

const body = node?.attrs?.body;
return React.createElement(StoryblokServerComponent, {
blok: Array.isArray(body) ? body[0] : undefined,
key: node.attrs?.id,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Risk of an undefined key

Suggested change
key: node.attrs?.id,
key: node.attrs?.id || `storyblok-component-${Date.now()}`,

...options.resolvers,
},
keyedResolvers: true,
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about the other options? You are only using options.resolvers instead of merging the whole options parameter

import { useStoryblokServerRichText } from './server-richtext';

const StoryblokRichText = forwardRef<HTMLDivElement, StoryblokRichTextProps>(
({ doc, resolvers }, ref) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest to return immediately if we don't have the doc, something like

    if (!doc) {
      return null;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugfix [PR] Fixes a bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants