8

我在前端使用 Angular 6 开发了一个网站。默认情况下,Angular 对 SEO 不友好,所以为了做到这一点,我以 Angular-Universal 或 Angular SSR(服务器端渲染)的方式实现了它。我更新了代码并比较了之前和现在的页面源,我可以在标签中看到我的应用程序,<app-root>并且</app-root>在只有“加载...”之前会出现。

我正在使用MetaServiceand TitleServicefrom@angular/platform-browser来分别更新<meta>Facebook 和 Twitter 所需的标签和<title>标签。

问题是当我在本地系统中运行节点服务器时,视图源向我显示了渲染的meta标签,但是当我在 AWS VM 上的节点服务器中运行相同的代码时,我没有得到渲染的meta标签,而是其他应用程序代码可用。

更新: 添加meta标签的功能

updateMetaTags(egElement: Elements[]) {
    this.url = 'https://example.com/eg/' + this.id;
    const title = egElement[1].innerHTML;
    this.tweetText = 'Check the latest blog on \"' + title + '\"';
    this.meta.addTags([
      { property: 'og:url', content: this.url },
      { property: 'og:type', content: 'website' },
      { property: 'og:title', content: title },
      { property: 'og:description', content: 'Author: ' + egElement[2].innerHTML },
      { property: 'og:image', content: this.egElement[3].img }
    ]);
  }

我在 ngOnInit() 中调用了这个函数。它在我的本地机器上正确渲染,但在服务器上却没有。

egElementid从服务调用返回到后端,并且meta服务已被导入并注入到构造函数中。

4

2 回答 2

4

如果您使用自定义 XHR 调用,例如不使用 Angular HttpClient,SSR 将不会等待 API 调用响应(如果使用 3rd 方库来检索 API 数据,也会发生这种情况)。查看您的站点,除了页面布局/页眉/页脚之外,没有发生服务器端渲染

我猜这与 SSR 中未检索到的 API 数据有关。也许你可以用一些关于这个的信息来更新你的问题?

有一个经过良好测试和维护ngx-meta的库,称为通用 (SSR) 兼容库。你可以看看他们的实现和演示,或者试试他们的库https://github.com/fulls1z3/ngx-meta

于 2018-11-13T10:01:58.990 回答
2

嗨,我也遇到了这个错误,所以请确保在您的server.ts文件中已导入import 'reflect-metadata';以反映所有元数据index.html

你可以看看我的server.ts配置文件\

import 'zone.js/dist/zone-node';
import 'reflect-metadata';

import { enableProdMode } from '@angular/core';
// Express Engine
import { ngExpressEngine } from '@nguniversal/express-engine';
// Import module map for lazy loading
import { provideModuleMap } from '@nguniversal/module-map-ngfactory-loader';

import * as express from 'express';
import { join } from 'path';
import { readFileSync } from 'fs';

// Faster server renders w/ Prod mode (dev mode never needed)
enableProdMode();

// Express server
const app = express();

const PORT = process.env.PORT || 4000;
const DIST_FOLDER = join(process.cwd(), 'dist/browser');

const template = readFileSync(join(DIST_FOLDER, 'index.html')).toString();

const domino = require('domino');
const win = domino.createWindow(template);
global['localStorage'] = win.localStorage;
global['window'] = win;
global['document'] = win.document;
global['Document'] = win.document;
global['DOMTokenList'] = win.DOMTokenList;
global['Node'] = win.Node;
global['Text'] = win.Text;
global['HTMLElement'] = win.HTMLElement;
global['navigator'] = win.navigator;
global['MutationObserver'] = getMockMutationObserver();

function getMockMutationObserver() {
  return class {
    observe(node, options) {}

    disconnect() {}

    takeRecords() {
      return [];
    }
  };
}

// * NOTE :: leave this as require() since this file is built Dynamically from webpack
const { AppServerModuleNgFactory, LAZY_MODULE_MAP } = require('./dist/server/main');

// Our Universal express-engine (found @ https://github.com/angular/universal/tree/master/modules/express-engine)
app.engine(
  'html',
  ngExpressEngine({
    bootstrap: AppServerModuleNgFactory,
    providers: [provideModuleMap(LAZY_MODULE_MAP)],
  }),
);

app.set('view engine', 'html');
app.set('views', DIST_FOLDER);

// Example Express Rest API endpoints
// app.get('/api/**', (req, res) => { });
// Serve static files from /browser
app.get(
  '*.*',
  express.static(DIST_FOLDER, {
    maxAge: '1y',
  }),
);

// All regular routes use the Universal engine
app.get('*', (req, res) => {
  res.render('index', { req });
});

// Start up the Node server
app.listen(PORT, () => {
  console.log(`Node Express server listening on http://localhost:${PORT}`);
});
于 2019-07-30T09:54:03.157 回答