0

我正在提供来自 express 的 Angular 页面。我无法通过 prerender 为产品提供渲染页面:

http://www.minilolo.com/?_escaped_fragment_=/products/Lolo-Pink

但是像这样的其他页面都可以:

http://www.minilolo.com/?_escaped_fragment_=/products

我想我可能需要添加一些快速路线,但想知道我是否走在正确的轨道上。谢谢!

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var app = express();

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.set('view engine', 'ejs');

app.use(require('prerender-node').set('prerenderToken', 'xyz123'));

/**
 * Development Settings
 */
if (app.get('env') === 'development') {
    // This will change in production since we'll be using the dist folder
    app.use(express.static(path.join(__dirname, '../client')));
    // This covers serving up the index page
    app.use(express.static(path.join(__dirname, '../client/.tmp')));
    app.use(express.static(path.join(__dirname, '../client/app')));


    // Error Handling
    app.use(function(err, req, res, next) {
        res.status(err.status || 500);
        res.render('error', {
            message: err.message,
            error: err
        });
    });
}

/**
 * Production Settings
 */
if (app.get('env') === 'production') {

    // changes it to use the optimized version for production
    app.use(express.static(path.join(__dirname, 'dist')));
    // added to serve up products pages
    app.use(function(req, res) {
        // Use res.sendfile, as it streams instead of reading the file into memory.
        res.sendfile(__dirname + '/dist/index.html');
    });

    // production error handler
    // no stacktraces leaked to user
    app.use(function(err, req, res, next) {
        res.status(err.status || 500);
        res.render('error', {
            message: err.message,
            error: {}
        });
    });
}

module.exports = app;

编辑:我已经找到了将“?_escaped_fragment_=”预渲染转换为“#!”的问题。然后 Angular 不知道要使用哪个路由,除非我有 $locationProvider.hashPrefix('!') (我不想使用)。如果可以提供帮助,我不想要 # 前缀。

2015-09-07T12:17:17.566Z got 200 in 12713ms for http://localhost:3000/#!/products
2015-09-07T12:17:18.773Z Timed out. Sending request with HTML on the page
2015-09-07T12:17:18.785Z got 200 in 12732ms for http://localhost:3000/#!/products
2015-09-07T12:19:04.589Z getting http://localhost:3000/#!/products
4

1 回答 1

1

如以下链接所述:

https://github.com/prerender/prerender/issues/198 https://developers.google.com/webmasters/ajax-crawling/docs/specification?hl=en

来自搜索的查询在路径末尾有“?_escaped_fragment_=”,而不是直接在 fqdn 之后。

http://www.minilolo.com/?_escaped_fragment_=/products/Lolo-Pink <-- 不是这个 http://www.minilolo.com/products/Lolo-Pink?_escaped_fragment_= <-- 这个!

于 2015-09-07T20:34:30.730 回答