Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docsify-server-renderer error #704

Closed
ccforeverd opened this issue Nov 19, 2018 · 21 comments
Closed

docsify-server-renderer error #704

ccforeverd opened this issue Nov 19, 2018 · 21 comments
Labels

Comments

@ccforeverd
Copy link

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

@ccforeverd
Copy link
Author

ccforeverd commented Nov 19, 2018

My work dir:

  • src
    • docs
      • ...
      • index.template.html
    • server
      • controller
        • docs-renderer.js
      • router
        • docs.js
        • index.js
      • index.js
  • package.json
  • ...

@ccforeverd
Copy link
Author

// 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
}

@ccforeverd
Copy link
Author

ccforeverd commented Nov 19, 2018

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:

const curFileName = this.router.parse().file

The method parse real need an argument of path

@yatagasaru
Copy link

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)

@bitkris-dev
Copy link

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?

@jthegedus
Copy link
Contributor

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

@kaesar
Copy link

kaesar commented Jan 15, 2019

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.

@zmnv
Copy link

zmnv commented Feb 8, 2019

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

@zmnv
Copy link

zmnv commented Feb 8, 2019

just download master branch.

npm run serve:ssr

> docsify@4.8.6 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

@zmnv
Copy link

zmnv commented Feb 8, 2019

All works with old 4.1.7 version.

"docsify-cli": "^4.1.7",

Last version 4.3.0 returns error

@zmnv
Copy link

zmnv commented Mar 12, 2019

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

@llwor94
Copy link

llwor94 commented Apr 10, 2019

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

@wll8
Copy link

wll8 commented Apr 16, 2019

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

@randyguogithub
Copy link

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

@jinguowu
Copy link

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

@loverajoel
Copy link

Same here, any update?

yubaolee added a commit to yubaolee/docsify that referenced this issue Jun 30, 2019
jliben pushed a commit to jliben/docsify that referenced this issue Sep 9, 2019
In order to use, just build then pack the dependency:
```
npm run build
npm pack packages/docsify-server-renderer/
```
@naihe138
Copy link

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

@stale
Copy link

stale bot commented Feb 4, 2020

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.

@stale stale bot added the wontfix label Feb 4, 2020
@stale stale bot closed this as completed Feb 11, 2020
@mattvb91
Copy link

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

@boxsnake
Copy link

boxsnake commented Jul 21, 2020

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.

@equinusocio
Copy link
Contributor

equinusocio commented Sep 5, 2020

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

DYSDF pushed a commit to DYSDF/docsify that referenced this issue Aug 19, 2021
DYSDF pushed a commit to DYSDF/docsify that referenced this issue Aug 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests