Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 2245

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 26, 20242024-11-26T03:37:08+00:00 2024-11-26T03:37:08+00:00

Use Preact in Next.js 13

  • 61k

Introduction

Next.js uses React by default. In this blogpost, I want to replace React with Preact and compare the build differences.

Preact is a JavaScript library, considered the lightweight 3kb alternative of React with the same modern API and ECMA Script support. Preact has the fastest Virtual DOM compared to other JavaScript frameworks. It is small in size. Tiny! It is designed to work in a browser with DOM, which justifies why it is so tiny.

Why Replace React

There are a few reasons why you would do this and there are some other posts that explain it very well.

The key distinction is that Preact lacks several of the experimental features included in React, like Suspense and Concurrent Mode. You have a strong argument for replacing React with Preact if you don't use any of these of the more sophisticated features of React.

I'll summarize some of the reasons you should replace React with Preact.

  1. Preact provides the thinnest possible Virtual DOM abstraction on top of the DOM.
  2. Preact has a small size!
  3. Preact is fast, and not just because of its size.
  4. Preact is highly compatible with React API and supports the same ECMA Script
  5. Preact has a package named preact/compat to let developers use React libraries with Preact.

Learn more about the differences of Preact from React here

Get Started

Let's start by creating a new Next.js app.

  npx create-next-app next-with-preact --use-pnpm   
Enter fullscreen mode Exit fullscreen mode

Next, we need to install Preact in app.

  pnpm install preact   
Enter fullscreen mode Exit fullscreen mode

Setup app Directory in Next.js 13

Next.js 13 introduces layouts with the app directory. To use the app directory, add the following to your next.config.js file.

  module.exports = {   reactStrictMode: true,   swcMinify: true,   experimental: {     appDir: true,   }, };   
Enter fullscreen mode Exit fullscreen mode

In the main directory, create an app directory and a hello directory in the app directory. Add a page.tsx in the hello directory.

The app directory should look like this

  app/ ├── hello │   └── page.tsx ├── head.tsx └── layout.tsx   
Enter fullscreen mode Exit fullscreen mode

The head.tsx and layout.tsx will be generated when you start your server in dev environment.

Add the following to your app/hello/page.tsx

  export default function HelloPage(): JSX.Element {   return <div>Hello from app directory</div>; }   
Enter fullscreen mode Exit fullscreen mode

Replace the contents of pages/index.tsx with a simple page component.

  export default function Home() {   return <div>Index Page from pages directory</div>; }   
Enter fullscreen mode Exit fullscreen mode

Run your app with pnpm dev and inspect http://localhost:3000/ and http://localhost:3000/hello to confirm everything is working.

The Swap

Once everything is working, we need to swap React with Preact in our Next.js app.

We need to tell Next.js that we'd like to swap out React for Preact by using Webpack's aliasing feature only in client production build. The dev server will run with React.

Let's update our next.config.js with the following

  module.exports = {   reactStrictMode: true,   swcMinify: true,   experimental: {     appDir: true,   },    webpack: (config, { dev, isServer }) => {     if (!dev && !isServer) {       Object.assign(config.resolve.alias, {         "react/jsx-runtime.js": "preact/compat/jsx-runtime",         react: "preact/compat",         "react-dom/test-utils": "preact/test-utils",         "react-dom": "preact/compat",       });     }     return config;   }, };   
Enter fullscreen mode Exit fullscreen mode

That's it!

Once you build your Next.js application with next build, you should see a decrease in bundle size. Compare the build results for our demo application.

With Preact
Next.js build with Preact

With React
Next.js build

next-plugin-preact

The Preact Team provides a plugin for use with Next.js. It works in both dev and production environments.

Install the plugin

  pnpm install next-plugin-preact preact react@npm:@preact/compat react-dom@npm:@preact/compat react-ssr-prepass@npm:preact-ssr-prepass preact-render-to-string   
Enter fullscreen mode Exit fullscreen mode

Usage

Use the plugin your next.config.js like this

  // next.config.js const withPreact = require("next-plugin-preact");  module.exports = withPreact({   /* regular next.js config options here */ });   
Enter fullscreen mode Exit fullscreen mode

If you are not using any experimental React features in your Next.js app, I would recommend replacing React with Preact to reduce your bundle size by a considerable chunk.

beginnersjavascriptreactwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.