Hướng dẫn next js import html file - js tiếp theo nhập tệp html

Thêm văn bản in đậm TextAdd, thêm văn bản in nghiêng,

Thêm báo giá, thêm mã,

Chèn đường dẫn

Thêm một liên kết,

Thêm một danh sách viên đạn, thêm một danh sách được đánh số, thêm một danh sách nhiệm vụ,

Đề cập trực tiếp đến người dùng hoặc TeamReference một vấn đề hoặc yêu cầu kéo

Thêm tiêu đề TextAdd Bold văn bản, thêm văn bản in nghiêng, thêm một danh sách viên đạn, thêm danh sách được đánh số, thêm một danh sách nhiệm vụ,

Phản ứng với ngón tay cái lên biểu tượng cảm xúc đã phản ứng với ngón tay cái xuống biểu tượng cảm xúc đã phản ứng với biểu tượng cảm xúc cười phản ứng với biểu tượng cảm xúc hooray đã phản ứng với biểu tượng cảm xúc bối rối đã phản ứng với biểu tượng cảm xúc trái tim đã phản ứng với biểu tượng cảm xúc tên lửa phản ứng với đôi mắt biểu tượng cảm xúc

Khi ai đó truy cập thư mục gốc của tôi "https://example.com/" Tôi muốn hiển thị một tệp .html bình thường. (index.html) Làm thế nào tôi có thể làm điều đó với Next.js?

Tôi đã thực hiện một nghiên cứu và chỉnh sửa tệp cấu hình của mình như thế này,

/** @type {import('next').NextConfig} */
const nextConfig = {
  reactStrictMode: true,
  rewrites: async () => {
    return [
      {
        source: "/public/myfile.html",
        destination: "/pages/api/myfile.js",
      },
    ];
  },
};

/pages/api/myFile.js

import fs from "fs";
const filename = "/index.html";

export default async function api(req, res) {
  res.setHeader("Content-Type", "text/html; charset=utf-8");
  res.write(await fs.readFileSync(filename, "utf-8"));
  res.end();
}

module.exports = nextConfig;

Nhưng tệp cấu hình này hiển thị trang .html của tôi ngay khi tôi nhấn URL này: https://example.com/index.html

hỏi ngày 6 tháng 4 lúc 15:45Apr 6 at 15:45

Hướng dẫn next js import html file - js tiếp theo nhập tệp html

2

  rewrites: async () => [
    {
      source: "/",
      destination: "/index.html",
    },
  ],

Đã thêm cái này trên tệp.config.js tiếp theo của tôi (trong đối tượng NextConfig)

Tôi có một tệp index.html trên thư mục /công khai của tôi.

Đã trả lời ngày 6 tháng 4 lúc 16:38Apr 6 at 16:38

Hướng dẫn next js import html file - js tiếp theo nhập tệp html

1

Ví dụ

  • Xuất khẩu tĩnh

next export cho phép bạn xuất ứng dụng.JS tiếp theo của mình sang HTML tĩnh, có thể chạy độc lập mà không cần máy chủ Node.js. Nên chỉ sử dụng next export nếu bạn không cần bất kỳ tính năng nào không được hỗ trợ yêu cầu máy chủ.

Nếu bạn đang tìm cách xây dựng một trang web lai trong đó chỉ có một số trang được đặt trước HTML tĩnh, thì Next.js đã tự động thực hiện điều đó. Tìm hiểu thêm về tối ưu hóa tĩnh tự động và tái tạo tĩnh gia tăng.

next export

Cập nhật tập lệnh xây dựng của bạn trong package.json để sử dụng next export:

"scripts": {
  "build": "next build && next export"
}

Chạy npm run build sẽ tạo thư mục

import fs from "fs";
const filename = "/index.html";

export default async function api(req, res) {
  res.setHeader("Content-Type", "text/html; charset=utf-8");
  res.write(await fs.readFileSync(filename, "utf-8"));
  res.end();
}

module.exports = nextConfig;
0.

next export xây dựng phiên bản HTML của ứng dụng của bạn. Trong thời gian

import fs from "fs";
const filename = "/index.html";

export default async function api(req, res) {
  res.setHeader("Content-Type", "text/html; charset=utf-8");
  res.write(await fs.readFileSync(filename, "utf-8"));
  res.end();
}

module.exports = nextConfig;
2,
import fs from "fs";
const filename = "/index.html";

