Modify html before sending to browser
Unanswered
anjan_kp posted this in #help-forum
anjan_kpOP
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
@anjan_kp 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
anjan_kpOP
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.