gatsby_Gatsby更快的WordPress网站

news/2024/5/9 4:02:56/文章来源:https://blog.csdn.net/cukw6666/article/details/107983515

gatsby

Talk about the latest cutting-edge web technologies, and you find some impressive names like React.js, Vue.js, Next.js, and so on. They have opened new gateways and approaches to building websites that can serve content to users quickly.

谈论最新的前沿Web技术,您会发现一些令人印象深刻的名称,例如React.js,Vue.js,Next.js等。 他们打开了新的门户网站和方法来构建可以向用户快速提供内容的网站。

One framework that can increase overall site speed and load times is Gatsby.js.

Gatsby.js是可以提高整体网站速度和加载时间的一种框架 。

快速网站的目标 (Goals of a Fast Website)

A website that loads fast entails the following perks and benefits for developers:

快速加载的网站为开发人员带来以下好处和好处:

  • Traffic and Engagement: You get more site visitors on a fast site, which translates to better ROI and user engagement.

    流量和参与度 :快速网站上吸引了更多网站访问者,这意味着更高的投资回报率和用户参与度。

  • Page Ranks: Fast websites earn higher browser rankings.

    页面排名 :快速网站在浏览器中的排名更高。

使用Gatsby.js和WordPress提高网站速度 (Improving Site Speed With Gatsby.js and WordPress)

Gatsby.js is a free and open-source React.js-based framework that helps developers build websites and applications. It is part of a general trend toward JAMstack (JavaScript APIs Markup) sites, which aim to increase overall site speed and load times.

Gatsby.js是一个免费的基于开源React.js的框架,可帮助开发人员构建网站和应用程序。 这是向JAMstack(JavaScript API标记)网站发展的总体趋势的一部分,该网站旨在提高网站的整体速度和加载时间。

But where does Gatsby fit with WordPress, one of the most widely-used CMS options available today? Despite its usability and robust community, WordPress can pose challenges to building a modern frontend, for the following reasons:

但是,盖茨比在哪里适合WordPress(今天可用的最广泛使用的CMS选项之一)? 尽管具有可用性和强大的社区,但出于以下原因,WordPress可能会给构建现代前端带来挑战:

  • Updates and Changes: WordPress is regularly updated, but it still lacks parity with other rapidly-changing front-end technologies. Staying up-to-date with these technologies adds an additional burden for the developer.

    更新和变更 :WordPress会定期更新,但仍与其他快速变化的前端技术缺乏同等性。 紧跟这些技术的最新发展为开发人员增加了额外的负担。

  • Continuous Integration and Deployment: Right now, few Continuous Integration/Deployment (CI/CD) options exist in the WordPress ecosystem.

    持续集成和部署 :目前,WordPress生态系统中几乎没有持续集成/部署(CI / CD)选项。

  • Integration Costs: It can be challenging to integrate some of the latest front-end technologies with a WordPress application.

    集成成本 :将一些最新的前端技术与WordPress应用程序集成可能是一个挑战。

Using Gatsby can address some of these limitations. In the following steps, we will show you how to integrate Gatsby with WordPress to take full advantage of both. First, we are going to configure a basic Gatsby project setup. Then, we’ll use it to fetch data from our WordPress site.

使用盖茨比可以解决其中一些限制。 在以下步骤中,我们将向您展示如何将Gatsby与WordPress集成以充分利用两者。 首先,我们将配置基本的盖茨比项目设置。 然后,我们将使用它来从WordPress网站获取数据。

将Gatsby.js与WordPress集成 (Integrating Gatsby.js with WordPress)

First, we will set up a demo Gatsby project.

首先,我们将建立一个Gatsby示范项目。

Install the Gatsby CLI by typing the following command in your terminal:

通过在终端中键入以下命令来安装Gatsby CLI:

  • npm install -g gatsby-cli

    npm install -g gatsby-cli

Next, create a new Gatsby site with the following command:

接下来,使用以下命令创建一个新的Gatsby网站:

  • gatsby new site-name

    gatsby新站点名称

To access your site folder contents, type the following:

要访问您的站点文件夹内容,请键入以下内容:

  • cd site-name

    cd 站点名称

