Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

All other Modules /

Discuss all other Modules here.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Bug in StaticPublisher?!


Go to End


17 Posts   3741 Views

Avatar
Garrett

Community Member, 245 Posts

24 September 2009 at 8:03am

Edited: 24/09/2009 8:03am

Hi,

I am nearly certain I have discovered a bug in the Static Publisher which disables you from exporting any site to HTML files which contains one or more instances of a Redirect Page containing an external URL. This appears to occur in \sapphire\filesystem\Filesystem.php where the method MakeFolder() attempts to name the file using the external URL(s) from the Redirect Pages. This can easily be reproduced in any version 2.3 and later by creating a Redirect Page on your site which has an external URL associated with it and then making a request in the browser to http://www.mysite.com/dev/buildcache?flush=1.

I think everyone will agree that when exporting a site to cache, links that redirect to external URL's need not have pages created for them.

Can anyone offer some help on how I can fix this locally? Add some code to prevent pages of this ClassName from being exported?

Any help offered would be greatly appreciated :)

Thanks,
Garrett

Avatar
martimiz

Forum Moderator, 1391 Posts

24 September 2009 at 8:29pm

I'm using StaticPublisher quite a bit, but never used redirectorpages. So I didn't test this yet, but if it is true, I'd consider it a bug as well :-(

You should be able to manually exclude the RedirectorPage type in your page's allPagesToCache() and pagesAffectedByChanges() methods. Something like

if ($this->ClassName != 'RedirectorPage') { ... }

Avatar
Garrett

Community Member, 245 Posts

25 September 2009 at 2:03am

Edited: 25/09/2009 2:04am

Yeah. I ended up doing exactly that:

function allPagesToCache() {

$urls = array();

$pages = DataObject::get("SiteTree");

foreach($pages as $page) {
if($page->ClassName!="RedirectorPage") {
$urls = array_merge($urls, (array)$page->subPagesToCache());
}
}

}

And that pretty much takes care of it. Thanks for replying. Hey listen-- since you're familiar with StaticPublisher, and I'm new, let me ask you a question-- how do you DEPLOY this static version? I looked at the documentation but it's a little unclear. It refers to an .htaccess file here, but this file is confusing because I think it's a collection of examples instead of the ACTUAL file I should use. I haven't been able yet to configure it such that when I request the normal URL for the site I get the static version. Could you walk me through that?

Thanks,
Garrett

Avatar
martimiz

Forum Moderator, 1391 Posts

25 September 2009 at 3:23am

Since you're clear about the allPagesToCache() and pagesAffectedByChanges() methods, I assume you got to the point where static files are written to the cache directory? If not, don't hesitate... :-)

Now all you need is a working .htaccess file. On my sites I'm using the .htaccess file pretty much as in the example, I just leave out the ## CONFIG FOR DEV ENVIRONMENTS part.

The '# Cached content - live webserver' part redirects request to the static file with the same name as the URLSegment - if they exist.
The '# Cached content - homepage' part redirects requests for the home page (www.mydomain.ext) to the cached index.html - if it exists.
The '# Dynamic content' part handles all other requests that couldn't be mapped to

On some hosts %{DOCUMENT_ROOT} is not defined. In that case it won't work, you'll have to replace it by a 'hardcoded' path to the doc root. See also http://www.silverstripe.org/customising-the-cms/show/257044#post257044

I'll paste in the part that works for me (on LAMP):


### SILVERSTRIPE START ###
RewriteEngine On
RewriteBase /

	
## CONFIG FOR TEST/LIVE ENVIRONMENTS
	
# Cached content - live webserver
RewriteCond %{REQUEST_METHOD} ^GET$
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{REQUEST_URI} /(.*[^/])/?$
RewriteCond %{DOCUMENT_ROOT}/html-cache/%1.html -f
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* /html-cache/%1.html [L]

# Cached content - homepage
RewriteCond %{REQUEST_METHOD} ^GET$
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{REQUEST_URI} ^/?$
RewriteCond %{DOCUMENT_ROOT}/html-cache/index.html -f
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* /html-cache/index.html [L]
	
## DYNAMIC CONFIG
	
# Dynamic content
RewriteCond %{REQUEST_URI} !(\.gif)|(\.jpg)|(\.png)|(\.css)|(\.js)|(\.php)$
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* sapphire/main.php?url=%1&%{QUERY_STRING} [L]
### SILVERSTRIPE END ###

Avatar
Garrett

Community Member, 245 Posts

25 September 2009 at 5:00am

Hi-- thanks again for your continued help. However, I am not noticing ANYTHING different in how the site behaves once I applied these changes. I'm pretty sure it's still running dynamic. My original RewriteBase was /looksmart. Here is my new .htaccess file:

### SILVERSTRIPE START ###

RewriteEngine On
	
## CONFIG FOR DEV ENVIRONMENTS

# Cached content - cache subdirectory
RewriteCond %{REQUEST_METHOD} ^GET$
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{REQUEST_URI} ^/looksmart/cache/(.*)$
RewriteCond %{REQUEST_URI} /looksmart/cache/(.*[^/])/?$
RewriteCond %{DOCUMENT_ROOT}/looksmart/cache/%1.html -f
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* /looksmart/cache/%1.html [L]

# Cached content - homepage
RewriteCond %{REQUEST_METHOD} ^GET$
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{REQUEST_URI} ^/looksmart/cache/?$
RewriteCond /looksmart/cache/index.html -f
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* /looksmart/cache/index.html [L]

## DYNAMIC CONFIG

# Dynamic content
RewriteCond %{REQUEST_URI} !(\.gif)|(\.jpg)|(\.png)|(\.css)|(\.js)|(\.php)$
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* sapphire/main.php?url=%1&%{QUERY_STRING} [L]

### SILVERSTRIPE END ###

The /looksmart/cache folder exists, and all the *.html files are in there. What am I doing wrong?

Avatar
socks

Community Member, 191 Posts

25 September 2009 at 7:47am

You can do a test to make sure that the static HTML pages are being served. Just download one of those HTML pages & make a simple change and see if it renders live.

Avatar
Garrett

Community Member, 245 Posts

25 September 2009 at 7:54am

Hi and thanks for your reply-- I think you may have misunderstood, however. There is nothing wrong with the HTML files themselves, it's just that when I go to the URL I normally would to look at this site, I am still seeing the dynamic version despite my drastic changes to the .htaccess file. I think I followed the StaticPublisher instructions correctly but alas it has had no effect. Ideas?

//Garrett

Avatar
socks

Community Member, 191 Posts

25 September 2009 at 8:40am

Edited: 25/09/2009 8:48am

It sounded like you were unsure if the Dynamic pages or the HTML pages were actually being served. That's why I suggested you modify an HTML page directly, visit your web browser and check to make sure the Static Publisher was or was not working.

If this is for your hosted site, not localhost, I think you need to put back your RewriteBase and use this exact code (at least that's what I did and it's working properly). I also left out the Config for Dev Environments (localhost). In martimiz's example, the default folder was changed from "cache" to "html-cache".

1.

### SILVERSTRIPE START ### 

<Files *.ss>
Order deny,allow
Deny from all
Allow from 127.0.0.1
</Files> 

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /looksmart

## CACHE CONFIG FOR TEST/LIVE ENVIRONMENTS ##

# Cached content - live webserver
RewriteCond %{REQUEST_METHOD} ^GET$
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{REQUEST_URI} /(.*[^/])/?$
RewriteCond %{DOCUMENT_ROOT}/cache/%1.html -f
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* /cache/%1.html [L]         

# Cached content - homepage
RewriteCond %{REQUEST_METHOD} ^GET$
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{REQUEST_URI} ^/?$
RewriteCond %{DOCUMENT_ROOT}/cache/index.html -f
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* /cache/index.html [L]   

## DYNAMIC CONFIG ##

# Dynamic content
RewriteCond %{REQUEST_URI} !(\.gif)|(\.jpg)|(\.png)|(\.css)|(\.js)|(\.php)$
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* sapphire/main.php?url=%1&%{QUERY_STRING} [L]   
</IfModule>   
### SILVERSTRIPE END ###

2. Did you add this to your mysite > _config.php ?

Object::add_extension("SiteTree", "FilesystemPublisher('cache/', 'html')");

3. And it sounds like you have put the needed code into Page.php (//Return a list of all pages to cache... & //Get a list of URLs to cache related to this page)

4. And obviously you ran mysite.com/dev/buildcache or that cache folder wouldn't exist.

Go to Top