error_page_assets icon indicating copy to clipboard operation
error_page_assets copied to clipboard

Supporting more than just error pages

Open md5 opened this issue 2 years ago • 4 comments

This is a nice little gem. I'm glad it's simple enough that it actually still works despite not being updated in a few years.

I notice that this is hard-coded to only look for files that have a numeric filename. For our use case, we would like to use this to generate a static "maintenance" page that is not used for errors. For the time being, I have worked around it by using 999.html.erb as the filename.

md5 avatar May 04 '23 19:05 md5

Amazing. I can't believe it still works. And how did you find it??

Haha, that workaround sounds fine.

bronson avatar Jul 21 '23 04:07 bronson

Related ... I never took the time to review PR #7. Do you have an opinion on whether it should be merged or not? (The need is there but now that I'm not running any Rails sites I can't run the code)

bronson avatar Jul 21 '23 04:07 bronson

I'd rather see the task called ErrorPageAssets::RakeTask instead of using the Rake namespace, since this is not part of rake. Other than that, it looks like a solid improvement to me.

It's a bit hard to review that PR on its face, but this diff makes it easier:

--- /dev/fd/11	2023-07-21 16:01:21
+++ /dev/fd/12	2023-07-21 16:01:21
@@ -1,17 +1,19 @@
+require 'rake'
+require 'rake/tasklib'
 require 'fileutils'
 
-# Use the Asset Pipeline to generate static error pages.
-# For example, /app/assets/html/404.html.erb will be compiled to /public/404.html
+module Rake
+  class ErrorPageAssetsTask < Rake::TaskLib
+    attr_accessor :name
 
+    def initialize(name = :error_pages, task_dependencies = [])
+      self.name = name
 
-Rake::Task['assets:precompile'].enhance do
-  Rake::Task['assets:precompile:error_pages'].invoke
+      yield self if block_given?
+
+      define
 end
 
-Rake::Task['assets:clobber'].enhance ['assets:clobber:error_pages']
-
-
-namespace :assets do
     def log msg
       # try to log like Sprockets even though their stuff is all private
       STDERR.puts msg
@@ -30,9 +32,11 @@
       end
     end
 
+    def define
+      namespace :assets do
   namespace :precompile do
     desc 'Copy the newest error page assets into /public'
-    task :error_pages do
+          task name do
             process_error_files do |src, dst|
               log "copy #{src} to #{dst}"
               FileUtils.cp src, dst
@@ -42,7 +46,7 @@
 
   namespace :clobber do
     desc 'Remove the error page assets in /public'
-    task :error_pages do
+          task name do
             process_error_files do |src,dst|
               log "clobber #{dst}"
               FileUtils.rm_f dst
@@ -50,4 +54,6 @@
     end
   end
 end
-
+    end
+  end
+end

md5 avatar Jul 21 '23 23:07 md5

Amazing. I can't believe it still works. And how did you find it??

Haha, that workaround sounds fine.

I don't recall exactly, but my browser history seems to show that I found it by searching for rails asset precompile error pages. I think I also looked at a couple blog posts and StackOverflow threads.

I also just ended up using 503.html.erb for the filename since we decided to serve the page with a 503 status.

md5 avatar Jul 21 '23 23:07 md5