6

I have a CakePHP site whose homepage is cached for 10 minutes at a time using Cake's default options. However I've been alerted that "every once in a while", once a day or so, it's losing the layout, just displaying the page content without the header, styling etc.

Removing the cached version and regenerating the page apparently fixes the problem. But why would/could this be happening in the first place? I can't say I've seen it happen myself (in any of my CakePHP projects) but does anyone else have any experience of this, or any ideas of ways to fix? Much appreciated!

EDIT: Looking at a broken cached home.php file compared to a working cache file, I've spotted this line:

$controller->layout = $this->layout = 'ajax'; //broken
$controller->layout = $this->layout = 'default'; //working

This seems like a bit of a breakthrough. I guess that sometimes the page is being called by an Ajax request, and then being cached in that format until the cache expires. Anyone know why this might be happening (I don't think we're trying to call the homepage via Ajax anywhere, and we don't even have an ajax layout!) and if there's anything we can do to stop it?

thesunneversets
  • 2,560
  • 3
  • 28
  • 46
  • I've seen something similar on occasions, but only in development, so not sure if would be caching in my case. A typical example is where I open Firefox and the page appears to have been loaded without the layout. Never looked into it but will get back if I find out more. – contrebis Jul 21 '11 at 01:34
  • @contrebis - That would be fantastic, if you could even begin to suggest a starting point for investigating this! As it is I have no idea how to even replicate, much less deal with, the problem :-/ – thesunneversets Jul 21 '11 at 01:51
  • I've seen it happen now and then w/ my in-development project. Would be interested to hear if there's a fix / explanation. – Dave Jul 21 '11 at 04:04
  • 1
    Take a look at the renderLayout() function in cake/libs/view/view.php. I'm looking at CakePHP 1.3.10. The first _render tries to do the job properly, but if it fails, it tries again without helpers, and triggers a E_USER_ERROR. Make sure error_reporting captures this, and check the error_log. No layout means there could be a problem with the helpers, or a bad reference in the layout name. Or even an extra ob_start() somewhere else in the code. – Reuben Jul 22 '11 at 04:39
  • 2
    Actually, looking at cake/libs/view/view.php, and renderCache(), cached output is echo'd immediately. So if there are any ob_start() calls, other than the ob_start() inside renderCache(), and it's not closed with an ob_get_clean() or similar, you'll loose the content of the echo. – Reuben Jul 22 '11 at 04:52
  • Theory #3. If the cached view was not written cleanly, or worse, was not read cleanly, then the `` string will be missing from the buffer, and the cached content will never be output. Dodgy reading of the cache can happen if it is being written while being read. That cachetime string should be right at the start of the cached file (and thus the substr to remove it before output). – Reuben Jul 22 '11 at 04:55
  • @Reuben - Wow, thanks for all that! Definitely sounds like a great place for me to start my investigations. Will let you know I get on... – thesunneversets Jul 22 '11 at 14:07
  • Do you at any point use an Ajax call to the home page, or does any page used for an Ajax call potentially redirect to the home page? Maybe that's when the cache picks the wrong layout for the page. – JJJ Aug 08 '11 at 09:57
  • @Juhana: I don't think we are using Ajax to call anything (though I'd better have a good look to make sure). Could such a call be being made from outside the application, or does that make no sense? We don't even have an ajax layout! (So I guess a quick fix would be to make an Ajax layout that's the same as the default layout... though naturally I'd prefer to get to the bottom of the problem instead!) – thesunneversets Aug 08 '11 at 10:11

2 Answers2

1

Are you sure no ajax requests are interfering with your code/request?

We had a nightmare issue recently where cake was not rendering the layout on back/forward browser clicks. See here: CakePHP no layout on back and forward button

It may also help to look into the cake request cycle (http://book.cakephp.org/2.0/en/getting-started/a-typical-cakephp-request.html) and narrow down when in the request (and where) the error is occurring, although I dunno how you would reproduce it :)

Hope you figure it out!

Community
  • 1
  • 1
Jongosi
  • 2,305
  • 1
  • 28
  • 31
0

Also, if you have any errors in your view, sometimes it won't load the layout. It will just spit out the view until the point where it experienced the error. Perhaps the error is not in displaying your layout, but some odd circumstance where you are generating an error with a Helper.

Ryan
  • 116
  • 1
  • 2
  • 10
  • I'm pretty sure at this stage that the page is being displayed as an Ajax request, calling a (previously non-existent) ajax layout. Unfortunately it then caches the resulting mess and uses it for non-Ajax requests. For the time being I've just made the ajax layout the same as the default layout. There's still a question though of why this is happening and how to code it so that it behaves a little more intelligently in future! – thesunneversets Aug 11 '11 at 09:54
  • Could you not just set the layout in your action? – Ryan Aug 13 '11 at 07:15
  • Or you could check the request type, and disable the cache temporarily if its determined to be AJAX http://book.cakephp.org/view/1292/Obtaining-Request-Information – Ryan Aug 13 '11 at 07:18