简体   繁体   English

如何在 LInkedin / FB 上共享单页应用程序子页面上动态生成的标题的标题和元数据?

[英]How do i share title and metadata on LInkedin / FB of dynamically generated titles on sub-pages of Single page application?

I have set up a Single page application in Angular and currently dynamically set the page title and OG/ meta parameters based on the routing.我在 Angular 中设置了一个单页应用程序,目前根据路由动态设置页面标题和 OG/ 元参数。 ie for instance everytime the route is changed within the app using this.即,例如每次在应用程序中使用此更改路线时。 the titleService also updates the page title and meta tags of the page: titleService 还会更新页面的页面标题和元标记:

import 'rxjs/add/operator/filter';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/mergeMap';

import { Component, OnInit } from '@angular/core';
import { Router, NavigationEnd, ActivatedRoute } from '@angular/router';
import { Title } from '@angular/platform-browser';

@Component({...})
export class AppComponent implements OnInit {
  constructor(
    private router: Router,
    private activatedRoute: ActivatedRoute,
    private titleService: Title
  ) {}
  ngOnInit() {
    this.router.events
      .filter((event) => event instanceof NavigationEnd)
      .map(() => this.activatedRoute)
      .map((route) => {
        while (route.firstChild) route = route.firstChild;
        return route;
      })
      .filter((route) => route.outlet === 'primary')
      .mergeMap((route) => route.data)
      .subscribe((event) => this.titleService.setTitle(event['title']));
  }
}

The problem now is that when I share one of the sub-pages of my app on Linkedin, It does not update the title and OG items of the URL based on my dynamically generated parameters within my app and instead shows the default title set on my app pre-routing.现在的问题是,当我在 Linkedin 上共享我的应用程序的一个子页面时,它不会根据我在我的应用程序中动态生成的参数更新 URL 的标题和 OG 项目,而是显示在我的应用预路由。

How can I share to Linkedin / Facebook so that it shows the title and description based on the dynamically updated titles on my page?如何分享到 Linkedin / Facebook 以便它根据我页面上动态更新的标题显示标题和描述?

Updating the meta data with a single page web app only updates it on the client side.使用单页 web 应用程序更新元数据仅在客户端更新它。 LinkedIn, Facebook, and other social sharing sites do not load and interpret JavaScript. LinkedIn、Facebook 和其他社交共享网站不会加载和解释 JavaScript。 They simply grab the HTML file returned from the server and use whatever meta data is included there.他们只需抓取从服务器返回的 HTML 文件并使用其中包含的任何元数据。 As such, the custom routing and related data that has been implemented in JavaScript will never be read, and instead the default will always be shown.因此,在 JavaScript 中实现的自定义路由和相关数据将永远不会被读取,而是始终显示默认值。

There are a number of possible solutions though, but all require a decent amount of work and have their pros and cons depending on the scope of your project.虽然有许多可能的解决方案,但所有解决方案都需要大量的工作,并且根据项目的 scope 各有利弊。 You could look into pre building the HTML on the server for initial page load or adjusting the HTML file's meta data on the server before sending it over.您可以查看在服务器上预先构建 HTML 以进行初始页面加载,或在发送之前在服务器上调整 HTML 文件的元数据。

A simple pre-render approach:一个简单的预渲染方法:

One approach to pre-rendering just the meta data would be to have a script that runs after you build your code that creates individual html pages with the adjusted meta data.预渲染元数据的一种方法是在您构建代码后运行一个脚本,该代码使用调整后的元数据创建单独的 html 页面。 (Disclaimer: I primarily use React and not much Angular, so the examples may be slightly off but the general idea should be the same across frameworks) (免责声明:我主要使用 React 而不是很多 Angular,因此示例可能略有偏差,但总体思路在框架之间应该是相同的)

Check out this file as an example -以这个文件为例 -
https://github.com/cid-harvard/growth-lab-app-front-end/blob/master/prerender-metadata.js https://github.com/cid-harvard/growth-lab-app-front-end/blob/master/prerender-metadata.js

Here I read in the built index.html file, modify the metadata for each route by using regex to find and replace a key set of characters (that are placed in the HTML template file) and then saved back out as a separate HTML file that is to be served with the proper meta data (but will otherwise run the SPA identically when the page is fully loaded).在这里,我读入了构建的index.html文件,通过使用正则表达式来查找和替换一组关键字符(放置在 HTML 模板文件中)修改每条路线的元数据,然后将其保存为单独的 Z4C4AD5FCA2E07A3F74DBBED 文件 C将使用正确的元数据提供服务(但在页面完全加载时会以相同方式运行 SPA)。

In order to be easily searched and replaced, the HTML template includes the "default" meta data like this:为了便于搜索和替换,HTML 模板包含如下“默认”元数据:

<title>$OG_TITLE</title>
<meta name="description" content="$OG_DESCRIPTION" />

I run the above script after the build script with a simple node prerender-metadata.js我在构建脚本之后使用简单的node prerender-metadata.js运行上述脚本

A simple server side render approach:一个简单的服务器端渲染方法:

Using the same style of building out the HTML template with searchable strings, you can also perform a similar search and replace on the server for more dynamic content.使用相同的样式构建带有可搜索字符串的 HTML 模板,您还可以在服务器上执行类似的搜索和替换以获取更多动态内容。 Instead of running a script right after the build step, you would run it on the server every time there is a request for the specific url.每次有对特定 url 的请求时,您都将在服务器上运行它,而不是在构建步骤之后立即运行脚本。 Here is an example that uses express and Node on the server:这是一个在服务器上使用 express 和 Node 的示例:

app.get('/terms-of-use', (req, res) => {
  const filePath = path.resolve(__dirname, '../../client', 'build', 'index.html');
  // read in the index.html file
  fs.readFile(filePath, 'utf8', function(err, data) {
    if (err) {
      return console.error(err);
    }
    // replace the special strings with server generated strings
    data = data.replace(/\$OG_TITLE/g, 'Terms of Use');
    const result  = data.replace(/\$OG_DESCRIPTION/g, "Terms of Use.");
    res.send(result);
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使子页面的离子标签可见? - How to make ion-tabs visible for the sub-pages? ionic 4 tabbar - 如何始终显示标签根而不是标签子页面 - ionic 4 tabbar - how to always show tab root instead tab sub-pages 如何在常规 PHP/HTML 页面和 angular 页面之间共享 PHP session? - How do I share PHP session between regular PHP/HTML pages and an angular page? 如何在Angular 8的浏览器选项卡中动态设置页面标题? - How to dynamically set page titles in the browser tab in Angular 8? 角度4:如何删除动态生成的行? - Angular 4 : How do I delete the row that is dynamically generated? 如何通过单个角度应用程序在每个子域级别动态添加脚本标签? - How to dynamically add scripts tag at each Sub-domain level through single angular application? 如何使用Routing在Angular 2应用程序周围共享数据? - How do I use Routing to share data around an Angular 2 application? 如何使用Github页面发布有角度的应用程序? - How do I publish an angular application using Github pages? 如何在Git Pages中部署AngularDart网页? - How do I deploy an AngularDart web page in Git Pages? 导航到单页 Angular 应用程序中的其他页面 - timeonsite 跟踪器 JS 未正确捕获页面标题 - Navigation to other pages in single-page Angular app - the page title is not properly captured by timeonsite tracker JS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM