Skip to content

Commit

Permalink
Merge pull request #641 from joshuay03/add-lazy-load-previews-and-pag…
Browse files Browse the repository at this point in the history
…es-config-option

Feature | `lazy_load_previews_and_pages` config option
  • Loading branch information
allmarkedup authored Oct 23, 2024
2 parents 01f5740 + 25552b3 commit cb4375b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 8 deletions.
1 change: 1 addition & 0 deletions config/app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.*]
Expand Down
11 changes: 10 additions & 1 deletion docs/src/_data/config_options.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 35 additions & 7 deletions lib/lookbook/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions lib/lookbook/reloaders.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cb4375b

Please sign in to comment.