export default async function api(req, res) {
  res.setHeader("Content-Type", "text/html; charset=utf-8");
  res.write(await fs.readFileSync(filename, "utf-8"));
  res.end();
}

module.exports = nextConfig;
3 và
import fs from "fs";
const filename = "/index.html";

export default async function api(req, res) {
  res.setHeader("Content-Type", "text/html; charset=utf-8");
  res.write(await fs.readFileSync(filename, "utf-8"));
  res.end();
}

module.exports = nextConfig;
4 sẽ tạo tệp HTML cho mỗi trang trong thư mục
import fs from "fs";
const filename = "/index.html";

export default async function api(req, res) {
  res.setHeader("Content-Type", "text/html; charset=utf-8");
  res.write(await fs.readFileSync(filename, "utf-8"));
  res.end();
}

module.exports = nextConfig;
5 của bạn (hoặc nhiều hơn cho các tuyến đường động). Sau đó, next export sẽ sao chép các tệp đã xuất vào thư mục chính xác.
import fs from "fs";
const filename = "/index.html";

export default async function api(req, res) {
  res.setHeader("Content-Type", "text/html; charset=utf-8");
  res.write(await fs.readFileSync(filename, "utf-8"));
  res.end();
}

module.exports = nextConfig;
7 sẽ tạo các tệp HTML trong next export thay vì
import fs from "fs";
const filename = "/index.html";

export default async function api(req, res) {
  res.setHeader("Content-Type", "text/html; charset=utf-8");
  res.write(await fs.readFileSync(filename, "utf-8"));
  res.end();
}

module.exports = nextConfig;
2.

Đối với các kịch bản nâng cao hơn, bạn có thể xác định một tham số gọi là

  rewrites: async () => [
    {
      source: "/",
      destination: "/index.html",
    },
  ],
0 trong tệp
  rewrites: async () => [
    {
      source: "/",
      destination: "/index.html",
    },
  ],
1 của bạn để định cấu hình chính xác trang nào sẽ được tạo.

CẢNH BÁO: Sử dụng

  rewrites: async () => [
    {
      source: "/",
      destination: "/index.html",
    },
  ],
0 để xác định các tuyến đường với bất kỳ trang nào được hỗ trợ
import fs from "fs";
const filename = "/index.html";

export default async function api(req, res) {
  res.setHeader("Content-Type", "text/html; charset=utf-8");
  res.write(await fs.readFileSync(filename, "utf-8"));
  res.end();
}

module.exports = nextConfig;
4 hiện đã bị bỏ qua và bị ghi đè. Chúng tôi khuyên bạn nên không sử dụng chúng cùng nhau.
: Using
  rewrites: async () => [
    {
      source: "/",
      destination: "/index.html",
    },
  ],
0 for defining routes with any
import fs from "fs";
const filename = "/index.html";

export default async function api(req, res) {
  res.setHeader("Content-Type", "text/html; charset=utf-8");
  res.write(await fs.readFileSync(filename, "utf-8"));
  res.end();
}

module.exports = nextConfig;
4 powered page is now ignored and gets overridden. We recommend not to use them together.

Các tính năng được hỗ trợ

Phần lớn các tính năng cốt lõi tiếp theo cần thiết để xây dựng một trang web tĩnh được hỗ trợ, bao gồm:

  • Các tuyến đường động khi sử dụng
    import fs from "fs";
    const filename = "/index.html";
    
    export default async function api(req, res) {
      res.setHeader("Content-Type", "text/html; charset=utf-8");
      res.write(await fs.readFileSync(filename, "utf-8"));
      res.end();
    }
    
    module.exports = nextConfig;
    
    4
  • Mở trước với
      rewrites: async () => [
        {
          source: "/",
          destination: "/index.html",
        },
      ],
    
    5
  • Tải trước JavaScript
  • Nhập khẩu động
  • Bất kỳ tùy chọn kiểu dáng nào (ví dụ: các mô-đun CSS, STEPED-JSX)
  • Dữ liệu phía máy khách tìm nạp
  • import fs from "fs";
    const filename = "/index.html";
    
    export default async function api(req, res) {
      res.setHeader("Content-Type", "text/html; charset=utf-8");
      res.write(await fs.readFileSync(filename, "utf-8"));
      res.end();
    }
    
    module.exports = nextConfig;
    
    3
  • import fs from "fs";
    const filename = "/index.html";
    
    export default async function api(req, res) {
      res.setHeader("Content-Type", "text/html; charset=utf-8");
      res.write(await fs.readFileSync(filename, "utf-8"));
      res.end();
    }
    
    module.exports = nextConfig;
    
    4
  • Tối ưu hóa hình ảnh bằng trình tải tùy chỉnh

