8. Namespaces
Now, let's talk about namespaces and how we, as a community, can use them to extend Frontity and create a better tool for everyone.
In Frontity state
, actions
and libraries
belong to a shared space among all packages, so each package needs to use its own namespace.
To avoid conflicts between packages we could simply use the name of the package, but we use namespaces instead because some packages are interchangeable. For example, it doesn't matter if you install wp-comments
(native WordPress comments) or disqus-comments
(Disqus comments) in your Frontity project because the theme is going to access it using the common comments
namespace and everything is going to work. In the future, another person could create a third comments
package, based on a new service, and as long as it respects the same structure (written in TypeScript), all the themes (even the old ones!) will work perfectly.
More examples of namespaces are:
source
: for examplewp-source
,wpgrahql-source
or evendrupal-source
โฆanalytics
: for examplegoogle-analytics
,gtm-analytics
,mixpanel-analytics
โฆnotifications
: for exampleonesignal-notifications
,pushwoosh-notifications
โฆshare
: for examplemodal-share
,native-share
โฆrouter
: for exampletiny-router
,3d-router
โฆ
But let's start from the beginning.
Using namespaces in package exports
As we've already seen, this could be a typical theme
package:
One thing you might notice is that roots
, state
, and actions
have a namespace called theme
. It may seem like it is not adding much value because it is the only namespace. Why not write it like this instead?
There are several reasons:
1. It's easier to be aware of the final structure
When you access state or actions, it's much easier to see what you need when you write it like this:
2. It's easier for TypeScript
Even though TypeScript is optional in Frontity, we make sure it has excellent support in case you want to use it. TypeScript gets really complex when you try to modify the structure of your objects, and in order to make it as simple as possible, it's good to create objects with the same structure that they will be consumed later. So yes, TypeScript just works :)
3. Multiple namespaces per package
Packages can export multiple namespaces and that's good. It makes Frontity more flexible.
For example, imagine we want to create a theme that implements its own share:
Making Frontity extensible through namespaces
This is the main reason namespaces exist in Frontity and a big part of how Frontity itself works.
We use namespaces to create abstractions on top of packages and, by doing so, they can communicate between each other without really knowing the specific implementation.
It's easier to understand with some examples.
Example: comments
comments
Take for example the @frontity/wp-comments
package which has an actions.comments.submit
method. As you can see this method is defined under the comments
namespace.
In the case of the @frontity/wp-comments
package the actions.comments.submit
is responsible for sending the content of the fields in the comment form to WordPress.
Now, all the theme
packages that want to submit a comments form can check if there is a package with the comments
namespace with an actions.comments.submit
method available. If there is, it can be used from any React component in the project to submit comment form data.
Users can use their frontity.settings.js
to install and configure wp-comments
:
But what if (and now this is where things become interesting) users don't want to use WordPress native comments but Disqus comments?
Then they just have to install a possible disqus-comments
package instead:
This possible disqus-comments
package might define a different implementation of actions.comments.submit
that instead of submitting the comment form data to WordPress it would instead submit it to be handled by Disqus.
Actually, the theme
has no idea about what specific implementation of comments
you have installed. Everything works and the theme didn't need to change.
Example: analytics
analytics
Let's take a look at another example: two actions in the analytics
namespace. All the packages that want to implement analytics need to have these two actions:
actions.analytics.sendPageview
: send a pageview to the analytics service.actions.analytics.sendEvent
: send an event to the analytics service.
The first one, actions.analytics.sendPageview
, is used by packages that implement router
, each time actions.router.set
is used.
The second one, actions.analytics.sendEvent
, is used by the theme when something interesting happens. For example:
When users open the share modal, a new event is sent to the analytics service of the analytics
package that is installed in the Frontity project, no matter which one it is ๐๐
If you still have any questions about Namespaces in Frontity, please check out the community forum, which is packed full of answers and solutions to all sorts of Frontity questions. If you don't find what you're looking for, feel free to start a new post.
Last updated