From 25552b30c8d059d0c27fb1cb2ebef683fd0355ab Mon Sep 17 00:00:00 2001 From: Joshua Young Date: Thu, 3 Oct 2024 17:01:28 +1000 Subject: [PATCH] Feature | `lazy_load_previews_and_pages` config option --- config/app.yml | 1 + docs/src/_data/config_options.yml | 11 +++++++- lib/lookbook/engine.rb | 42 +++++++++++++++++++++++++------ lib/lookbook/reloaders.rb | 2 ++ 4 files changed, 48 insertions(+), 8 deletions(-) diff --git a/config/app.yml b/config/app.yml index 5d074eaf7..6579ccf0e 100644 --- a/config/app.yml +++ b/config/app.yml @@ -50,6 +50,7 @@ shared: component_paths: [app/views, app/components] reload_on_change: ~ + lazy_load_previews_and_pages: false live_updates: false listen_paths: [] listen_extensions: [rb, html.*] diff --git a/docs/src/_data/config_options.yml b/docs/src/_data/config_options.yml index 21bd6f3b3..119ffebee 100644 --- a/docs/src/_data/config_options.yml +++ b/docs/src/_data/config_options.yml @@ -261,7 +261,16 @@ system: example: config.lookbook.reload_on_change = true description: | By default Lookbook uses the value of the `cache_classes` and `reload_classes_only_on_change` Rails config options to decide if - it should attempt to update the preview data after changes. If set the value of this config option will take precedence and be used instead. + it should attempt to update the preview data after changes. If set, the value of this config option will take precedence and be used instead. + + - name: lazy_load_previews_and_pages + types: Boolean + default: "false" + example: config.lookbook.lazy_load_previews_and_pages = true + description: | + By default Lookbook loads all preview and page data on application initialization, and then on changes depending + on the value of `config.lookbook.reload_on_change`. If set to `true`, Lookbook will defer the initial loading of + preview and page data until it is first requested. - name: live_updates types: Boolean diff --git a/lib/lookbook/engine.rb b/lib/lookbook/engine.rb index 6585f9abc..9ca3dd43b 100644 --- a/lib/lookbook/engine.rb +++ b/lib/lookbook/engine.rb @@ -68,13 +68,15 @@ class Engine < Rails::Engine end config.after_initialize do - if Engine.reloading? - reloaders.add(:previews, Engine.preview_watch_paths, opts.listen_extensions, &Engine.method(:load_previews)) - reloaders.add(:pages, Engine.page_watch_paths, opts.page_extensions, &Engine.method(:load_pages)) - reloaders.execute - else - Engine.load_previews - Engine.load_pages + unless opts.lazy_load_previews_and_pages + if Engine.reloading? + reloaders.add(:previews, Engine.preview_watch_paths, opts.listen_extensions, &Engine.method(:load_previews)) + reloaders.add(:pages, Engine.page_watch_paths, opts.page_extensions, &Engine.method(:load_pages)) + reloaders.execute + else + Engine.load_previews + Engine.load_pages + end end Engine.run_hooks(:after_initialize) @@ -196,10 +198,36 @@ def preview_watch_paths def pages @_pages ||= PageCollection.new + + if opts.lazy_load_previews_and_pages && !@_loaded_pages + @_loaded_pages = true + + if reloading? + reloader = reloaders.add(:pages, Engine.page_watch_paths, opts.page_extensions, &Engine.method(:load_pages)) + reloader.execute + else + load_pages + end + end + + @_pages end def previews @_previews ||= PreviewCollection.new + + if opts.lazy_load_previews_and_pages && !@_loaded_previews + @_loaded_previews = true + + if reloading? + reloader = reloaders.add(:previews, Engine.preview_watch_paths, opts.listen_extensions, &Engine.method(:load_previews)) + reloader.execute + else + load_previews + end + end + + @_previews end def preview_controller diff --git a/lib/lookbook/reloaders.rb b/lib/lookbook/reloaders.rb index 8ef8a5a79..a3bf4f7f8 100644 --- a/lib/lookbook/reloaders.rb +++ b/lib/lookbook/reloaders.rb @@ -16,6 +16,8 @@ def add(name, directories, extensions, &callback) Rails.application.reloaders << reloader Rails.application.reloader.to_run { reloader.execute_if_updated } end + + reloader end def execute