【问题标题】:Cannot Render Contentful content by using Angular Universal (SSR)无法使用 Angular Universal (SSR) 呈现内容内容
【发布时间】:2020-07-19 15:13:37
【问题描述】:

我正在使用 Contentful CMS 开发一个需要 SSR 的 Angular 应用程序。我已按照Angular Universal 中提到的所有步骤进行操作。但它只是渲染静态内容,而不是来自 Contentful 的动态内容。我也尝试过 TransferState API,但它不适用于 Contentful。我的代码是

app.server.module.ts

import { NgModule } from '@angular/core';
import { ServerModule, ServerTransferStateModule } from '@angular/platform-server';

import { AppModule } from './app.module';
import { AppComponent } from './app.component';
import { ModuleMapLoaderModule } from '@nguniversal/module-map-ngfactory-loader';

@NgModule({
  imports: [
    AppModule,
    ServerModule,
    ServerTransferStateModule,
    ModuleMapLoaderModule,
   ],
  bootstrap: [AppComponent],
})
export class AppServerModule {}

server.ts

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

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

// Express server
const app = express();

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

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

const domino = require('domino');
const fs = require('fs');
const path = require('path');
const template = fs.readFileSync('./dist/browser/index.html').toString();
const win = domino.createWindow(template);
global['window'] = win;
global['document'] = win.document;

// 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}`);
});

contentful.service.ts

private client = createClient({
    space: ENV.contentful.spaceId,
    accessToken: ENV.contentful.token
  });

我是做错了什么还是需要为 Contentful 做些不同的事情?

提前致谢。

【问题讨论】:

    标签: angular server-side-rendering angular-universal contentful


    【解决方案1】:

    Contentful SDK 不使用 Angular 的 HttpClient 模块来获取数据,因此默认情况下 TransferState API 不会工作。

    您首先需要使用 axios 的适配器,如下所述:https://github.com/contentful/contentful.js/blob/master/ADVANCED.md#angular-universal

    然后在你的app.module.ts 中使用TransferHttpCacheModulehttps://github.com/angular/universal/blob/master/docs/transfer-http.md

    【讨论】:

    • 我已经尝试过这个解决方案,但现在从 Contentful 得到空响应。它将所有请求发送到当前域,例如localhost:4000/entries?sys.id=ID_HERE
    • 这是ngx-axios-adapter 库的一个已知问题。这是一个解决方法:github.com/patrickhousley/ngx-axios-adapter/issues/…
    • 不知道你是否还在做这个项目,但是有人用axios适配器给了我答案:stackoverflow.com/questions/61910699/…
    • 仅供参考,ngx-axios-adapter 并没有真正维护。我尝试将它与 ng11 一起使用,但这是不行的。适配器执行的逻辑是在 axios 调用发送到服务器之前拦截它,获取所有请求详细信息并使用 ng httpClient 创建类似的请求。这就是我最终要做的。
    【解决方案2】:

    我已经通过使用 Contentful API 而不是它的 SDK 解决了这个问题。 将其 API 与 HttpClient 一起使用。

    API 参考: https://www.contentful.com/developers/docs/references/content-delivery-api/

    注意:在 HttpClient 中使用完整的 API url,无需任何代理。

    【讨论】:

    • 你如何处理包含?在 SDK 中它们似乎是合并的,直接调用 API 时它们是一个单独的对象
    • 我必须按照以下文档编写自定义方法来解析链接:contentful.com/developers/docs/concepts/links
    猜你喜欢
    • 2019-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-07
    • 1970-01-01
    • 2019-02-15
    • 2015-06-23
    • 1970-01-01
    相关资源
    最近更新 更多