简体   繁体   English

如何将 JS 反应页面转换为 HTML 页面

[英]How to convert a JS react page into an HTML page

So I'm working on a CMS project that allows users to create their own websites just like wordpress or other CMS platforms...所以我正在开发一个 CMS 项目,允许用户创建自己的网站,就像 wordpress 或其他 CMS 平台一样......

The users can implement different modals into their websites (text modal, image modal, search modal and other stuff), and then we create an object with the created page infos.用户可以在他们的网站中实现不同的模态(文本模态、图像模态、搜索模态和其他东西),然后我们用创建的页面信息创建一个 object。

The Object contains all the page infos like the example bellow: Object 包含所有页面信息,如下例所示:

{
 pageName: "Home page",
 pageLink: "home",
 pageSlug: "home-page",
 pageMetaDescription: "home meta description",
 pageMetaTitle : "home meta title",
 pageModals : [
  modal1: {
   //modal infos here.
  }
  modal2: {
   //modal infos here.
  }
 ]
}

What I'm doing now is stocking these Objects on a database and when the user requests a page, I fetch the object and then generate a react JS file.我现在正在做的是将这些对象存储在数据库中,当用户请求页面时,我获取 object,然后生成一个 React JS 文件。 But this approach isn't the best for performance or SEO.但这种方法对于性能或 SEO 来说并不是最好的。

So I would like to actually generate an HTML file from these Objects and store them in the database and when the user requests a page, it just loads the generated HTML file instead of fetching the Object and populating a react JS page.所以我想从这些对象实际生成一个 HTML 文件并将它们存储在数据库中,当用户请求页面时,它只是加载生成的 HTML 文件而不是获取 Object 并填充反应 JS 页面。

If you have an Idea or approach to do this, I would like your help.如果您有这样做的想法或方法,我希望得到您的帮助。

Yes you can use Next.js to handle your case Next allow you to fetch some external data and render html based on api result是的,您可以使用 Next.js 来处理您的情况 接下来,您可以获取一些外部数据并根据 api 结果呈现 html

Here an example from documentation adapted for your cases这是根据您的案例改编的文档中的示例

// pagesInfos will be populated at build time by getStaticProps()
function Blog({ pagesInfos }) {
  const { pageSlug, pageMetaDescription , pageMetaTitle ...} = pagesInfos
  return (
    <div>
      <h1>{postMetaTitle}</h1>
      <p>{pageMetaDescription}</p>
    </div>
  )
}

// This function gets called at build time on server-side.
// It won't be called on client-side, so you can even do
// direct database queries.
export async function getStaticProps() {
  // Call an external API endpoint to get posts.
  // You can use any data fetching library
  const res = await fetch('https://.../getPages')
  const pagesInfos = await res.json()

  // By returning { props: { pagesInfos } }, the Blog component
  // will receive `pagesInfos` as a prop at build time
  return {
    props: {
      pagesInfos,
    },
  }
}

export default Blog

Here is full docs: https://nextjs.org/docs/basic-features/data-fetching/get-static-props这是完整的文档: https://nextjs.org/docs/basic-features/data-fetching/get-static-props

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM