Next.js Discord

Discord Forum
\\n\" +\n \"\\n\" +\n \"\\n\" +\n \"\\n\" +\n \"\\n\" +\n \"\";\n\n\n return (\n <> \n {someSavedCodeExample}\n \n \n);\n}I want just to append the someSavedCodeExample to the head, and the user takes responsibility for whatever the content he writesI saw this lib:https://github.com/remarkablemark/html-react-parserbut I am looking for a solution without any 3rd party libraries","dateCreated":"2023-08-14T11:42:40.000Z","answerCount":9,"author":{"@type":"Person","name":"Yaman"},"suggestedAnswer":{"@type":"Answer","text":"https://nextjs.org/docs/app/api-reference/components/script","url":"https://nextjs-forum.com/post/1140611443914457171#message-1140611678585757789","dateCreated":"2023-08-14T11:43:36.000Z","author":{"@type":"Person","name":"@ts-ignore"}}}}

how to embed script that saved in my db in nextjs app?

Unanswered
Yaman posted this in #help-forum
Open in Discord
in my application the user can write custom code that will be added to the head of the document, but I am not able to find a way how would I embed the code content in the app, is there any way to do it?
here is example of how it may look like:

function App({ Component, pageProps }: AppProps) {

  const someSavedCodeExample =
      "<!--Start of JS code-->\n" +
      '<script type="text/javascript">\n' +
      '  console.log("test")\n' +
      "</script>\n" +
      "<!--Start of JS code-->\n" +
      "\n" +
      "<!--Start of CSS code-->\n" +
      "<style>\n" +
      " /*CSS code goes here*/\n" +
      "</style>\n" +
      "<!--End of CSS code-->";


  return (
  <>      
      <Head>{someSavedCodeExample}</Head>
      <Component {...pageProps} />
  </>
);
}


I want just to append the someSavedCodeExample to the head,
and the user takes responsibility for whatever the content he writes

I saw this lib:
https://github.com/remarkablemark/html-react-parser

but I am looking for a solution without any 3rd party libraries

9 Replies

but the code is not only one script
I have a code editor in the browser and the user writes whatever code he wants
"you can add your own as children in template strings"
can you give an example of how would I do it?
<Script>{`alert(1)`}</Script>
but my idea the user maybe writes script
and then I will add it to the <Script> tag
I will have then nested scripts...
@joulev I will use html-react-parser it is the best way I found,
but also I have another question now I want to add the same code for the body element how would I do it?
the issue here the user may write any thing
scripts
styles
html code: btns divs etc...
how would I append the code to the bottom of the body element?
I could use _document.tsx
but I have to get the code from the db and my API setup using RTK so I have to but everything inside ReduxProvider
@joulev
Please check this for a better explanation of what I meant:
https://github.com/remarkablemark/html-react-parser/discussions/1015