Sha256: cd5248e6ac24b8f66683148f4f6ba148c40a60ccf8da03bbcde9540bed4be1cb

Contents?: true

Size: 1.92 KB

Versions: 6

Compression:

Stored size: 1.92 KB

Contents

# frozen_string_literal: true

require "rack/indifferent"

class Roda
  module RodaPlugins
    module BridgetownSSR
      def self.configure(app, _opts = {}, &block)
        app.opts[:bridgetown_site] = Bridgetown::Site.start_ssr!(&block)
      end
    end

    register_plugin :bridgetown_ssr, BridgetownSSR
  end
end

module Bridgetown
  module Rack
    class Roda < ::Roda
      plugin :hooks
      plugin :common_logger, Bridgetown::Rack::Logger.new($stdout), method: :info
      plugin :json
      plugin :json_parser
      plugin :cookies
      plugin :public, root: Bridgetown::Current.preloaded_configuration.destination
      plugin :not_found do
        output_folder = Bridgetown::Current.preloaded_configuration.destination
        File.read(File.join(output_folder, "404.html"))
      rescue Errno::ENOENT
        "404 Not Found"
      end
      plugin :error_handler do |e|
        puts "\n#{e.class} (#{e.message}):\n\n"
        puts e.backtrace
        output_folder = Bridgetown::Current.preloaded_configuration.destination
        File.read(File.join(output_folder, "500.html"))
      rescue Errno::ENOENT
        "500 Internal Server Error"
      end

      def _roda_run_main_route(r) # rubocop:disable Naming/MethodParameterName
        if self.class.opts[:bridgetown_site]
          # The site had previously been initialized via the bridgetown_ssr plugin
          Bridgetown::Current.site ||= self.class.opts[:bridgetown_site]
        end
        Bridgetown::Current.preloaded_configuration ||=
          self.class.opts[:bridgetown_preloaded_config]

        r.public

        r.root do
          output_folder = Bridgetown::Current.preloaded_configuration.destination
          File.read(File.join(output_folder, "index.html"))
        end

        super
      end

      # Helper shorthand for Bridgetown::Current.site
      # @return [Bridgetown::Site]
      def bridgetown_site
        Bridgetown::Current.site
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
bridgetown-core-1.0.0.alpha6 lib/bridgetown-core/rack/roda.rb
bridgetown-core-1.0.0.alpha5 lib/bridgetown-core/rack/roda.rb
bridgetown-core-1.0.0.alpha4 lib/bridgetown-core/rack/roda.rb
bridgetown-core-1.0.0.alpha3 lib/bridgetown-core/rack/roda.rb
bridgetown-core-1.0.0.alpha2 lib/bridgetown-core/rack/roda.rb
bridgetown-core-1.0.0.alpha1 lib/bridgetown-core/rack/roda.rb