Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#37 2021-02-12 11:23:21

etc
Developer
Registered: 2010-11-11
Posts: 4,617
Website GitHub

Re: etc_cache: cache trying hard to be smart

I’m trying

    RewriteCond %{DOCUMENT_ROOT}/textpattern/cache/$1 -f [OR]
    RewriteCond %{DOCUMENT_ROOT}/textpattern/cache/$1(\.html) -f [OR]
    RewriteCond %{DOCUMENT_ROOT}/textpattern/cache/$1(\.xml) -f [OR]
    RewriteCond %{DOCUMENT_ROOT}/textpattern/cache/$1index.html -f
    RewriteRule ^(.*) cache/$1%1 [PT,L]

but %1 capture group does not seem to work (see below). Will set an environment variable maybe…

!https://httpd.apache.org/docs/current/images/syntax_rewritecond.png!

But isn’t it reasonable to suggest that if a user wants to output an article as application/javascript, he should then url-title it name.js? This would simplify things a lot.

Offline

#38 2021-02-12 11:23:51

etc
Developer
Registered: 2010-11-11
Posts: 4,617
Website GitHub

Re: etc_cache: cache trying hard to be smart

philwareham wrote #328809:

Just to throw extra work into the mix, I’d also need an Nginx version of the .htaccess rules – sorry!

Not me!

Offline

#39 2021-02-12 11:31:04

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 10,714
Website GitHub

Re: etc_cache: cache trying hard to be smart

etc wrote #328810:

%1 capture group does not seem to work.

I’ve never used %, only $. Long shot: do percent replacements need to be in curly braces? %{1}


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#40 2021-02-12 11:43:31

etc
Developer
Registered: 2010-11-11
Posts: 4,617
Website GitHub

Re: etc_cache: cache trying hard to be smart

Seemingly %n come from RewriteCond:

!https://httpd.apache.org/docs/current/images/rewrite_backreferences.png!

The problem is we need to capture them in the left hand term.

Offline

#41 2021-02-12 11:47:14

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 10,714
Website GitHub

Re: etc_cache: cache trying hard to be smart

Eeek, above my pay grade! I’m only a lowly regex hacker and they normally take me aaaages in regex101 to make them do what I want reliably.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#42 2021-02-12 14:47:47

Vienuolis
Member
From: Vilnius, Lithuania
Registered: 2009-06-14
Posts: 285
Website GitHub GitLab Twitter

Re: etc_cache: cache trying hard to be smart

Textpattern powered websites will now serve visitors as hypertext flat-files, suspended in ‘maintenance mode’, too!

With this separation of dynamic SQL generation and static HTML delivery, in addition to the similar theming, and with the native HTML, CSS, TXP, PHP, also sections, categories, keywords, etc strict separation principle, and managed by smart and open-minded developers, Textpattern stands as exceptionally agile, neat, light, secure, and robust WWW publishing loom, unique on CMS market.

Offline

#43 2021-02-12 14:58:11

etc
Developer
Registered: 2010-11-11
Posts: 4,617
Website GitHub

Re: etc_cache: cache trying hard to be smart

Version 0.2 of etc_static is ready. What’s new:

  • pages with no-cache, no-store, private cache-control headers or any vary header will not be cached. In particular, that’s true in dev mode.
  • pages being visited by a logged-in user will not be cached, though he can be served from public cache. It means you need to log out to populate it.
  • cache will try to append the appropriate extension, so static files should be served with the correct mediatype.

The next big step is an admin interface, so etc_static will probably merge with etc_cache.

You’ll need to update .htaccess with the following:

