Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

c a
c a

Posted on • Originally published atchrisdevcode.hashnode.dev on

     

How To Convert HTML templates to NextJS (v13)

Now that Next.JS is an awesome frontend tool. How can we get HTML/CSS/JS Templates to work perfectly in a Next.JS project? This article will take you through the steps.

Requirements

To fully grasp the content, its highly recommended that you have the following tools installed on your development environment:

  1. NodeJS

  2. Text Editor (Maybe VIM?)

  3. Basic React.js and Next.JS knowledge

Finding a Template

For the demo, we will use one of the open-sourced templates fromStartbootstrap. We will use the Freelancer theme, a perfect template for a personal portfolio website.

https://startbootstrap.com/theme/freelancer

The template can be downloaded using thelink and also cloned from the GitHub repository:

https://github.com/startbootstrap/startbootstrap-freelancer

Note: Feel free to use any template.

Creating a Next.JS Project

To start a new project, open up a command line application and run the script:

    npx create-next-app@latest my-portfolio
Enter fullscreen modeExit fullscreen mode

Note:my-portfoliocan be replaced with the name of the project.

Note that we get some prompts for Next.JS configuration:

     Would you like to use TypeScript with this project? No / Yes     Would you like to use ESLint with this project? No / Yes     Would you like to use src/ directory with this project? No / Yes     Would you like to use experimental app/ directory with this project? No / Yes     What import alias would you like configured? @/*
Enter fullscreen modeExit fullscreen mode

Follow along with thecommit

After the installation, run the development server using the command:

    npm run dev
Enter fullscreen modeExit fullscreen mode

Next.JS development server runs onlocalhost:3000

The HTML Template Structure

The HTML template consists of the basic HTML, CSS, and JS files.

    .     assets        favicon.ico        img            avataaars.svg            portfolio                cabin.png                cake.png                circus.png                game.png                safe.png                submarine.png     css        styles.css     index.html     js         scripts.js    5 directories, 11 files
Enter fullscreen modeExit fullscreen mode

To successfully convert the project into Next.JS, follow the steps below:

Adding CSS and Assets

Inside the template directory, copy thestyles.css file inside thecss/ folder, and paste it into the Next.JS project:src/styles.

After that, we import the styles into our project by adding the following line inside thesrc/pages/_app.js file:

Follow along with thecommit.

Back to the template folder, copy theassets folder into the Next.JSpublic folder.

Follow along with thecommit.

One final step copying the JS folder from the template directory to Next.JS public folder.

Follow along with thecommit.

Adding Custom JavaScript file to Next.JS

Next.JS has made it easier to use custom script files, usingCustom**document**

https://nextjs.org/docs/advanced-features/custom-document

The default_document.js file has the following content:

    import { Html, Head, Main, NextScript } from 'next/document'    export default function Document() {      return (        <Html lang="en">          <Head />          <body>            <Main />            <NextScript />          </body>        </Html>      )    }
Enter fullscreen modeExit fullscreen mode

To add the script, we add the following inside the body tag:

    {/* Custom JS files */}    <script    async    src='https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js'    ></script>    {/* theme js */}    <script async src='/js/scripts.js'></script>    {/* startbootstrap forms */}    <script    async    src='https://cdn.startbootstrap.com/sb-forms-latest.js'    ></script>
Enter fullscreen modeExit fullscreen mode

The updated file will now contain:

    import { Html, Head, Main, NextScript } from 'next/document'    export default function Document() {      return (        <Html lang='en'>          <Head />          <body>            <Main />            <NextScript />            {/* Custom JS files */}            <script              async              src='https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js'            ></script>            {/* theme js */}            <script async src='/js/scripts.js'></script>            {/* startbootstrap forms */}            <script              async              src='https://cdn.startbootstrap.com/sb-forms-latest.js'            ></script>          </body>        </Html>      )    }
Enter fullscreen modeExit fullscreen mode

Follow along with thecommit

Adding Page Content

First, we clean thesrc/pages/index.js file to contain the following:

    import Head from 'next/head'    import Image from 'next/image'    export default function Home() {      return (        <>          <Head>            <title>Freelancer Portfolio</title>            <meta name='description' content='Generated by create next app' />            <meta name='viewport' content='width=device-width, initial-scale=1' />            <link rel='icon' href='/favicon.ico' />          </Head>        </>      )    }
Enter fullscreen modeExit fullscreen mode

The next steps will be adding the page content from theindex.html file in the template to theindex.js file in our Next.JS project.

The content includes everything inside thebody tag, without thescripts tag:

https://gist.github.com/achingachris/805284613a19305a2ee98fc59bc1098e

To add the code, we have to use JSX syntax. To make our work easier, we will use a tool to convert HTML to JSX:

https://transform.tools/html-to-jsx

So well paste the HTML into to the site and convert it to JSX. This is the updated file:

https://gist.github.com/achingachris/45927f894e06f4c360bb4bcf57801064

Follow along with thecommit.

Conclusion

The article gives the most important yet basic procedure to use when converting an HTML template to Next.JS

https://github.com/achingachris/convert-html-to-nextjs

Top comments(1)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
yusufjon1997 profile image
Dev Bro
  • Joined

I have got hydration error when I clone and build your next app. I am also facing with this issue. If you find a solution please share with me

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Python(Django). JS. Software Eng
  • Location
    Kenya
  • Education
    Software Engineering
  • Work
    Software Eng Lead
  • Joined

More fromc a

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp