Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix "EOFError error" when using webpack dev server over HTTPS #113

Merged
merged 4 commits into from
Feb 23, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 31 additions & 18 deletions lib/inline_svg/webpack_asset_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,45 @@ def self.find_asset(filename)

def initialize(filename)
@filename = filename
@asset_path = Webpacker.manifest.lookup(@filename)
end

def pathname
file_path = Webpacker.instance.manifest.lookup(@filename)
return unless file_path
return if @asset_path.blank?

if Webpacker.dev_server.running?
asset = Net::HTTP.get(Webpacker.dev_server.host, file_path, Webpacker.dev_server.port)

begin
Tempfile.new(file_path).tap do |file|
file.binmode
file.write(asset)
file.rewind
end
rescue StandardError => e
Rails.logger.error "Error creating tempfile: #{e}"
raise
end
dev_server_asset(@asset_path)
elsif Webpacker.config.public_path.present?
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we can get rid of this check. I can't think of a scenario where an application using webpacker would not have Webpacker.config.public_path configured. 🤔

File.join(Webpacker.config.public_path, @asset_path)
end
end

private

else
public_path = Webpacker.config.public_path
return unless public_path
def dev_server_asset(file_path)
asset = fetch_from_dev_server(file_path)

File.join(public_path, file_path)
begin
Tempfile.new(file_path).tap do |file|
file.binmode
file.write(asset)
file.rewind
end
rescue StandardError => e
Rails.logger.error "[inline_svg] Error creating tempfile for #{@filename}: #{e}"
raise
end
end

def fetch_from_dev_server(file_path)
http = Net::HTTP.new(Webpacker.dev_server.host, Webpacker.dev_server.port)
http.use_ssl = Webpacker.dev_server.https?
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VERIFY_NONE should be sufficient here, since the webpack dev server should be run in development environments only.


http.request(Net::HTTP::Get.new(file_path)).body
rescue StandardError => e
Rails.logger.error "[inline_svg] Error fetching #{@filename} from webpack-dev-server: #{e}"
raise
end
end
end