# --- Append to .htaccess right after RewriteRule ^(.+) - [PT,L] --- 
    RewriteCond %{REQUEST_METHOD} =GET
    RewriteCond %{QUERY_STRING} ^$
    RewriteCond %{DOCUMENT_ROOT}/path/to/site/cache/$1 -f [OR]
    RewriteCond %{DOCUMENT_ROOT}/path/to/site/cache/$1index.html -f
    RewriteRule ^(.*) cache/$1 [PT,L]

    RewriteCond %{REQUEST_METHOD} =GET
    RewriteCond %{QUERY_STRING} ^$
    RewriteCond %{DOCUMENT_ROOT}/path/to/site/cache/$1.html -f
    RewriteRule ^(.*) cache/$1.html [PT,L]

Repeat the last block for every mediatype you intend to use without name extension, replacing html with the latter.

# Name: etc_static v0.2 
# Type: Admin/Public/AJAX plugin
# Static cache
# Author: Oleg Loukianov
# URL: https://github.com/bloatware/
# Recommended load order: 5

# .....................................................................
# This is a plugin for Textpattern CMS - http://textpattern.com/
# To install: textpattern > admin > plugins
# Paste the following text into the 'Install plugin' box:
# .....................................................................

YToxMTp7czo0OiJuYW1lIjtzOjEwOiJldGNfc3RhdGljIjtzOjY6ImF1dGhvciI7czoxNDoi
T2xlZyBMb3VraWFub3YiO3M6MTA6ImF1dGhvcl91cmkiO3M6Mjk6Imh0dHBzOi8vZ2l0aHVi
LmNvbS9ibG9hdHdhcmUvIjtzOjc6InZlcnNpb24iO3M6MzoiMC4yIjtzOjExOiJkZXNjcmlw
dGlvbiI7czoxMjoiU3RhdGljIGNhY2hlIjtzOjQ6ImNvZGUiO3M6MjU3MzoiLyogIC8vIC0t
LSBBcHBlbmQgdG8gLmh0YWNjZXNzIHJpZ2h0IGFmdGVyIFJld3JpdGVSdWxlIF4oLispIC0g
W1BULExdIC0tLSAvLw0KICAgIFJld3JpdGVDb25kICV7UkVRVUVTVF9NRVRIT0R9ID1HRVQN
CiAgICBSZXdyaXRlQ29uZCAle1FVRVJZX1NUUklOR30gXiQNCiAgICBSZXdyaXRlQ29uZCAl
e0RPQ1VNRU5UX1JPT1R9L3BhdGgvdG8vc2l0ZS9jYWNoZS8kMSAtZiBbT1JdDQogICAgUmV3
cml0ZUNvbmQgJXtET0NVTUVOVF9ST09UfS9wYXRoL3RvL3NpdGUvY2FjaGUvJDFpbmRleC5o
dG1sIC1mDQogICAgUmV3cml0ZVJ1bGUgXiguKikgY2FjaGUvJDEgW1BULExdDQoNCiAgICBS
ZXdyaXRlQ29uZCAle1JFUVVFU1RfTUVUSE9EfSA9R0VUDQogICAgUmV3cml0ZUNvbmQgJXtR
VUVSWV9TVFJJTkd9IF4kDQogICAgUmV3cml0ZUNvbmQgJXtET0NVTUVOVF9ST09UfS9wYXRo
L3RvL3NpdGUvY2FjaGUvJDEuaHRtbCAtZg0KICAgIFJld3JpdGVSdWxlIF4oLiopIGNhY2hl
LyQxLmh0bWwgW1B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ICAgIGlmICghZW1wdHkoJGhlYWRlcnNbJ2NvbnRlbnQt
dHlwZSddKSkgZm9yZWFjaCgkbWltZXR5cGVzIGFzICR0eXBlID0+ICRleHQpIHsNCiAgICAg
IGlmIChzdHJwb3MoJGhlYWRlcnNbJ2NvbnRlbnQtdHlwZSddLCAkdHlwZSkgIT09IGZhbHNl
KSB7DQogICAgICAgICRmaWxlIC49ICRleHRlbnNpb24gPT09ICRleHQgPyAnJyA6ICcuJy4k
ZXh0OyBicmVhazsNCiAgICAgIH0NCiAgICB9DQoNCiAgICAkZGlyID0gJGNhY2hlLmltcGxv
ZGUoJy8nLCAkZGlyKTsNCiAgICBpZiAoIWlzX2RpcigkZGlyKSkgbWtkaXIoJGRpciwgMDc3
NywgdHJ1ZSk7DQogICAgJG91dCA9IG9iX2dldF9jb250ZW50cygpOw0KICAgIGZpbGVfcHV0
X2NvbnRlbnRzKCRkaXIuJy8nLiRmaWxlLCAkb3V0KTsNCiAgfSwgJ3RleHRwYXR0ZXJuX2Vu
ZCcpOw0KfSwgJ3RleHRwYXR0ZXJuJyk7DQoiO3M6NDoidHlwZSI7czoxOiI1IjtzOjU6Im9y
ZGVyIjtzOjE6IjUiO3M6NToiZmxhZ3MiO3M6MToiMCI7czo0OiJoZWxwIjtzOjA6IiI7czoz
OiJtZDUiO3M6MzI6IjY1YTkwZGZlMTZkYTMzNzg5YmVjNDM3NDE2Y2I5Y2VkIjt9