Finally, start the development server to begin building your site:

最后,启动开发服务器以开始构建您的站点:

  • gatsby develop

    盖茨比开发

安装gatsby-source-wordpress插件 (Installing the gatsby-source-wordpress Plugin)

Assuming that you have a WordPress site already set up, and you would like to have a frontend built with Gatsby.js, all you need to do is pull your existing site data into your static Gatsby site. You can do that with the gatsby-source-wordpress plugin.

假设您已经建立了一个WordPress网站,并且希望使用Gatsby.js构建前端,那么您要做的就是将现有的网站数据拉入静态的Gatsby网站。 您可以使用gatsby-source-wordpress插件来做到这一点。

This tutorial uses the default WordPress REST API site, though you are free to use a pre-existing WordPress setup if you have one.

本教程使用默认的WordPress REST API站点 ,但是如果您有的话,可以自由使用现有的WordPress设置。

Inside your terminal, type the following to install this plugin:

在终端内,键入以下内容以安装此插件:

  • npm install gatsby-source-wordpress

    npm安装gatsby-source-wordpress

配置插件 (Configuring the Plugin)

Inside your gatsby-config.js file, the main Gatsby configuration file, you will add some WordPress-specific configuration options. These include your WordPress site’s baseUrl, the preferred HTTP protocol, and settings pertaining to the Advanced Custom Fields (ACF) plugin. The includedRoutes fields specifies the data we want to fetch.

