Next.js Discord

Discord Forum
\n It works perfectly if i input the URL directly or refresh the page but it doesn't work when I arrive on that page using 's from other pagesI would appreciate if you could point out what is wrong or if there is a better way","dateCreated":"2023-09-26T21:56:09.000Z","answerCount":10,"author":{"@type":"Person","name":"Alano Español"},"acceptedAnswer":{"@type":"Answer","text":"for this specific case, you should use the node version instead: https://www.npmjs.com/package/highlight.js#user-content-nodejs-on-the-server","url":"https://nextjs-forum.com/post/1156348510510268547#message-1156411875500773456","dateCreated":"2023-09-27T02:07:56.000Z","author":{"@type":"Person","name":"riský"},"upvoteCount":1}}}

<script> tag doesn't run presumably because of the client routing

Answered
Alano Español posted this in #help-forum
Open in Discord
Alano EspañolOP
I am using highlight.js to highlight code snippets in my pages
  <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/highlight.min.js"></script>
  <script>hljs.highlightAll();</script>

It works perfectly if i input the URL directly or refresh the page but it doesn't work when I arrive on that page using <Link>'s from other pages
I would appreciate if you could point out what is wrong or if there is a better way
Answered by riský
for this specific case, you should use the node version instead: https://www.npmjs.com/package/highlight.js#user-content-nodejs-on-the-server
View full answer

10 Replies

Alano EspañolOP
https://github.com/vercel/next.js/discussions/17919
^^This discussion seems closely related, will try these once I have the chance
Not sure if it will help with this particular case or if it makes a difference but its worth checking if the solutions from the git issue you provided doesn't help, especially because the discussion is 3y old and still open.
for this specific case, you should use the node version instead: https://www.npmjs.com/package/highlight.js#user-content-nodejs-on-the-server
Answer
you should try your hardest to use native solutions for react (esp when modifying the website)
Alano EspañolOP
Had to use the node version of highlight and run hljs.highlightAll(); in a useffect
It takes a bit of time before code gets highlighted sometimes
But it is good enough for now
Thanks to both of you guys for the assist
Yay!