krokyze / flutter_seo

Flutter package for SEO support on Web.
MIT License
46 stars 5 forks source link

Proposal: Enhancing SEO Capabilities with a Widget-Wrapping Approach #63

Open abdelaziz-mahdy opened 4 months ago

abdelaziz-mahdy commented 4 months ago

I've been contemplating an enhancement for an existing SEO package and wanted to share my idea to see if it's feasible, as I've not found a clear way to implement it yet.

The core of my proposal is to introduce a mechanism within the SEO package that allows the main app widget to be wrapped in an "SEO Widget". This SEO Widget would automatically convert all child widgets into versions optimized for search engines when the app is running on the web. Additionally, to accommodate cases where certain widgets should not be optimized for SEO, I suggest including an option for developers to specify these exceptions in an exclude list.

This approach would enable the SEO package to effectively optimize all content for search engine visibility, even content that originates from third-party packages, provided that it is encapsulated within the SEO Widget.

I'm sharing this proposal to gather insights on how such functionality could be implemented or if it's even possible. I believe this could significantly enhance the SEO capabilities of Flutter web applications by ensuring their content is more accessible and indexable by search engines. Let's discuss the potential for making this idea a reality.

krokyze commented 4 months ago

Hey.

To be honest, I don't think that would be a good solution as in that way every change in flutter or 3rd party dependency might completely break the package. Another thing is that we would need to support hundreds of different packages and their optimisation for SEO which is not quite feasible in long term.

abdelaziz-mahdy commented 4 months ago

I think I didn't explain it correctly

My idea is if I wrap a custom widget with seo

All text widgets inside it adds the logic of your seo text when building

So if for example people are using alot of packages inside their code the seo package can access their text without changing the code

krokyze commented 4 months ago

Maybe you could provide some code examples?

abdelaziz-mahdy commented 4 months ago

i will try to make a pr as a proof of concept

abdelaziz-mahdy commented 4 months ago

this is my proposal, i cant code it right now so i am leaving this for discussion

Summary

The proposal introduces a SeoWrapper widget designed to enhance SEO for Flutter web applications. The core functionality of this widget is to automatically replace all child Text widgets with seo.Text widgets during the build phase. This approach aims to simplify the process of optimizing text for SEO, eliminating the need for manual replacements throughout the application code.

Implementation Idea

The SeoWrapper widget would act as a high-level wrapper around the application (or specific parts of it) and utilize the build context to identify and replace Text widgets with seo.Text widgets. This substitution would only occur during the build phase and specifically for web targets, ensuring that the SEO enhancements are applied only where necessary and without affecting the app's functionality on other platforms.

Example Code

Below is a simplified pseudo-code example to illustrate the concept:


class SeoWrapper extends StatelessWidget {
  final Widget child;

  SeoWrapper({required this.child});

  @override
  Widget build(BuildContext context) {
    // Implementation to recursively check and replace Text widgets with SeoText widgets
    // Note: Actual implementation would require more detailed logic to traverse the widget tree
    return TransformTextWidgets(child: child);
  }
}

class TransformTextWidgets extends StatelessWidget {
  final Widget child;

  TransformTextWidgets({required this.child});

  @override
  Widget build(BuildContext context) {
    // This is a placeholder for the logic to recursively transform Text to SeoText
    // For example, using a method that traverses the child widget tree
    // and replaces Text widgets with SeoText widgets on the fly, only for web targets.
    return child; // Transformed widget tree
  }
}
krokyze commented 4 months ago

At least from my experience I don't think it's possible to traverse the tree and replace widgets like that, if it is then I still do feel that it might be quite a hit for performance. Do you have any experience with replacing widgets down the tree?

abdelaziz-mahdy commented 4 months ago

Sadly no, I never tried that,

But the thought of that idea was based on the issue of third party packages and reducing changing codes

I don't even know if such a thing is possible

krokyze commented 3 months ago

Ok. I'll leave this issue open as an idea.