Comments (8)
So I was having some issues with this as well but I found a pretty good solution.
First the issue: NavigationEnd is called as soon as ngOnInit() finishes and does not wait for subscriptions to complete (and it shouldn't). Even deferred function won't add data from the subscriptions. As a hack you could add a function to manually update them when the subscription is done, however, this would be bad SEO as the values would load and then be updated (It's possible that the googlebot would only read the first values and move on).
The Best Solution: (No changes to @nglibs/meta needed). You need to load the data before the page is rendered. You can do this using the resolver in your routing module. Read this: https://angular.io/docs/ts/latest/api/router/index/Resolve-interface.html
Then on your page you can get the data by:
constructor(private route: ActivatedRoute) {}
ngOnInit(){
this.product = this.route.snapshot.data['product'];
}
This ensures that the subscription is completed before the component is loaded and all of the metadata can be set immediately after in ngOnInit().
Note: you do not need to use the defer method for this.
@kblestarge Let me know if any of this is unclear and best of luck.
@fulls1z3 Awesome job on this project.
from ngx-meta.
Hi @kblestarge, I'll analyze this and will return to you at the first opportunity.
from ngx-meta.
Hi @kblestarge, just started, first with fixing the dependencies. But I'm planning to resolve all open issues on this & fellow repositories at once, while I have some free time this & tomorrow evenings.
Probably tomorrow, or Saturday I'll publish a release. Stay tuned :)
from ngx-meta.
@Raydaq-git that works great for me! Thank you!
from ngx-meta.
Hi @kblestarge, @Raydaq-git. First of all I'd like to thank to @Raydaq-git for coming up quickly and offering such a working solution as well as both your appreciation.
The use of resolver would definitely work in most cases, no doubts. However, I'd like to offer another alternative too: the callback function - which might be a slightly more elegant way to handle this.
I need to clarify that, it could be the case only if you're using a certain REST call to fetch the data. I mean, there's a generic service (could be a SEO service returning page titles) that you invoke by passing the page id/key/etc as argument.
Well, if you look at the app.module.ts
on @nglibs/example-app, you'll notice that the metaFactory
contains the (key: string) => translate.get(key)
as a callback. It means, the value that the meta property contains gets resolved using that callback function.
And, if you look at the config.json, home.routes.ts, and about.routes.ts you'll notice keys/values like "defaultPageTitle": "DEFAULT_TITLE"
, "defaultMetaDescription": "DEFAULT_DESC"
, as well as:
{
path: '',
component: HomeComponent,
data: {
meta: {
title: 'PUBLIC.HOME.PAGE_TITLE',
override: true,
description: 'PUBLIC.HOME.META_DESC'
}
}
}
In this example app, the callback function is the translate.get
(thanks to @ocombe, for his awesome project ngx-translate). ngx-translate uses these keys are to retrieve translations of meta information on en.json and on tr.json.
In your case, a more or less a similar approach should work:
metaFactory
callback: (key: string) => seoService.getEntityBySeoName(key)
This way, you won't need to call meta.setTitle/meta.setTag within the subscriptions. Simply pass the keys to a generic service which handles retrieval of seo data, and you're done ;)
Meantime, I feel that you're getting the meta info on the entity level - not using a generic method. The callback approach might not work if you stick using separate methods for every object. But there might be a way to make it generic - it's up to the architecture of your project.
Anyway, please feel free to ask anything you need, I'll be glad to provide help as far as in me lies.
from ngx-meta.
Maybe I don't understand how this is supposed to work. Is it even possible to update the meta information outside of the ngOnInit function?
from ngx-meta.
Looks like it works inside of a route param subscription declared in the ngOnInit:
this.route.params.subscribe((params: Params) => { this.meta.setTitle('this works fine'); });
but not inside my REST http subscription:
this.xrdService.entity.getEntityBySeoName(this.seoName) .subscribe((result: XRDEntity) => { this.meta.setTitle('this does not work'); });
Any suggestions?
from ngx-meta.
@fulls1z3, any helpful insights?
from ngx-meta.
Related Issues (20)
- remove lodash peerDependency as it's not needed
- meta tags API call fails HOT 2
- meta doesn't get updated.
- upgrade to Angular 7 HOT 1
- Not fetching meta tags in web master HOT 1
- Make configurable callback exceptions HOT 2
- Default fallback on null instead of error (unless not defined in configuration)
- use with Angular i18n
- Set dynamic meta tags to use api call
- Allow use of ngx-meta in angular 8 project HOT 3
- Do you have any plan to apply for angular8? HOT 2
- Angular v9 support HOT 4
- The title does not display correctly in the browser history
- AOT Compiling Error - Function calls are not supported in decorators but 'MetaModule' was called HOT 7
- @ngx-meta/core Doesn't support Angular 11.X.X HOT 11
- Setting Tags from ts file.
- Linked-In Social Share issue HOT 1
- Uncaught TypeError: Cannot read property 'core' of undefined at ngx-meta-core.umd.js:4 HOT 1
- Angular 11/12 support HOT 6
- after ng update 12 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ngx-meta.