Offline

#44 2021-02-12 15:07:48

etc
Developer
Registered: 2010-11-11
Posts: 4,617
Website GitHub

Re: etc_cache: cache trying hard to be smart

If someone could translate these apache rules to Nginx, that would be ace.

Offline

#45 2021-02-12 15:49:03

philwareham
Core designer
From: Haslemere, Surrey, UK
Registered: 2009-06-11
Posts: 3,553
Website GitHub Twitter

Re: etc_cache: cache trying hard to be smart

Pete is the resident Nginx expert – but I’ve pestered him enough this week. Will mention it to him in due course, I’m sure there are ways to duplicate your Apache rules within it.

Offline

#46 2021-02-13 16:18:56

Vienuolis
Member
From: Vilnius, Lithuania
Registered: 2009-06-14
Posts: 285
Website GitHub GitLab Twitter

Re: etc_cache: cache trying hard to be smart

I guess that an URL Toolkit for the Hiawatha werbserver would much simpler, inserting two extra (bolder) lines only for the cache:

UrlToolkit {
ToolkitID = textpattern
RequestURI exists Return
Match ^/(files|images)/ Return
Match ^/(favicon.ico|robots.txt|sitemap.xml)$ Return
Match ^/themes/reguo/styles/reguo.css$ Return
Method GET Return
Match ^/(.*) Rewrite /cache/$1.html
Match [^?]*(\?.*)? Rewrite /index.php$1
}

Did I catch the right meaning of these Apache rules, Oleg?

Offline

#47 2021-02-13 17:32:05

etc
Developer
Registered: 2010-11-11
Posts: 4,617
Website GitHub

Re: etc_cache: cache trying hard to be smart

Dunno, I’m new to rewrites. The idea is:

  • if the request type is GET
  • and if the query string (i.e. ?something) is empty
  • and cache/request_uri or cache/request_uri/index.html or cache/request_uri.html exist

redirect request_uri to cache/request_uri(.html). For example,

/section/?pg=2   ->   leave it alone
/section/        ->   /cache/section/ (will serve index.html from there)
/section/title   ->   /cache/section/title.html

but only if these .html files exist.

Am I complicating? The first version did not append .html to cached files, but they were served without content-type header, though browsers considered them as html by default.

Offline

#48 2021-02-14 05:52:39

Pat64
Plugin Author
From: France
Registered: 2005-12-12
Posts: 1,517
Website GitHub Twitter

Re: etc_cache: cache trying hard to be smart

I’m sorry Oleg, but your second plugin version do not work anymore: the cache directory isn’t created, so there is no more conversion into static files…
Tested on a local MAMP PC server.


Patrick.

Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.

Offline

Board footer

Powered by FluxBB