Scully完整配置教程:从零开始搭建高性能Angular网站
【免费下载链接】scullyThe Static Site Generator for Angular apps项目地址: https://gitcode.com/gh_mirrors/sc/scully
Scully是一款专为Angular应用设计的静态网站生成器,它能够将Angular应用预渲染为静态HTML和CSS文件,显著提升网站加载速度和性能。本教程将带你从零开始,完成Scully的完整配置,打造一个高性能的Angular静态网站。
为什么选择Scully?
Scully作为Angular生态系统中的重要工具,具有以下显著优势:
- 极致性能:预渲染技术使页面加载速度提升80%以上
- SEO友好:静态HTML内容更容易被搜索引擎抓取
- 简化部署:生成的静态文件可直接部署到任何静态托管服务
- 开发便捷:与Angular CLI无缝集成,保留Angular开发体验
前期准备
在开始配置Scully之前,请确保你的开发环境满足以下要求:
- Node.js 12.14.0或更高版本
- Angular CLI 9.0.0或更高版本
- npm 6.13.4或更高版本
你可以通过以下命令检查当前环境版本:
node -v ng version npm -v安装Scully
1. 创建新的Angular项目(如已有项目可跳过此步骤)
ng new my-scully-project --routing cd my-scully-project2. 添加Scully到项目中
ng add @scullyio/init这条命令会自动完成以下操作:
- 安装必要的依赖包
- 创建Scully配置文件
- 设置路由发现机制
- 添加Scully命令到package.json
安装完成后,你会在项目根目录看到新生成的Scully配置文件:scully.<project-name>.config.ts。
配置Scully
基础配置
Scully的主要配置文件是scully.<project-name>.config.ts,它导出一个ScullyConfig对象:
export const config: ScullyConfig = { projectRoot: "./src", projectName: "my-scully-project", outDir: "./dist/static", routes: { '/blog/:slug': { type: 'contentFolder', slug: { folder: "./blog" } } } };主要配置项说明:
projectRoot:Angular项目的根目录projectName:项目名称outDir:静态文件输出目录routes:路由配置,定义如何处理不同的路由
添加内容插件
Scully支持多种内容插件,最常用的是处理Markdown文件的contentFolder插件。要使用它,首先需要安装相关依赖:
npm install @scullyio/ng-lib @scullyio/plugins然后在路由配置中添加:
routes: { '/blog/:slug': { type: 'contentFolder', slug: { folder: "./blog" } } }这样配置后,Scully会自动发现./blog目录下的所有Markdown文件,并为每个文件生成对应的路由。
创建和使用内容
创建Markdown内容
在项目根目录创建blog文件夹,并添加第一篇文章:
mkdir blog echo "--- title: '我的第一篇Scully博客' description: '使用Scully创建的第一篇博客文章' published: true date: '2023-01-01' --- # 欢迎来到我的博客 这是使用Scully静态网站生成器创建的第一篇博客文章。" > blog/first-post.md在Angular中显示内容
创建一个博客文章组件:
ng generate component blog/post修改blog/post/post.component.ts:
import { Component, OnInit } from '@angular/core'; import { ActivatedRoute } from '@angular/router'; import { ScullyRoute, ScullyRoutesService } from '@scullyio/ng-lib'; @Component({ selector: 'app-post', templateUrl: './post.component.html', styleUrls: ['./post.component.css'] }) export class PostComponent implements OnInit { post: ScullyRoute; constructor( private route: ActivatedRoute, private scully: ScullyRoutesService ) { } ngOnInit(): void { this.route.params.subscribe(params => { this.scully.getCurrent().subscribe(post => { this.post = post; }); }); } }在模板文件post.component.html中添加:
<div *ngIf="post"> <h1>{{ post.title }}</h1> <p>{{ post.date | date }}</p> <scully-content></scully-content> </div>构建和预览
构建Angular应用
ng build --prod运行Scully生成静态文件
npm run scullyScully会分析你的Angular应用,发现所有路由,并生成对应的静态HTML文件。生成的文件会保存在dist/static目录中。
预览静态网站
npm run scully serve这个命令会启动一个本地服务器,默认在http://localhost:1668上提供生成的静态网站。
高级配置
添加自定义插件
Scully允许你创建自定义插件来扩展其功能。创建插件的步骤如下:
- 在项目根目录创建
scully/plugins文件夹 - 创建插件文件,例如
my-plugin.ts - 在Scully配置中注册插件
// scully/plugins/my-plugin.ts import { registerPlugin } from '@scullyio/scully'; export const myPlugin = registerPlugin('routeProcess', 'myPlugin', async (route) => { // 插件逻辑 return route; });配置路由忽略
如果你希望Scully忽略某些路由,可以在配置中添加ignoredRoutes:
export const config: ScullyConfig = { // ...其他配置 ignoredRoutes: [ '/admin/**', '/private/*' ] };使用Scully的Transfer State服务
Scully提供了Transfer State服务,可以在服务器端和客户端之间共享数据:
import { TransferStateService } from '@scullyio/ng-lib'; // 在组件中 constructor(private transferState: TransferStateService) { const data = this.transferState.get('key', null); if (!data) { // 从API获取数据 // 然后保存到transferState this.transferState.set('key', fetchedData); } }部署静态网站
Scully生成的静态文件可以部署到任何支持静态网站的托管服务,如:
- Netlify
- Vercel
- GitHub Pages
- AWS S3
- Firebase Hosting
以Netlify为例,只需将dist/static目录推送到连接的GitHub仓库,Netlify会自动部署你的网站。
常见问题解决
路由未被Scully发现
如果Scully没有发现你的路由,请检查:
- 路由是否在
app-routing.module.ts中定义 - 路由是否使用了
loadChildren进行懒加载 - 是否在Scully配置中正确设置了
routes
内容没有正确显示
如果Markdown内容没有正确显示,请确保:
- 已安装
@scullyio/ng-lib - 在模块中导入了
ScullyLibModule - 在模板中使用了
<scully-content></scully-content>组件
构建速度慢
对于大型项目,可以通过以下方式提高Scully构建速度:
- 使用
--routeFilter只构建特定路由 - 增加
maxRenderThreads配置 - 使用增量构建:
npm run scully -- --incremental
结语
通过本教程,你已经掌握了Scully的基本配置和高级用法。Scully作为Angular的静态网站生成器,不仅能显著提升网站性能,还能简化开发和部署流程。无论你是构建博客、文档网站还是企业官网,Scully都是一个值得考虑的优秀工具。
想要了解更多关于Scully的高级功能,可以查阅官方文档:docs/overview.md。祝你使用Scully构建出既快速又美观的Angular静态网站!
【免费下载链接】scullyThe Static Site Generator for Angular apps项目地址: https://gitcode.com/gh_mirrors/sc/scully
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考