Modify html before sending to browser
Unanswered
Saltwater Crocodile posted this in #help-forum
Saltwater CrocodileOP
I have a requirement to add a superscript tag for all registered marks in a page. I need to run a string replace regex on the final html generated by nextjs server. How can I achieve this?
5 Replies
@Saltwater Crocodile I have a requirement to add a superscript tag for all registered marks in a page. I need to run a string replace regex on the final html generated by nextjs server. How can I achieve this?
Maybe you can use middleware, and use
response.transformData
?wait I don't think thats available
I think you can wrap everything in a server component, by converting the
children
propWhat you can do is wrap something like
and SuperscriptRegistered
You can wrap it around every text.
Or you can use a similar approach to wrap everything, but I'm not sure if it'll work, as it'll be too complex
<SuperscriptRegistered>
We love BrandX® and BrandY®. The BrandZ® series is coming soon.
</SuperscriptRegistered>
and SuperscriptRegistered
import React from 'react';
type Props = {
children: string;
};
export default function SuperscriptRegistered({ children }: Props) {
// Replace all occurrences of ® with superscript
const replaced = children.replace(/®/g, '<sup>®</sup>');
return (
<span dangerouslySetInnerHTML={{ __html: replaced }} />
);
}
You can wrap it around every text.
Or you can use a similar approach to wrap everything, but I'm not sure if it'll work, as it'll be too complex
Saltwater CrocodileOP
I am already doing something similar. But I don't want to add it everywhere in code.. the best solution can be to modify in an endware but looks like nextjs doesn't allow it. They should think of adding such feature.