Các tính năng không được hỗ trợ

Các tính năng yêu cầu máy chủ Node.js hoặc logic động không thể được tính toán trong quá trình xây dựng, không được hỗ trợ:

  • Tối ưu hóa hình ảnh (Trình tải mặc định)
  • Định tuyến quốc tế hóa
  • Tuyến API
  • Viết lại
  • Chuyển hướng
  • Tiêu đề
  • Phần mềm trung gian
  • Tái tạo tĩnh gia tăng
  •   rewrites: async () => [
        {
          source: "/",
          destination: "/index.html",
        },
      ],
    
    8
  •   rewrites: async () => [
        {
          source: "/",
          destination: "/index.html",
        },
      ],
    
    9

import fs from "fs"; const filename = "/index.html"; export default async function api(req, res) { res.setHeader("Content-Type", "text/html; charset=utf-8"); res.write(await fs.readFileSync(filename, "utf-8")); res.end(); } module.exports = nextConfig; 7

Có thể sử dụng API

import fs from "fs";
const filename = "/index.html";

export default async function api(req, res) {
  res.setHeader("Content-Type", "text/html; charset=utf-8");
  res.write(await fs.readFileSync(filename, "utf-8"));
  res.end();
}

module.exports = nextConfig;
7 thay vì
import fs from "fs";
const filename = "/index.html";

export default async function api(req, res) {
  res.setHeader("Content-Type", "text/html; charset=utf-8");
  res.write(await fs.readFileSync(filename, "utf-8"));
  res.end();
}

module.exports = nextConfig;
3, nhưng nó đi kèm với một vài cảnh báo:

  • import fs from "fs";
    const filename = "/index.html";
    
    export default async function api(req, res) {
      res.setHeader("Content-Type", "text/html; charset=utf-8");
      res.write(await fs.readFileSync(filename, "utf-8"));
      res.end();
    }
    
    module.exports = nextConfig;
    
    7 không thể được sử dụng cùng với
    import fs from "fs";
    const filename = "/index.html";
    
    export default async function api(req, res) {
      res.setHeader("Content-Type", "text/html; charset=utf-8");
      res.write(await fs.readFileSync(filename, "utf-8"));
      res.end();
    }
    
    module.exports = nextConfig;
    
    3 hoặc
    import fs from "fs";
    const filename = "/index.html";
    
    export default async function api(req, res) {
      res.setHeader("Content-Type", "text/html; charset=utf-8");
      res.write(await fs.readFileSync(filename, "utf-8"));
      res.end();
    }
    
    module.exports = nextConfig;
    
    4 trên bất kỳ trang nào. Nếu bạn có các tuyến đường động, thay vì sử dụng
    import fs from "fs";
    const filename = "/index.html";
    
    export default async function api(req, res) {
      res.setHeader("Content-Type", "text/html; charset=utf-8");
      res.write(await fs.readFileSync(filename, "utf-8"));
      res.end();
    }
    
    module.exports = nextConfig;
    
    4, bạn sẽ cần định cấu hình tham số
      rewrites: async () => [
        {
          source: "/",
          destination: "/index.html",
        },
      ],
    
    0 trong tệp
      rewrites: async () => [
        {
          source: "/",
          destination: "/index.html",
        },
      ],
    
    1 của mình để cho nhà xuất khẩu biết các tệp HTML nào sẽ xuất ra.
  • Khi
    import fs from "fs";
    const filename = "/index.html";
    
    export default async function api(req, res) {
      res.setHeader("Content-Type", "text/html; charset=utf-8");
      res.write(await fs.readFileSync(filename, "utf-8"));
      res.end();
    }
    
    module.exports = nextConfig;
    
    7 được gọi trong quá trình xuất, các trường next export0 và next export1 của tham số next export2 của nó sẽ là các đối tượng trống, vì trong quá trình xuất, không có máy chủ nào chạy.
  • import fs from "fs";
    const filename = "/index.html";
    
    export default async function api(req, res) {
      res.setHeader("Content-Type", "text/html; charset=utf-8");
      res.write(await fs.readFileSync(filename, "utf-8"));
      res.end();
    }
    
    module.exports = nextConfig;
    
    7 sẽ được gọi trên mọi điều hướng phía khách hàng, nếu bạn chỉ muốn tìm nạp dữ liệu theo thời gian xây dựng, hãy chuyển sang
    import fs from "fs";
    const filename = "/index.html";
    
    export default async function api(req, res) {
      res.setHeader("Content-Type", "text/html; charset=utf-8");
      res.write(await fs.readFileSync(filename, "utf-8"));
      res.end();
    }
    
    module.exports = nextConfig;
    
    3.will be called on every client-side navigation, if you'd like to only fetch data at build-time, switch to
    import fs from "fs";
    const filename = "/index.html";
    
    export default async function api(req, res) {
      res.setHeader("Content-Type", "text/html; charset=utf-8");
      res.write(await fs.readFileSync(filename, "utf-8"));
      res.end();
    }
    
    module.exports = nextConfig;
    
    3.
  • import fs from "fs";
    const filename = "/index.html";
    
    export default async function api(req, res) {
      res.setHeader("Content-Type", "text/html; charset=utf-8");
      res.write(await fs.readFileSync(filename, "utf-8"));
      res.end();
    }
    
    module.exports = nextConfig;
    
    7 nên tìm nạp từ API và không thể sử dụng các thư viện cụ thể của Node.js hoặc hệ thống tệp như
    import fs from "fs";
    const filename = "/index.html";
    
    export default async function api(req, res) {
      res.setHeader("Content-Type", "text/html; charset=utf-8");
      res.write(await fs.readFileSync(filename, "utf-8"));
      res.end();
    }
    
    module.exports = nextConfig;
    
    3 có thể.

