docsify-server-renderer error

Created on 19 Nov 2018  路  21Comments  路  Source: docsifyjs/docsify

My code here:

// docs-renderer.js
const fs = require('fs')
const path = require('path')
const docsifyRenderer = require('docsify-server-renderer')
const templatePath = path.resolve(__dirname, '../../docs/index.template.html')

const docsifyConfig = {
  basePath: '/src',
  // ...
}

const renderer = new docsifyRenderer({
  template: fs.readFileSync(templatePath, 'utf-8'),
  config: docsifyConfig
})

async function test () {
  renderer.renderToString('docs/')
    .then(console.log)
    .catch(console.log)
}

test()

module.exports = renderer

Then:

TypeError: Cannot read property 'indexOf' of undefined
    at AbstractHistory.parse (/Users/zhangshuyao/Sites/git/docs/node_modules/docsify-server-renderer/build.js:349:30)
    at Compiler.compile (/Users/zhangshuyao/Sites/git/docs/node_modules/docsify-server-renderer/build.js:553:37)
    at Renderer.<anonymous> (/Users/zhangshuyao/Sites/git/docs/node_modules/docsify-server-renderer/build.js:1126:28)
    at Generator.next (<anonymous>)
    at c (/Users/zhangshuyao/Sites/git/docs/node_modules/docsify-server-renderer/build.js:14:99)
    at process._tickCallback (internal/process/next_tick.js:68:7)
    at Function.Module.runMain (internal/modules/cjs/loader.js:744:11)
    at startup (internal/bootstrap/node.js:285:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:739:3)

There is shotcut of docsify-server-renderer/build.js

// line 553
  var curFileName = this$1.router.parse().file;
// line 346-360
  AbstractHistory.prototype.parse = function parse (path$$1) {
    var query = '';

    var queryIndex = path$$1.indexOf('?');
    if (queryIndex >= 0) {
      query = path$$1.slice(queryIndex + 1);
      path$$1 = path$$1.slice(0, queryIndex);
    }

    return {
      path: path$$1,
      file: this.getFile(path$$1),
      query: parseQuery(query)
    }
  };

I think maybe there is something wrong with my code or build.js after rollup-build, pls help me to check it.

Best

wontfix

Most helpful comment

All works with old 4.1.7 version.

"docsify-cli": "^4.1.7",

Last version 4.3.0 returns error

All 21 comments

My work dir:

  • src

    • docs

    • ...

    • index.template.html

    • server

    • controller



      • docs-renderer.js



    • router



      • docs.js


      • index.js



    • index.js

  • package.json
  • ...
// src/server/router/docs.js

require('colors')
const path = require('path')
const send = require('koa-send')
const renderer = require('../controller/docs-renderer')

const docsDir = path.resolve(__dirname, '../../docs')

const routerName = 'docs'
const routerPath = '/docs/(.*)?'
const routerHandler = async ctx => {
  const sendPath = ctx.path.replace(`/${routerName}`, '') || '/'
  const sendOptions = {
    root: docsDir,
    index: 'index.html'
  }

  try {
    await send(ctx, sendPath, sendOptions)
  } catch (e) {
    console.log(`[${'send'.magenta} ${'404'.red}] ${ctx.path}`)
  }
}

module.exports = {
  name: routerName,
  path: routerPath,
  handler: routerHandler
}

When i visit http://localhost:3333/docs#/, it's all OK with router model hash. But if i try ssr, there is allways a error of parse.

Pls see:

https://github.com/docsifyjs/docsify/blob/a257a4ab4eaaa5bf5e9345f0ffd6f5d9dd476c40/src/core/render/compiler.js#L121

https://github.com/docsifyjs/docsify/blob/a257a4ab4eaaa5bf5e9345f0ffd6f5d9dd476c40/src/core/router/history/abstract.js#L10

The method parse real need an argument of path

I have the same problem

const Renderer = require('docsify-server-renderer')
const readFileSync = require('fs').readFileSync
const express = require ('express');

const app = express();

