TryGhost/Ghost

Dynamic Routing: custom meta title & body_class output for static routes using data key

kirrg001 opened this issue · 17 comments

Reported in the forum: https://forum.ghost.org/t/when-using-dynamic-routes-meta-title-and-body-class-are-empty/3765

Custom Meta Title

routes:
   /page1/:
    data: page.SLUG
    template: page

If you use

<head>
    <title>{{meta_title}}</title>
</head>

{{#page}}
{{/page}}

It always outputs the blog meta title and not the custom page meta title.

I see two problems:

  1. the StaticRoutesRouter does not forward the correct context, see. It has to forward the resource type as well (e.g. page) if the data key was provided.

  2. The meta title helper is not aware of the this.page context object -> data.page.

Body Class Helper

{{body_class}}

This helper also needs to respect this.page.

Code suggestion:

page = this.post && this.post.page ? {slug: this.post.slug} : this.page || false;
classes.push('page-' + page.slug);

Workaround

{{#page}}
  {{meta_title}}
{{/page}}

Ghost: master

In the dynamic routes also the following fields are empty

— Meta Data
— Twitter Card
— Facebook Card

For example, I have these data added

dos

But he answers me with this data.

uno

@godofredoninja Thanks. It's for sure the same underlying problem 👍 The helpers should respect the page context object.

Is there a chance this will be fixed in the next release? :)

naz commented

@velobarplus if there's somebody from the community to pick it up and contribute a PR we'd definitely help and merge it asap ;)

cotko commented

@kirrg001 I'm wiling to do this, but what's up with the missing resourceType in the StaticRoutesRouter, where shuold that be used?
Also you say that key should be used for that: page if the data key was provided - what about when there is more than one data key:

routes:
    data:
        page:
           resource: posts
           type: read
           slug: page1
        morepages:
           resource: posts
           type: read
           slug: page-more
        tags:
           resource: tags
           type: browse
           limit: all   

what should the implementation of StaticRoutesRouter.getResourceType() return in this case?

@kirrg001 I'm wiling to do this, but what's up with the missing resourceType in the StaticRoutesRouter, where shuold that be used?

Nice 👍

We have to push the resource type into the context object.

e.g. resource type is "page" => context = [this.routerName, resourceType]

Also you say that key should be used for that: page if the data key was provided - what about when there is more than one data key:

If there is more than one data definition, the target url does not represent a single resource. I'd not do anything in this case and don't push the resource type.

cotko commented

@kirrg001 ok so if a post is turned into a page, then data.page is present here in the schema meta helper (now that page is pushed into a context).

This in turns calls getPostSchema method, but the page only contains partial data about the post; missing are

  • tags
  • authors
  • primary_author = null // probably because of missing authors?
  • primary_tag = null // probably because of missing tags?

What to do in this case, should post turned into a page also fetch authors and tags so that full data is present, or should meta data in case of page skip this part ?

The static controller should probably auto include tags and authors if the resource is post | page.

But i'd suggest to create separate and small pull requests per problem 👍
Thanks for your help!

All issues tagged with routing have been tagged with later and closed until we prioritise working on the Routing feature again.

Just for information, workaround is described in detail in https://stackoverflow.com/a/54788950/466677

I've noticed this same issue and in my opinion, this basically makes custom routes useless — at least for what I am trying to do (replacing the home page). Technically it works but it's a pain without the {{body_class}} and other parameters being set properly.

I've tried numerous ideas as a workaround but came up empty-handed each time 🙈.

I'm 🤞 for someone to take point on this and get it in on one of the next releases.

Re-opening this to handle prioritisation

Some fixes landed in 2.17.0.

Further problems I see:

  • the whole meta layer & helper layer needs to be reviewed & updated to understand page and post
  • data: page.{slug} does not provide authors,tags by default, which "breaks" the tags helper (empty output)
  • multiple api versions & routing layer/dynamic routing need to be fully reviewed to work 100% generic. There are still some hard-coded checks inside.

The areas need some love and time.


Cannot read property 'slug' of undefined

----------------------------------------

TypeError: Cannot read property 'slug' of undefined
    at getPostSchema (/core/server/data/meta/schema.js:72:31)
    at getSchema (/core/server/data/meta/schema.js:179:20)
    at core/server/data/meta/index.js:101:27```
/resources/:
    permalink: /{slug}/
    template: resources
    filter: tag:-news
    order: featured desc, published_at desc
    data:
      post: page.SLUG

The error is not catched/logged properly. Needs investigation why the error is happening.

stale commented

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.