Ghost: Public API doesn't work in error.hbs template

Created on 25 May 2016  路  5Comments  路  Source: TryGhost/Ghost

Issue Summary

Public API doesn't work in error.hbs template

Steps to Reproduce

  1. I had tried use this code for testing error page with enabled api
$.get(ghost.url.api('posts', {limit: "all", include: "tags"})).done(function(data){
      searchData = data.posts;
      console.log(searchData);
 })

and I get this message:

ghost is not defined

Technical details:

  • Ghost Version: 0.8.0
  • Node Version: 0.12.7
  • Browser/OS: chrome
  • Database: mysql
themes / frontend

Most helpful comment

I've always considered that it is bad practice to use the main layout on the error page, and more specifically to include ghost_head or foot on the error template. I think it's better to have a 500 error page that can render freely of any other aspect of the software, to minimise the chance of causing the error to be thrown again.

The server-side error environment is quite different to rendering a normal page of your theme, lots of things aren't loaded, and the reason for this particular issue is that if you call ghost_head from an error page it simply exits rather than adding anything, so as not to cause any more errors...

The default error template provided by Ghost is entirely stand-alone as an example of what we consider best practice here. You can see this file here: user-error.hbs.

However, I've seen that using the default layout in the error template is common amongst themes, and I have a feeling the reason for that is symptom of a different problem - the fact that error.hbs is used for both 404s as well as 5xx errors. It's pretty common to want to heavily customise a 404 page and render all sorts of useful stuff (such as search) on that page to get a user to stay on the site, meanwhile I think it's fairly standard to have a very simple 5xx template.

I'd propose extending the error handling system to support error-404.hbs and error-500.hbs, to handle different sorts of errors differently. I'd propose that ghost_head should still not output anything if we're on a 500 or 5xx error, but that it would be ok to output some things (such as the API scripts) if we're on a 404 page?

All 5 comments

What is the use case for needing the API on an error page?

In my mind, if users are seeing an error then something is wrong and you want to be depending on as few features as possible to ensure that your error page doesn't also end up causing an error!

the error page uses the main design of the theme and I'm trying to get the plugin ghostHunter worked on all pages :) (my search box is in the header)

I've always considered that it is bad practice to use the main layout on the error page, and more specifically to include ghost_head or foot on the error template. I think it's better to have a 500 error page that can render freely of any other aspect of the software, to minimise the chance of causing the error to be thrown again.

The server-side error environment is quite different to rendering a normal page of your theme, lots of things aren't loaded, and the reason for this particular issue is that if you call ghost_head from an error page it simply exits rather than adding anything, so as not to cause any more errors...

The default error template provided by Ghost is entirely stand-alone as an example of what we consider best practice here. You can see this file here: user-error.hbs.

However, I've seen that using the default layout in the error template is common amongst themes, and I have a feeling the reason for that is symptom of a different problem - the fact that error.hbs is used for both 404s as well as 5xx errors. It's pretty common to want to heavily customise a 404 page and render all sorts of useful stuff (such as search) on that page to get a user to stay on the site, meanwhile I think it's fairly standard to have a very simple 5xx template.

I'd propose extending the error handling system to support error-404.hbs and error-500.hbs, to handle different sorts of errors differently. I'd propose that ghost_head should still not output anything if we're on a 500 or 5xx error, but that it would be ok to output some things (such as the API scripts) if we're on a 404 page?

A very thoughtful answer, thanks, it would have been perfect

Closing in favour of the feature spec: #8079

Was this page helpful?
0 / 5 - 0 ratings