在您的主要盖茨比( gatsby-config.js配置文件gatsby-config.js文件中,您将添加一些特定于WordPress的配置选项。 其中包括WordPress网站的baseUrl ,首选HTTP protocol以及与“ 高级自定义字段(ACF)插件”有关的设置。 includedRoutes字段指定我们要获取的数据。

Using the demo WordPress site from the step above, the current frontend looks like this:

使用上面步骤中的演示WordPress网站,当前前端如下所示:

For the purposes of this tutorial, add the following code to a file called gatsby-config.js:

就本教程而言,将以下代码添加到名为gatsby-config.js的文件中:

module.exports = {// ...plugins: [// ...{resolve: `gatsby-source-wordpress`,options: {// Your WordPress source.baseUrl: `demo.wp-api.org`,protocol: `https`,// Only fetches posts, tags and categories from the baseUrl.includedRoutes: ['**/posts', '**/tags', '**/categories'],// Not using ACF so putting it off.useACF: false}},],
}

使用提取的WordPress数据 (Using Fetched WordPress Data)

Once your Gatsby site is fetching data from your WordPress source URL, it’s time to create your site pages. This is done by implementing the createPages API in the gatsby-node.jsfile, which makes the fetched data available to queries from GraphQL. At build time, the gatsby-source-wordpress plugin fetches your data, and uses it to ”automatically infer a GraphQL schema” which you can query against.

一旦Gatsby网站从WordPress源URL提取数据,就该创建网站页面了。 这是通过在gatsby-node.js文件中实现createPages API来完成的,该文件使获取的数据可用于GraphQL的查询。 在构建时, gatsby-source-wordpress插件获取您的数据,并使用它“自动推断一个GraphQL模式”,您可以对其进行查询。

Add the following code to a file called gatsby-node.js:

将以下代码添加到名为gatsby-node.js的文件中:

/*** Implement Gatsby's Node APIs in this file.** See: https://www.gatsbyjs.org/docs/node-apis/*/// You can delete this file if you're not using itconst path = require(`path`);
const slash = require(`slash`);/** STEP #1: Implement the Gatsby API “createPages”. This is* called after the Gatsby bootstrap is finished so you have* access to any information necessary to programmatically* create pages.* Will create pages for WordPress pages (route : /{slug})* Will create pages for WordPress posts (route : /post/{slug})*/
exports.createPages = async ({ graphql, actions }) => {const { createPage } = actions;// STEP #2: Query all WordPress Posts Data./** The “graphql” function allows us to run arbitrary* queries against the local Gatsby GraphQL schema. Think of* it like the site has a built-in database constructed*  from the fetched data that you can run queries against.*/const result = await graphql(`{allWordpressPost {edges {node {idslugstatustemplateformat}}}}`);// Check for any errorsif (result.errors) {throw new Error(result.errors);}// Access query results via object destructuring.const { allWordpressPost } = result.data;const postTemplate = path.resolve(`./src/templates/post.js`);// STEP #3: Create pages in Gatsby with WordPress Posts Data./*** We want to create a detailed page for each* post node. We'll just use the WordPress Slug for the slug.* The Post ID is prefixed with 'POST_'*/allWordpressPost.edges.forEach(edge => {createPage({path: `/${edge.node.slug}/`,component: slash(postTemplate),context: {id: edge.node.id}});});
};

This will iterate through existing WordPress post data.

这将遍历现有的WordPress发布数据。

步骤#4:创建post.js模板 (Step #4: Creating a post.js Template)

Next, we will create a folder for templates, where you can add files for posts, pages, and layouts. For now, we will create a post.js file to fetch posts from our WordPress site.

接下来,我们将为模板创建一个文件夹,您可以在其中添加文章,页面和布局的文件。 现在,我们将创建一个post.js文件以从WordPress网站获取帖子。

Add the following code to the file:

将以下代码添加到文件中:

import { graphql } from 'gatsby';
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import Layout from '../layouts';class PostTemplate extends Component {render() {const post = this.props.data.wordpressPost;// STEP #5: Use title and content in Gatsby.return (<Layout><h1 dangerouslySetInnerHTML={{ __html: post.title }} /><div dangerouslySetInnerHTML={{ __html: post.content }} /></Layout>);}
}PostTemplate.propTypes = {data: PropTypes.object.isRequired,edges: PropTypes.array
};export default PostTemplate;// STEP #4: Get current WP Post data via ID.
export const pageQuery = graphql`query($id: String!) {wordpressPost(id: { eq: $id }) {titlecontent}}
`;

检查最终结果 (Examining the Final Result)

To start the development server and view the final result, type the following command in your terminal:

要启动开发服务器并查看最终结果,请在终端中键入以下命令:

  • npm start

    npm开始

You will get the link where you can access the site locally, along with other details like the number of posts, categories, and tags that are being fetched.

您将获得一个链接,您可以在其中本地访问该站点,以及其他详细信息,例如正在获取的posts数, categoriestags

Here’s a GIF that demonstrates what this will look like:

这是一个GIF,展示了它的外观:

Let’s take a look at this revamped front-end which is now powered with Gatsby.js and a WordPress back-end:

You can see how our application is only fetching the required data from the WordPress site. This includes posts, tags and categories. To retrieve other types of data like widgets or comments, you will need to add the appropriate values to the includedRoutes option.

您可以看到我们的应用程序如何仅从WordPress网站获取所需数据。 这包括poststagscategories 。 要检索其他类型的数据(例如小部件或注释),您需要将适当的值添加到includedRoutes选项。

结论 (Conclusion)

By following this tutorial, you now have a WordPress application backend integrated with a Gatsby.js frontend. Gatsby provides a fast web experience and brings with it added benefits that can enhance your existing WordPress site. You now have a platform for further experimentation with using Gatsby to power your site,

通过遵循本教程,您现在拥有了一个与Gatsby.js前端集成的WordPress应用程序后端。 Gatsby提供了快速的Web体验,并带来了额外的好处,可以增强您现有的WordPress网站。 现在,您有了使用Gatsby为网站提供动力的进一步试验平台,

翻译自: https://www.digitalocean.com/community/tutorials/faster-wordpress-sites-with-gatsby

gatsby

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.luyixian.cn/news_show_818884.aspx

如若内容造成侵权/违法违规/事实不符,请联系dt猫网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

从“奥运门票网站800万访问量”想到的成本、质量、进度、风险等关系

看到这个新闻后颇有一番滋味&#xff0c;更体验到我一直关注的软件性能着实无处不在&#xff0c;这个案例也确实值得我们好好反思一下。不知道网友们有没有报名参加国际日语考试的——这个网站每年05年前都有人不能报名&#xff0c;因为报名人数太多&#xff0c;所以报名当日大…

Web2.0网站的路径依赖--探讨蚂蚁社区为什么要导入博客

郑昀 2007-5-13以下文章即将讨论的问题涉及这几个关键词&#xff1a;l 网站启动的核心用户从哪里来&#xff1f;l 网站启动时需要什么样的种子用户&#xff1f;l 最初吸引来的种子用户对网站有什么路径依赖作用&#xff1f;麦田曾经问道&#xff1a;“4. …

郑昀邀请网站开发工程师架构师加盟Web3.0新锐网站[工作地点-北京财智国际大厦]

公司新年新气象&#xff0c;刚刚换到了一个大办公室&#xff0c;也离中关村更近了。随着公司规模的扩大&#xff0c;开始了新一轮的招兵买马&#xff0c;哈哈。请各位帮忙看看有没有合适的人选推荐&#xff0c;不胜感谢&#xff01;招聘:研发部招聘岗位1&#xff1a;PHP网站架构…

大型网站架构与分布式架构

解决问题的通用思路是将分而治之&#xff08;divide-and-conquer&#xff09;&#xff0c;将大问题分为若干个小问题&#xff0c;各个击破。在大型互联网的架构实践中&#xff0c;无一不体现这种思想。架构目标低成本:任何公司存在的价值都是为了获取商业利益。在可能的情况下&…

高性能网站性能优化

从LiveJournal后台发展看大规模网站性能优化方法于敦德 2006-3-16一、LiveJournal发展历程LiveJournal是99年始于校园中的项目&#xff0c;几个人出于爱好做了这样一个应用&#xff0c;以实现以下功能&#xff1a;博客&#xff0c;论坛 社会性网络&#xff0c;找到朋友 聚合&am…

JS实现网站悬浮广告

如图黄色区块会沿着浏览器的上下左右碰撞移动,可关闭,鼠标移上去会停止 <!doctype html> <html> <he><meta charset"utf-8"><title>广告</title><style type"text/css">*{pding:0px;margin:0px;}#ad{position:a…

一步一步SharePoint 2007之十七:解决实现Form认证后无法再用SharePoint Designer编辑网站的问题...

一步一步SharePoint 2007之十七&#xff1a;解决实现Form认证后无法再用SharePoint Designer编辑网站的问题摘要前面的文章中&#xff0c;我曾提到&#xff0c;Form认证目前还有缺陷&#xff0c;就是实现Form认证后&#xff0c;就不可以用SharePoint Designer编辑网站了。这点真…

[导入]谈谈网站静态化

摘要: 写在前头静态化是解决减轻网站压力,提高网站访问速度的常用方案,但在强调交互的We2.0 时代,对静态化提出了更高的要求,静态不仅要能静,还要能动,下面我通过一个项目,谈谈网站静态化后的架构设计方案,同时和大家探讨一下,在开源产品大行其道,言架构必称MemberCache, Nginx…

如何获取视频网站的信息(土豆例子)

现在不少的视频网站都提供转载视频的功能 我之前写过一篇CHSNS#中通过youku的视频网址获取视频截图及视频信息&#xff0c;虽然可能地址已经改了但却未涉及土豆网&#xff0c;其实土豆获取的方法与优酷是差不多的。 http://www.tudou.com/ 其中的一个视频 http://www.tudou.com…

h5游戏网站源码_从WEB前端角度看H5游戏开发

WEB开发与游戏开发的区别WEB前端的大部分工作集中在利用现有的主流前端框架&#xff08;vue / react / angular&#xff09;及其周边开源代码库生态组织整个项目的架构并实现业务逻辑代码&#xff0c;往往同一种逻辑可以选择用不同的抽象方式来实现&#xff0c;不同抽象方式的思…

浅谈网站架构分析

前后端分离&#xff1a;在前后端不分离的应用模式中&#xff0c;前端页面看到的效果都是由后端控制&#xff0c;由后端渲染页面或重定向&#xff0c;也就是后端需要控制前端的展示&#xff0c;前端与后端的耦合度很高。 核心&#xff1a;后端只需要通过接口发送json数据&#…

给图片下方加水印_批量加水印、批量压缩图片:这个神奇的网站你值得拥有!...

各位老铁们大家好&#xff0c;听说今早一辆装有13吨百世快递的货车被烧了&#xff0c;我是只能暗自祈祷自己快递不在其中的宝藏女孩9妹~对于很多需要推送图文的新媒体人来说&#xff0c;图片处理是每天的必经工作任务(就好像9妹一样&#xff0c;能文能武还能出策划)&#xff0c…

防火墙 用户只能访问指定网站_网站优化吸引用户访问的方法有哪些?

如果网站长期没有被访问的话&#xff0c;即使网站设置的再精美都是没有用的&#xff0c;互联网用户使用搜索引擎搜索不同的网站&#xff0c;但一些用户进入网站后返回很快&#xff0c;而一些用户长期频繁地访问网站&#xff0c;甚至收集网站。这是网站优化的魅力所在&#xff0…

如何防止通过url攻击_如何通过更好的URL结构改善网站SEO排名

URL是有效的SEO策略不可或缺的一部分。它们被视为网站在互联网上轻松被访问的关键因素之一。网站的URL应以符合逻辑结构的一致方式进行定义。选定的URL结构应反映网站内容的组织方式&#xff0c;并且包含访问者用来查找站点的关键词。这就是为什么它如此重要以至于要注意网站的…

php 爬虫_公众号简单爬虫--把公众号文章全部转载到网站(三)

公众号简单爬虫--把公众号文章全部转载到网站&#xff08;三&#xff09;根据上一篇的方法,我们得到了一个包含标题,时间,作者,封面,文章连接等信息的json文件.接下来,就是要提取文章详细信息和把一系列的信息写入数据库.这里先说明几点,我们转贴公众号文章,会有个问题,就是图片…

PHP毕业设计旅游网站,基于PHP旅游网站的开发与设计(含录像)

基于PHP旅游网站的开发与设计(含录像)(程序代码,数据库,毕业论文25000字)随着Internet的普及&#xff0c;越来越多的企业建立了自己的WWW网站&#xff0c;电子商务应时而生。本论文主要讨论如何建立一个简单的旅游电子商务网站。本网站主要包括4大系统&#xff1a;注册模块&…

vue倒计时时间实现圆形进度条_Vue实现列表倒计时效果_vue.js教程,AngularJS教程_我爱模板网 - 提供下载各种免费建站资源,免费网站模板,免费网页特效,让你爱上建站!...

我爱模板网要实现下面的效果&#xff1a;即从后台获取数据&#xff0c;然后根据返回的时间&#xff0c;如“2019-12-17 23:59:59”&#xff0c;做成倒计时效果。第一步、布局&#xff1a;{{item.storeName}}{{item.storeDistance}}{{item.storeIndustryName}}{{item.countDownH…

利用Minify加速 优化网站性能教程

Minify 是用PHP5开发的应用&#xff0c;通过遵循一些Yahoo的优化规则来提高网站的性能。它会合并多个CSS或者JavaScript文件&#xff0c;移除一些不必要的空格和注释&#xff0c;进行gzip压缩&#xff0c;并且会设置浏览器的缓存头。Minify 在设计上和Yahoo的 Combo Handler Se…

保护水资源公益网站html,保护水资源公益海报世界水日宣传海报

保护水资源公益海报世界水日宣传海报是一篇世界水日地球日环境保护矢量公益海报模板&#xff0c;内容制作比较精美&#xff0c;水是生命之源公益海报,水是我们的生命之源,保护水资源是我们每个人的共同责任,不浪费任何一滴水&#xff0c;需要这样的一篇节水主题宣传海报素材的朋…

登录网站后自动退出需重新登录_微博自动关注陌生人?你的账号有可能被“劫持”了!...

近期&#xff0c;北京市举报中心接到网民举报称&#xff0c;他的新浪微博账号经常自动关注陌生人&#xff0c;其中大多是营销账号&#xff0c;自己并不感兴趣&#xff0c;真正想看的内容却找不到了&#xff0c;影响使用体验。北京市举报中心依法依规交由网站处置&#xff0c;在…