let renderer = new Renderer({
    template: readFileSync('./docs/index.template.html', 'utf-8'),
    config: {
        name: 'Test',
        basePath: '/docs',
        repo: 'te/st',
        loaddNavbar: true, 
        loadSidebar: true, 
        subMaxLevel: 3, 
        auto2top: true
    }
})
app.get('*', (req, res) =>{
    renderer.renderToString(req.url, (html, err) =>{
        if(err) throw err
        console.log(html)
    })
})
app.listen(3000, console.log("RUNING ON PORT 3000"))
TypeError: Cannot read property 'indexOf' of undefined
    at AbstractHistory.parse (/home/ga/tes/ns/doc/node_modules/docsify-server-renderer/build.js:349:30)
    at Compiler.compile (/home/ga/tes/ns/doc/node_modules/docsify-server-renderer/build.js:553:37)
    at Renderer.<anonymous> (/home/ga/tes/ns/doc/node_modules/docsify-server-renderer/build.js:1126:28)
    at Generator.next (<anonymous>)
    at c (/home/ga/tes/ns/doc/node_modules/docsify-server-renderer/build.js:14:99)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:189:7)

I've the exact same error. I'm trying to use docsify with SSR using docsify-server-renderer but seems impossible. Also, the documentation talks about a renderer.renderToString(url) but it's not explained what the "url" parameter should be.

@QingWei-Li Can you take a look, please?

Has anyone been able to identify which was the last working version of docsify?

About docsify-server-renderer/build.js, even if you change this ...

// line 346-360
  AbstractHistory.prototype.parse = function parse (path$$1) {
    var query = '';

    var queryIndex = path$$1.indexOf('?');
    if (queryIndex >= 0) {
      query = path$$1.slice(queryIndex + 1);
      path$$1 = path$$1.slice(0, queryIndex);
    }

    return {
      path: path$$1,
      file: this.getFile(path$$1),
      query: parseQuery(query)
    }
  };

by ... (around line 349)

// line 346-360
  AbstractHistory.prototype.parse = function parse (path$$1) {
    var query = '';
    var queryIndex = -1;                             // new
    if (path$$1) queryIndex = path$$1.indexOf('?');  // edited
    if (queryIndex >= 0) {
      query = path$$1.slice(queryIndex + 1);
      path$$1 = path$$1.slice(0, queryIndex);
    }

    return {
      path: path$$1,
      file: this.getFile(path$$1),
      query: parseQuery(query)
    }
  };

It works apparently or simply avoid the error. But I got another situation, may be with the basePath, because the README.md file don't load.
I don't know if I'm wrong, but it seems that if you use docsify-cli it doesn't render the content for better SEO either.

Same error! Just download https://github.com/docsifyjs/docsify-ssr-demo
npm install
npm start

[SSR] Serving . now.
Listening at http://localhost:4000

TypeError: Cannot read property 'indexOf' of undefined

just download master branch.

npm run serve:ssr

> [email protected] serve:ssr /Users/user/docsify-master
> cross-env SSR=1 node server

Serving "/Users/user/docsify-master" at http://127.0.0.1:3000
Ready for changes
Ready for changes
(node:2594) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'indexOf' of undefined

All works with old 4.1.7 version.

"docsify-cli": "^4.1.7",

Last version 4.3.0 returns error

No. Something wrong.. Can you update docsify-cli or ssr example repo, pls?

i'm having the same issue... anyone have any luck?

It seems that the examples on docsify-ssr-demo are incomplete and incorrect.
I don't know why.

I don't know how https://docsify.now.sh/ssr is implemented.

git clone https://github.com/docsifyjs/docsify-ssr-demo
cd docsify-ssr-demo
npm i 
npm start
# open http://localhost:4000
# TypeError: Cannot read property 'indexOf' of undefined
git clone https://github.com/docsifyjs/docsify
cd docsify
npm i
npm run dev:ssr
# open http://localhost:3000
# UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'indexOf' of undefined

The same error found, even upgrade to the latest version of docsify

The same error found, even upgrade to the latest version of docsify 4.3

Same here, any update?

The same error found, even upgrade to the latest version of docsify

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Still having this issue. Did anyone manage to find a fix?

I looked around the code, and found the possible reason:

  • It seems that the "main" render part resolve relative paths to absolute paths before rendering.
  • And when _renderFile is called, for absolute paths, the function uses fetch to get file.
  • Since fetch is not available for local files without a file:// scheme, the fetch won't work.
  • As a result, the content is empty.

Same here with v4.4.1. Is the fix already released?

Was this page helpful?
0 / 5 - 0 ratings