gpt4 book ai didi

javascript - 为什么我的同构 Cycle.js 应用程序在服务器上渲染时会导致 xstream 异常?

转载 作者:行者123 更新时间:2023-12-03 06:04:57 24 4
gpt4 key购买 nike

我正在尝试编写一个同构演示 Cycle.js/Hapi.js应用程序,但在服务器上渲染时 xstream 失败并出现异常。这里出了什么问题?我的应用程序基于 Cycle.js' isomorphic app example .

回溯看起来像这样:

TypeError: Uncaught error: s[i]._add is not a function
at CombineProducer._start (/Users/arve/Projects/hapi-cycle/node_modules/xstream/core.js:190:22)
at Stream._add (/Users/arve/Projects/hapi-cycle/node_modules/xstream/core.js:976:19)
at MapOperator._start (/Users/arve/Projects/hapi-cycle/node_modules/xstream/core.js:717:18)
at Stream._add (/Users/arve/Projects/hapi-cycle/node_modules/xstream/core.js:976:19)
at LastOperator._start (/Users/arve/Projects/hapi-cycle/node_modules/xstream/core.js:596:18)
at Stream._add (/Users/arve/Projects/hapi-cycle/node_modules/xstream/core.js:976:19)
at Stream.addListener (/Users/arve/Projects/hapi-cycle/node_modules/xstream/core.js:1050:14)
at Object.streamSubscribe (/Users/arve/Projects/hapi-cycle/node_modules/@cycle/xstream-adapter/lib/index.js:39:16)
at /Users/arve/Projects/hapi-cycle/node_modules/@cycle/base/lib/index.js:49:30
at Array.map (native)

渲染代码基本上如下所示:

import Cycle from '@cycle/xstream-run'
import xs from 'xstream'
import {html, section, h1, p, head, title, body, div, script, makeHTMLDriver,} from '@cycle/dom'
import serialize from 'serialize-javascript'
import Logger from '@arve.knudsen/js-logger'
let logger = Logger.get('server.rendering')

let wrapVTreeWithHtmlBoilerplate = ([vtree, context,]) => {
return (
html([
head([
title('Cycle Isomorphism Example'),
]),
body([
div('.app-container', [vtree,]),
script(`window.appContext = ${serialize(context)};`),
// script(clientBundle),
]),
])
);
}

let main = (sources) => {
let vtree = (
section('.home', [
h1('The homepage'),
p('Welcome to our spectacular web page with nothing special here.'),
])
)
return {
DOM: vtree,
}
}

let renderIndex = (request, reply) => {
let context = xs.of({})
Cycle.run((sources) => {
let vtree = main(sources).DOM
let wrappedVTree = xs.combine(vtree, context)
.map(wrapVTreeWithHtmlBoilerplate)
.last()
return {
DOM: wrappedVTree,
};
}, {
DOM: makeHTMLDriver((html) => {
let wrappedHtml = `<!doctype html>${html}`
}),
context: () => {return context},
PreventDefault: () => {},
})
}

您可以找到完整的源代码here .

我在 OS X 上运行,使用 Node v6.6.0、babel-node 6.14.0、Hapi 15.0.3、@cycle/dom 12.2.5 和 @cycle/xstream-run 3.1.0。如果您需要更多信息,请告诉我。

最佳答案

错误的原因是渲染的VTree不是流。我将代码更改为以下内容并且它有效:

let vtree = sources.context.map(({}) => {
return (
section('.home', [
h1('The homepage'),
p('Welcome to our spectacular web page with nothing special here.'),
])
)
})
return {
DOM: vtree,
}

sources.context.map 调用(我从原始同构示例中借用)确保 vtree 是一个流。

关于javascript - 为什么我的同构 Cycle.js 应用程序在服务器上渲染时会导致 xstream 异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39592780/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com