seo 包是一个 Flutter 插件,旨在通过动态管理元标签和 body 属性来增强 Web 应用程序的 SEO 功能。它允许开发者直接从 Dart 代码设置标题、描述、Open Graph 标签和其他与 SEO 相关的内容。这确保了搜索引擎能够正确索引网页,从而提高可见性和排名。该包与 Flutter 的 Web 平台无缝集成,并支持服务器端渲染的考虑因素。对于需要丰富元数据控制而无需手动操作 HTML 的应用来说,它特别有用。
适合场景
动态元标签更新
改进的搜索引擎索引
社交媒体分享优化
单页应用SEO
基于路线的内容个性化
服务器端渲染兼容性
主要特点
轻松管理元标签
支持 Open Graph 和 Twitter 卡片
动态标题和描述更改
跨平台 Web 支持
最小化样板代码
与 Flutter 路由的集成
适合对象
仅限 Web 的 Flutter 应用
面向搜索引擎优化的Web应用
动态内容的应用程序
注重搜索可见性的开发者
需要社交媒体元数据的项目
使用客户端路由的应用程序
使用建议
不适用于移动平台(Android/iOS)
在多路由场景中需要谨慎处理
仅限网页平台功能
不替代完整的 SSR 框架
高级SEO需要手动配置
最好与正确的页面结构配合使用
原始 README
以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。
flutter_seo
Flutter package for enabling SEO (meta, body tag) support on Web. The package listens to widget tree changes and converts Seo.text(...), Seo.image(...), Seo.link(...), Seo.head(...) widgets into html document tree. View Demo
Use usePathUrlStrategy() to ensure that Google recognizes each URL as a distinct page. Failure to do so may result in Google perceiving all URLs as the same page. For additional details, refer to this video.
Wrap your app within SeoController which will handle listening to widget tree changes and updating the html document tree. In case your app has authorization and user is logged in you can disable the controller by enabled: false as it's redundant to update the html document tree at that state.
WidgetTree (recommended) - based on traversing widget tree, while it's bit slower than SemanticsTree it's production ready and doesn't have any blocking Flutter SDK issues.
SemanticsTree (experimental) - based on traversing semantic data node tree. Does traverse the tree faster but doesn't support Seo.head(...), Seo.text(style: ...), Seo.link(rel: ...), Seo.html(html: ...)
Sample Usage
You should wrap all your SEO required widgets accordingly within Seo.text(...), Seo.image(...), Seo.link(...) and SEO required pages within Seo.head(...). From personal experience it's more comfortable to create custom AppText, AppImage, AppLink, AppHead base widgets and use those in the project.
Seo.html(
html: '<div>Some raw html</div>',
child: ...,
); // converts to: <div>Some raw html</div>
Head
Seo.head(
tags: [
MetaTag(name: 'title', content: 'Flutter SEO Example'),
LinkTag(rel: 'canonical', href: 'http://www.example.com'),
],
child: ...,
); // converts to: <meta name="title" content="Flutter SEO Example"><link rel="canonical" href="http://www.example.com" />
Warning: Open Graph (og:title, og:description, etc.) and Twitter Card (twitter:title, twitter:description, etc.) will not work. Read more.
Tips
Supporting Open Graph, Twitter Card tags
Facebook, Twitter, etc. simply load index.html and don't execute any JavaScript that webpage contains so we're not able to change meta tags within Dart code. The proposed solution is to create simple Server-Side Rendering which would add Open Graph, Twitter Card tags within index.html before returning it to Client.
Demo
View demo here: https://flutter-seo.netlify.app
PageSpeed Insights
Google Search
Landing page has been indexed and does appear in Search
Remaining pages have Discovered - currently not indexed status. I am investigating why.