probberechts / hexo-theme-cactus

:cactus: A responsive, clean and simple theme for Hexo.
https://probberechts.github.io/hexo-theme-cactus/
MIT License
3.15k stars 783 forks source link

Handle 404's with page #290

Closed chelsea-codes closed 1 year ago

chelsea-codes commented 2 years ago

It would be nice to have 404's handled with a page out-of-the-box - or maybe have it be optional.

Thanks for the theme!

Chelsea

probberechts commented 2 years ago

I agree. Pull requests are welcome!

metters commented 2 years ago

Is it even possible to add this feature? If I am not mistaken, this is something handled by the web-server.

probberechts commented 2 years ago

The webserver can be configured to show a specific page on a 404 error, but by default it typically redirects to a 404.html page at the root if such a page exists. The requested feature is to implement such a custom 404 page.

metters commented 2 years ago
  1. I add a new page with hexo new page 404 and add some text:
    
    ---
    title: 404
    date: 2022-06-27 19:57:51
    ---

Cannot find the page you are searching for.

2. In order to add styling I need to add an `*.ejs`-file, lets say I named it `notfound.ejs`, because I am afraid `404.ejs` will cause issues. I am going to rename it to `404.ejs` at the end, if possible.
3. Since, the 404 page is of type page I need to add the processing step the [`page.ejs`](https://github.com/probberechts/hexo-theme-cactus/blob/master/layout/page.ejs):
```ejs
<article class="post" itemscope itemtype="http://schema.org/BlogPosting">
  <%- partial('_partial/post/gallery') %>
  <div class="content" itemprop="articleBody">
      <% if (page.notfound) { %>
          <%- partial('_partial/notfound') %>
      <% } else if (page.search || page.type === "search") { %>
        <%- partial('_partial/search') %>
      <% } else if (page.type === "tags") { %>
... and so on
metters commented 2 years ago

I am aware that the 404 page is being generated as index.html inside a 404-directory at the moment. This is something, I need to resolve too. Maybe my whole approach does not work, because the goal is to have the 404 in the root directory.

veraposeidon commented 2 years ago

@metters You can use Hexo Generator to register a route for /404.html. Also, I added 404-page-generating support in this PR #330, and you can merge this PR to have a try.

metters commented 2 years ago

Thanks!

I tried and my screen stays white when I do a local hexo server. Probably a mistake on my side, I am going to try again sometime this weekend.

veraposeidon commented 2 years ago

You may check the hexo_project/public folder after running hexo generate. It should generate a 404.html there.

metters commented 1 year ago

i believe this issue can be closed.