使用 CodeSandbox、Contentful 和 Netlify 三者完全在云端开发、编辑和部署网站
设置开发环境可能是一个繁琐的过程。我曾经在一家公司工作,当时我们开发的定制商店系统,在两天内就搭建好了开发环境,这可是个了不起的成就(不过说实话,当时 Vagrant 和 Docker 还没有出现)。为什么呢?
软件产品的构建很大程度上依赖于你最喜欢的编辑器或 IDE 等工具来提高效率,而软件的运行和更新则依赖于已安装的依赖项,例如数据库、shell 程序或服务器。这是否仍然是必需的?或者,我们现在可以抛弃所有这些,完全依赖云服务吗?
我最近做了一个关于2018 年前端技术栈的演讲,探讨了如何在电脑上不放任何文件的情况下实现如此大的突破。事实证明,现在你真的可以创建网站,使其可编辑,然后部署它们(我非常喜欢最近的静态网站生成器),只需在任何一台电脑上使用强大的在线服务即可。
CodeSandbox——城里新的在线编辑器
不久前,我注意到 CodeSandbox 在 React 原型设计中的应用越来越广泛,当时人们开始在 Twitter 上分享一些包含特定 React 模式或最佳实践的沙盒。我立刻想到:“我们需要另一个在线编辑器吗?”
今年早些时候,我写了一篇关于如何在 React 中使用渲染属性的文章,并决定尝试一下CodeSandbox。它感觉不像我本地的编辑器(我用的是 VSCode),但非常接近。
在 CodeSandbox 中,你可以从 50 万个用户沙盒中 fork 一个开始,也可以选择使用 React、Vue、Angular 和其他框架的入门模板从头开始。纵观所有用户创建的沙盒,你会发现该编辑器目前主要用于 React 生态系统中的快速原型设计。然而,这并不意味着你不能用它来构建 React 生态系统内外更复杂的项目。
完全在云中开始 JavaScript 开发
在过去,使用现代框架启动一个基于 JavaScript 的新项目非常繁琐,而且对初学者来说远非友好。React 团队发现这种情况必须改变,于是提出了create-react-app。这个项目可以帮助你在几分钟内引导并启动一个新的 React 项目,它移除了所有配置,并提供了所有必要的默认设置(#zeroconfig all the things 🎉)。
create-react-app是 CodeSandbox 创建新 React 项目的基础。对于 Preact、Vue 和其他框架,我们也提供类似的 CLI 工具,甚至还有一个“原始”入门模板,它没有过多的框架依赖,而是使用Parcel(一个新的零配置打包器——相信我,它太棒了!)来提供您所需的所有自由。
当您决定采用 React 路线并初始化新项目时,您将获得一个可以投入 React 开发的代码库。
重要的编辑器功能让您忘记您“只是”在一个在线编辑器中
cmd/ctrl+p 快速访问文件和命令
在进行 Web 开发时,有几样东西我离不开——首先是CMD+p
和CMD+Shift+p
。这两个快捷键可以让你跳转到任何文件,或者通过快速易用的模糊搜索执行任何可用的命令。编程通常与生产力有关,这两个快捷键可以帮助你无需离开键盘就能完成任何操作。
依赖关系处理和自动安装
但现在你处于云环境中,对吧?那么如何安装依赖项呢?CodeSandbox 提供了一个对话框,让你可以轻松地从 npm 中选择依赖项。当你使用此对话框安装软件包时,依赖项package.json
将自动更新。太棒了!
默认包含 Prettier
在 CodeSandbox 中开发时,Prettier默认启用,可配置,并且运行非常流畅!
在单独的窗口中进行热重载
看看上面的截图,编辑器提供了浏览器内预览功能。更酷的是,你可以在单独的窗口中打开预览,这对于像我这样的双显示器配置来说非常完美。这样,代码在一个显示器上显示,而我可以在另一个显示器上近乎实时地看到更改。
使用 TypeScript 类型定义的项目的自动完成功能
当我发现 VSCode 可以自动识别 npm 包中的类型定义时,我最终决定使用 TypeScript。作为一名 JavaScript 开发者,我非常习惯于没有强大自动补全功能的工作,但看到我的编辑器自动识别 TypeScript 定义真是太棒了。看到 CodeSandbox 也能做到同样的事情,真是太好了!
GitHub 集成使 CodeSandbox 成为一个真正的工具
最后一个吸引我的功能是 GitHub 集成,它允许你在 CodeSandbox 中创建项目,将其推送到 GitHub,然后直接从 CodeSandbox 提交到 GitHub。非常酷!
唯一缺少的功能
唯一缺少的就是分屏模式,可以同时查看多个文件,这让它无法成为我日常的开发应用。希望它能尽快上线!🤞🏻
Contentful——适用于任何项目的内容基础设施
使用 CodeSandbox,您可以快速创建下一个 JavaScript 项目并将其推送到 GitHub。然而,在进行网站开发时,很多项目都是为那些不太擅长编写代码的人构建的。
以一个朋友的快速单页作品集网站为例。如何实现这个项目,既省去了通过拉取请求更新内容的麻烦,又无需自己搭建完整的内容管理系统?您可以使用 Contentful 的内容基础设施来实现。
使用 Contentful,您可以在几分钟内定义所需的数据模型,并使用 JSON API 获取数据。对于上面的例子,您需要一个实体,其中包含分别用于图像、标题和段落的单独字段。这种灵活性正是 Contentful 的亮点——创建内容类型portfolio
并定义三个所需的字段,而无需设置服务器或类似操作。
您的非技术朋友现在可以对您正在构建的 JavaScript 应用程序进行内容更改,而无需编辑 JSON 文件或 React 代码。
之后,您可以使用提供的JavaScript SDK获取朋友编辑的Contentful数据。
import React from "react";
import ReactDOM from "react-dom";
import { createClient } from "contentful";
import "./styles.css";
// create the SDK client with the needed credentials
// which you can get in the web app
const client = createClient({
space: "...",
accessToken: "..."
});
function Portfolio() { /* … */ }
class App extends React.Component {
constructor() {
super();
this.state = { isLoading: true, portfolio: null };
// fetch the entry of your portfolio entry type
client
.getEntries({
content_type: "portfolio"
})
.then(({ items }) => {
this.setState({ isLoading: false, portfolio: items[0] });
});
}
render() {
return this.state.isLoading ? (
<div>Loading</div>
) : (
<div className="App">
<Portfolio {...this.state.portfolio.fields} />
</div>
);
}
}
~~~{% endraw %}
[When you look at the code above](https://codesandbox.io/s/5wxvp413zl), one of my favorite things is that you can connect content entries with your frontend component quite easily ({% raw %}`<Portfolio {...this.state.portfolio.fields} />`). This connection makes Contentful [a perfect fit for component-driven applications and sites](https://www.contentful.com/blog/2017/10/11/love-letter-to-component-ready-cms/?utm_campaign=deploy-cloud-codesandbox&utm_medium=referral&utm_source=devto&utm_content=deploy-cloud-codesandbox&utm_term=).
### Netlify – the few-clicks static deployment tool
Now, you have the code editable and connected to Github in CodeSandbox, and you can edit the content that powers the application via Contentful. The last step is to deploy this application somewhere.
[Netlify](https://www.netlify.com/) is a relatively new service out there that specializes in static deployments. The cool thing about Netlify is that they also allow you to define build scripts - they’re your CI service and host, so to say.
To deploy a site to Netlify you can start by importing a specific GitHub repository:

Your next steps are to define a build command and public directory in which the static files will be present. In the case of *create-react-app*, the build command is {% raw %}`npm run build`{% endraw %} and the published directory will be {% raw %}`build`{% endraw %}. After submitting these two configurations, your first deploy will get up and running, and you’ll get a unique subdomain on netlify.com.
One less obvious thing after importing a GitHub repository in Netlify is that this process also defines webhooks on GitHub. Now every time you push new code to GitHub, Netlify will automatically redeploy your site. Magic? Magic!

### Usage of webhooks to connect all the pieces
For this portfolio demo site, there is no additional webhook configuration needed because the React application fetches that data right from the browser. Client-side only applications have the significant downside that they show a loading spinner initially until they fetched all needed API data though. A more performant way to do it is to statically pre-render React on the server and then do something that is called [hydration](https://reactjs.org/docs/react-dom.html#hydrate) when the client-side React code kicks in.
The static pre-render approach means that you would have to rerender the generated HTML files and deploy them whenever your friend updates her portfolio data in Contentful, too. An additional webhook notifying Netlify has to be configured in Contentful in that case.

With a little bit more of webhook configuration, you can then set up a complete deployment pipeline without any local installation and configuration shipping static sites without loading spinners.
### Use the power of web services to not reinvent the wheel
In conclusion, I’m amazed how much the web development field has changed. [Serverless technologies change the way we work](https://www.contentful.com/blog/2018/04/05/graphql-and-serverless-where-cloud-computing-is-heading/?utm_campaign=deploy-cloud-codesandbox&utm_medium=referral&utm_source=devto&utm_content=deploy-cloud-codesandbox&utm_term=) and we no longer have to worry about all the tiny pieces that might be needed in a project. For running Node.js projects we can use [Zeit](https://zeit.co/), for authentication [Auth0](https://auth0.com/), and for a performant search experience [Algolia](https://www.algolia.com/).

Webhooks and serverless functions give me the possibility to connect all these services and write quick connectors in the cloud in an easy and speedy manner. That’s very exciting because I can now focus on building things rather than setting things up and... **I can even create and deploy websites on my friend's computer while we’re sitting on his balcony.**