Module: SvelteOnRails::BootContext

Defined in:
lib/svelte_on_rails/lib/boot_context.rb

Class Method Summary collapse

Class Method Details

.descriptionObject

Short human-readable label for the log line when we skip SSR startup.



39
40
41
42
43
44
45
46
47
# File 'lib/svelte_on_rails/lib/boot_context.rb', line 39

def description
  return "rails server"               if defined?(::Rails::Server)
  return "puma"                       if puma_launcher_alive? || program_basename == "puma"
  return "rake: #{Rake.application.top_level_tasks.join(', ')}" if rake_task?
  return "rails console"              if defined?(::Rails::Console)
  return "rails runner"               if defined?(::Rails::Command::RunnerCommand)
  return "rails dbconsole"            if defined?(::Rails::DBConsole)
  "program=#{program_basename}"
end

.program_basenameObject



61
62
63
# File 'lib/svelte_on_rails/lib/boot_context.rb', line 61

def program_basename
  File.basename($PROGRAM_NAME.to_s)
end

.puma_launcher_alive?Boolean

Returns:

  • (Boolean)


49
50
51
52
53
54
# File 'lib/svelte_on_rails/lib/boot_context.rb', line 49

def puma_launcher_alive?
  return false unless defined?(::Puma::Launcher)
  ObjectSpace.each_object(::Puma::Launcher).any?
rescue
  false
end

.rake_task?Boolean

Returns:

  • (Boolean)


56
57
58
59
# File 'lib/svelte_on_rails/lib/boot_context.rb', line 56

def rake_task?
  defined?(::Rake) && Rake.respond_to?(:application) &&
    Rake.application.top_level_tasks.any?
end

.serving_http?Boolean

Returns true when we are certain the current process is a Puma web server (or a rails server process that will boot Puma). Everywhere else (rake, console, runner, tests, generators, unknown wrappers) returns false. False negatives are safe because the SsrClient has a lazy-start fallback that kicks in on the first render request.

Returns:

  • (Boolean)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/svelte_on_rails/lib/boot_context.rb', line 10

def serving_http?
  # 1. Explicit user overrides always win.
  return true  if ENV["SVELTE_ON_RAILS_FORCE_SSR"]   == "1"
  return false if ENV["SVELTE_ON_RAILS_DISABLE_SSR"] == "1"

  # 2. Positive signal: `rails server` has been dispatched. This
  #    constant is defined by Rails::Command::ServerCommand before the
  #    app boots, and is a very reliable indicator.
  return true if defined?(::Rails::Server)

  # 3. Positive signal: a Puma::Launcher instance is already alive.
  #    Puma instantiates its Launcher inside Puma::Runner#run, which
  #    runs BEFORE Rails' after_initialize (because Puma requires the
  #    Rack app, and requiring the app is what triggers Rails boot).
  #    So by the time this method is called from our after_initialize
  #    hook, the launcher exists in ObjectSpace iff we are inside Puma.
  return true if puma_launcher_alive?

  # 4. Positive signal: launched via `puma` / `pumactl` binary directly.
  #    Redundant with (3) in most cases, but harmless and useful for
  #    exotic launchers that bypass Puma::Runner.
  return true if program_basename == "puma"

  # Everything else: not an HTTP-serving context (as far as we can tell
  # at boot time). If we're wrong, the lazy fallback will handle it.
  false
end