How to Integrate Prism.js with Next.js 13
Published on 2nd Jun, 2023
3 min read
Prism.js is an open-source JavaScript library that provides developers with a simple yet feature-rich solution for syntax highlighting. Developed by Lea Verou, Prism.js stands out for its lightweight footprint and robust functionality. It allows developers to beautifully highlight code snippets across a wide range of programming languages and markup formats, creating visually stunning and easily readable content.
Yet prism.js is an excellent library for syntax highlighting, it is very tricky and complex to properly integrate it with frameworks like Next.js. So, in this blog post, I will show you some of the best ways to integrate Prism.js with Next.js 13.
Method 1
You can simply import prism js in a client component, and through useEffect()
hook, you can highlight all code blocks at once.
Or,
Import CSS to app/layout.tsx
You can check out all the different language support and themes you add to your site here at Prism.js. I chose the tomorrow theme.
Highlight code through useEffect()
Method 2
The method 1
is not the optimized method and contains some issues. Although it will work fine, it may cause a poor user experience. It causes a hydration error in the browser, as the HTML sent from the server and the final rendered HTML don't match.
However, if you are pre-rendering your site, there is a better alternative. Specifically, we are going to be looking at using data coming from a blog, as this is how this site is generated. We can get rid of the useEffect Prism call in the front-end code, and move all the highlighting work to server-side/build time!
Add some necessary dependencies:
Or,
Import CSS to app/layout.tsx
You can check out all the different language support and themes you add to your site here at Prism.js. I chose the tomorrow theme.
Highlight code in the server
You can use both server components and getServerSideProps()
/ getStaticProps()
Here I showed a server component.
As long as we have the prism CSS in our front-end bundle, we don’t have to use any JavaScript on the front end to get beautiful code highlighting. This creates the most performant front-end experience by allowing us to load fewer dependencies and run less JS in the front end!