Tay Ray Chuan home archive

expiring '/1' pages in bloggart

Sun, 6 Jun 2010 13:10:33 +0800 | Filed under bloggart

Recently, I suggested a fix for ListingContentGenerators - instead of setting, say, /tag/foo/1, set only /tag/foo/. I do have some doubts if this should be considered a 'fix'; from the existing code base, it looks like having a /tag/foo/1 was pretty much intentional.

Anyway, while I await Nick's review, I've gone ahead and pushed this upstream in my fork/setup. However, it poses some challenges for myself, and other earlier users, as it means 'deprecating' references/bookmarks etc. to the old '/1' pages.

To set up permanent redirects for these pages, apply this patch:

diff --git a/generators.py b/generators.py
index 4dc9795..c859d71 100644
--- a/generators.py
+++ b/generators.py
@@ -190,6 +190,18 @@ class ListingContentGenerator(ContentGenerator):

     path_args['pagenum'] = pagenum
     static.set(_get_path() % path_args, rendered, config.html_mime_type)
+
+    # handle requests for legacy /1 pages
+    if pagenum == 1:
+      path_args['pagenum'] = 1
+      redir_from = cls.path % path_args
+      redir_to = _get_path() % path_args
+      content = static.get(redir_from)
+      if content and not content.status==301:
+        static.set(redir_from, '', config.html_mime_type,
+          headers=['Location: %s' % redir_to], status=301
+        )
+
     if more_posts:
         deferred.defer(cls.generate_resource, None, resource, pagenum + 1,
                        posts[-2].published)

Now, some days/months have passed, and you wish to forget about these pages altogether. Here's a patch that goes on top of the previous one:

diff --git a/generators.py b/generators.py
index c859d71..a317d11 100644
--- a/generators.py
+++ b/generators.py
@@ -197,10 +197,8 @@ class ListingContentGenerator(ContentGenerator):
       redir_from = cls.path % path_args
       redir_to = _get_path() % path_args
       content = static.get(redir_from)
-      if content and not content.status==301:
-        static.set(redir_from, '', config.html_mime_type,
-          headers=['Location: %s' % redir_to], status=301
-        )
+      if content:
+        static.remove(redir_from)

     if more_posts:
         deferred.defer(cls.generate_resource, None, resource, pagenum + 1,

After one run through, remove this code block altogether:

diff --git a/generators.py b/generators.py
index a317d11..4dc9795 100644
--- a/generators.py
+++ b/generators.py
@@ -190,16 +190,6 @@ class ListingContentGenerator(ContentGenerator):

     path_args['pagenum'] = pagenum
     static.set(_get_path() % path_args, rendered, config.html_mime_type)
-
-    # handle requests for legacy /1 pages
-    if pagenum == 1:
-      path_args['pagenum'] = 1
-      redir_from = cls.path % path_args
-      redir_to = _get_path() % path_args
-      content = static.get(redir_from)
-      if content:
-        static.remove(redir_from)
-
     if more_posts:
         deferred.defer(cls.generate_resource, None, resource, pagenum + 1,
                        posts[-2].published)

Of course, I imagine there's another way to do this by just looping through all the BlogPost entries; I just though hooking on to the ListingContentGenerator was more convenient.

Note: you can choose to trigger only ListingContentGenerator-type content (eg. tags, archives) with my rc/regen-sel feature, instead of regenerating the whole site.

blog comments powered by Disqus