Chúng tôi khuyên bạn nên di chuyển đến

import fs from "fs";
const filename = "/index.html";

export default async function api(req, res) {
  res.setHeader("Content-Type", "text/html; charset=utf-8");
  res.write(await fs.readFileSync(filename, "utf-8"));
  res.end();
}

module.exports = nextConfig;
3 qua
import fs from "fs";
const filename = "/index.html";

export default async function api(req, res) {
  res.setHeader("Content-Type", "text/html; charset=utf-8");
  res.write(await fs.readFileSync(filename, "utf-8"));
  res.end();
}

module.exports = nextConfig;
7 bất cứ khi nào có thể.


Nhập JS HTML tiếp theo như thế nào?

Vì điều đó, tôi đã làm như sau:..
Tạo một tệp HTML. Đầu tiên, tạo một tệp HTML nói myfile.html với nội dung mong muốn của bạn ....
Tạo API. Tạo một API thư mục mới bên trong thư mục trang của bạn và tạo myfile.js bên trong nó với nội dung sau: nhập fs từ "fs"; ....
Cập nhật tiếp theo. Cấu hình. ....
Bước cuối cùng ..

JS tiếp theo có sử dụng HTML không?

Theo mặc định, NEXT.js Ghế trình tạo trước mỗi trang.Điều này có nghĩa là Next.js tạo HTML cho mỗi trang trước, thay vì tất cả được thực hiện bởi JavaScript phía máy khách.Next.js generates HTML for each page in advance, instead of having it all done by client-side JavaScript.

Tại sao JS tiếp theo không có chỉ mục HTML?

JS, bạn không thực sự tạo các trang HTML.Thay vào đó, bạn đang tạo ra các tài sản sản xuất mà tiếp theo.Máy chủ JS sau đó sẽ phục vụ cho khách truy cập.Đó là lý do tại sao bạn không nhìn thấy một chỉ mục.you aren't actually creating the HTML pages. Instead, you're creating the production assets that the Next. js server will then serve to visitors. That's why you're not seeing an index.

JS tiếp theo không tốt cho?

Mặc dù NextJS đang phát triển nhanh chóng và nhiều tính năng đến, nhưng nó vẫn có một số nhược điểm và vấn đề mà bạn có thể thấy dưới đây: Chi phí linh hoạt-JS Next không cung cấp nhiều trang phía trước tích hợp, vì vậy bạn phải tạo toàn bộ lớp mặt trướctừ mặt đất lên.Cost of flexibility – Next JS does not provide many built-in front pages, so you have to create the whole front-end layer from the ground up.