Open dramalho opened 5 months ago
My own site is a small Rails app but it's still using Paperclip for images because I haven't got around to answering this very question 😬 I have a vague memory of looking into using pre-defined/named variants https://www.bigbinary.com/blog/rails-7-adds-ability-to-use-predefined-variants but I can't have reached a conclusion.
Thanks for creating this issue, hopefully it will motivate me and we can find an answer.
I am using variants to build the srcset (it's like paperclip in that sense, before variants you would define transformations on views and such, variants are just pre-defined transformations) -- I was looking at the crawler class and it really only processes <a>
tags, which I should have guessed, otherwise static assets would come along as well :)
I've got two minds for this:
a
tags :)Paperclip would just write the assets in the public folder, was it? (I should know, my memory selectively blocked that for now :D )
ok, a minor update
I tried (2) from my comment above, I can write a script / rake task that goes on to generate URLS for every variant representation in the database, yada yada yada, BUT rails generates signed urls (with expiration) and I didn't went down the rabbit hole deep enough to ensure a URL generated on the console ends up being the same as the rack app will eventually generate. That's the blocker there so far, because otherwise I could just generate the URL and copy the file over to the build
folder and be done with it - possibly
anyhoo, I then gave up and via the power of bundle exec gem open parklife
🙈 I patched the scan_for_links
method to be slightly more comprehensive
def scan_for_links(html)
doc = Nokogiri::HTML.parse(html)
urls = doc.css('a').map { |v| URI.parse(v[:href]) }
urls.concat(doc.css('img').map { |v| URI.parse(v[:src]) })
urls.concat(doc.css('source').map { |v| URI.parse(v[:srcset]) })
# urls.concat(doc.css('[style*="background-image]').map { |v| URI.parse(v[:srcset]) })
urls.each do |uri|
# Don't visit a URL that belongs to a different domain - for now this is
# a guess that it's not an internal link but it also covers mailto/ftp
# links.
next if uri.host
# Don't visit a path-less URL - this will be the case for a #fragment
# for example.
next if uri.path.nil? || uri.path.empty?
yield uri.path
end
end
this works fine, except that in my design I ended up needing to put images as style='background-image
and that's slightly messy to content with -- a bit too much I would say , so I'll probably just change the markup
anyways, unsure how you feel about this or what scenarios you might have run into before that kept scan_for_links
to stick to <a>
tags, but I would love to hear you thoughts :)
I think Parklife's Rails integration might be able to take care of most of this by subscribing to the service_url.active_storage
instrumentation event, noting the blobs referenced while the site is crawled, and then copying the relevant files to the build directory (or exposing them for you to do whatever). BUT it doesn't seem to work - I only ever see the events from a single attachment 🤷🏻♂️ So it doesn't work, but I think it should! I need to spend more time on it.
Unsure if this anyone has raised this before (or even encountered) but I'm trying to figure out if I can replace Jekyll with a very lean rails app to run my blog locally and then use parklife to generate a static build .
All good so far - base url, assets, etc - but I've now hit an ActiveStorage issue where I'm taking advantage of the thing to preprocess images (webp / srcset).
i.e. on my view I'm using something like
which generates
Running the build script looks ok too, it's generating urls using the
--base
I need BUT it won't actually crawl the images and so they never end up in thebuild
folder .I'm unsure how the crawling process work so I'm unsure if it's a problem with not gathering those urls (some are inside a
source
tag , that might be why) or something else. Regular assets are handled byassets:precompile
+cp
but this is a slightly different issue and I'm wondering if you have some words of wisdom here :)Thanks