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

怎样实现这样的链接 mydomain.com/zh_cn/document 或者 mydomain.com/en/document,而在网址中没有/#/ #720

Closed
graydonli opened this issue Dec 19, 2018 · 11 comments
Labels

Comments

@graydonli
Copy link

我使用docsify有好几个月了,最近买了域名,然后也成功实现了把个人域名加进去。可是现在每次链接都是mydomain.com/#/document_01 mydomain.com/#/zh_cn/document_01 总是有/#/ 我怎样才能去掉每篇文章链接中的/#/

@upupming
Copy link
Contributor

upupming commented Dec 19, 2018 via email

@graydonli
Copy link
Author

谢谢!我昨晚试了把rountermode配置成history模式,可以去掉#,可是每次刷新页面,就出现404了,我查到vuejs,是这样规定的,不过可以有解决方案。你有解决方案吗?我不是很懂前端

@upupming
Copy link
Contributor

刷新的话确实是不能访问的:

image

看了一下 vuejs 对 History Mode 的解释,这是因为使用了 pushState,这样我们才能够到达指定 URL 而不用重新加载页面。而刷新的时候是直接请求的,如果服务端不加配置的话就会直接返回 404。

vuejs 的文档中已经给出了解决方案,就是在刷新请求其他页面的时候,都将其重定向到 index.html 就行。

比如我用的 Nginx,配置如下:

location / {
  try_files $uri $uri/ /index.html;
}

效果如下:

https://katex.upupming.site/supported

另外:Github Pages 是静态资源托管,应该是不支持这种处理的,所以必须使用带服务端技术的托管服务,Firebase、now.sh、heroku 这几个应该都是可以的。

@graydonli
Copy link
Author

非常感谢!我试下部署到netlify上看能不能试下这种操作

@jthegedus
Copy link
Contributor

jthegedus commented Jan 28, 2019

For the English speakers/readers:

using the history routerMode will error with a return of

Cannot GET /some-route

as the web page doesn't hit the index.html that has Docsify in it, so it can't then route to the correct .md resource and process it for rendering.

To fix this, you must configure your hosting server to redirect any non-root URLs to the root. That is: yoursite.com/apples should redirect to yoursite.com/, Docsify will load and then should send you to the /apples route.

With Firebase Hosting & Docsify "routerMode": "history" your Hosting rules should be:

{
  "hosting": {
    "public": "docs",
    "ignore": [".*"],
    "rewrites": [
      {
        "source": "**",
        "destination": "/index.html"
      }
    ]
  }
}

This will re-route every URL under your domain and send it to /index.html

Issues: the sidebar header defaults to window.location.pathname which is not / when you load a non-root route. That is, yoursite.com/apples is the current pathname despite the Firebase Hosting rewrite. To fix this, you should use the Docisify config nameLink.

My config with the above Firebase Hosting config is:

  // history mode routing
  routerMode: "history",
  nameLink: "/",

Thanks to everyone here for investigating and Google Translate 💯

I will endeavour to update the docs with this information.

@timaschew
Copy link
Member

@jthegedus great job! Closing this issue. I've created #759 to improve docs.

@graydonli
Copy link
Author

For the English speakers/readers:

using the history routerMode will error with a return of

Cannot GET /some-route

as the web page doesn't hit the index.html that has Docsify in it, so it can't then route to the correct .md resource and process it for rendering.

To fix this, you must configure your hosting server to redirect any non-root URLs to the root. That is: yoursite.com/apples should redirect to yoursite.com/, Docsify will load and then should send you to the /apples route.

With Firebase Hosting & Docsify "routerMode": "history" your Hosting rules should be:

{
  "hosting": {
    "public": "docs",
    "ignore": [".*"],
    "rewrites": [
      {
        "source": "**",
        "destination": "/index.html"
      }
    ]
  }
}

This will re-route every URL under your domain and send it to /index.html

Issues: the sidebar header defaults to window.location.pathname which is not / when you load a non-root route. That is, yoursite.com/apples is the current pathname despite the Firebase Hosting rewrite. To fix this, you should use the Docisify config nameLink.

My config with the above Firebase Hosting config is:

  // history mode routing
  routerMode: "history",
  nameLink: "/",

Thanks to everyone here for investigating and Google Translate 💯

I will endeavour to update the docs with this information.

The docs which how to deploy documents to NOW.sh does not detailed enough. Please update it. Thanks so much!!

Here's the docs I talk about just now.
https://docsify.js.org/#/ssr

@upupming
Copy link
Contributor

upupming commented Feb 1, 2019

@watson8544
You can refer the now.sh docs at https://zeit.co/docs/v1/, and this repo https://github.com/zeit/now-examples may help.

By the way, you should be clear about the difference between v1 and v2 of now.sh.

@jthegedus
Copy link
Contributor

There is a lot of work to improve the hosting docs. I will endeavor to improve them when I get a change. The changes I think need to be made are:

  • split SSR vs standard Docsify hosting examples
  • make notes on specific config, what routerMode do the various hosting services support/prefer
  • add an example for each hosting service for readers to further investigate and discuss in issues (so people can refer to a concrete example as opposed to "my setup is private but I'm doing...")

@likenow
Copy link

likenow commented Apr 24, 2020

@adoyle-h
Copy link

@jthegedus How to use "routerMode": "history" for Github Pages? I cannot redirect any non-root URLs to the root.

Related Issue: #238

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

6 participants