Best approach to implementing static asset middleware plugin? #805
-
Hi! Experienced Ruby dev but Bridgetown noob here. I've read all of the docs and a lot of the bridgetown-core source code so I am somewhat familiar with all of the moving parts, but I could use some help pointing me in the right direction with my problem. I'm building a photo blog and I'm currently storing my photos in Ideally what I want to build is a plugin that acts as "middleware" for my static images and performs some transformations before writing to output, for example:
Let's I have an image:
The plugin would write multiple images to the output dir:
What is the best way to approach writing a plugin like this? I am very experienced with Ruby and working with images, but I'm new to Bridgetown so I'm unsure the best way to tap into the build process to manipulate static assets like this. Are there any existing plugins that do something similar that I could look at for inspiration? Any advice you have would be greatly appreciated. Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hey @caseyohara, I don't know if there are any existing plugins that do what you're after. The cloudinary plugin (https://github.com/bridgetownrb/bridgetown-cloudinary) helps you integrate Cloudinary with Bridgetown which can do transformations and the like, I'm not sure, I've never used it. To your actual question, I'd firstly recommend against storing your images in the Git repo itself. It can cause the repo size to really balloon. I have a photo gallery website powered by Bridgetown (https://fslash42.com). I do store the photos in the project, but they're in a gitignored folder. I have a rake task which syncs this folder to an S3 bucket which has a CloudFront distribution in front of it. I'd recommend doing something similar. If you're keen on storing your images in the repo anyway, I'd recommend storing them in a folder prefixed with a # plugins/builders/photos_builder.rb
class Builders::AlbumsBuilder < SiteBuilder
def build
hook :site, : pre_read do |site|
# Transform your images here and write them to a folder in the project which will be output, eg: `photographs`
end
end You might need to experiment with different hooks to see which one works for you: https://www.bridgetownrb.com/docs/plugins/hooks#complete-list-of-hooks. Also you probably don't want to do this on every single build so you'd need a way to optimise/cache this work. Without knowing more specifics it's hard to advise. Feel free to ask further questions if you'd like, I'm happy to help! I'm also happy to share my photo gallery website's source with you (privately) if you like. |
Beta Was this translation helpful? Give feedback.
Hey @caseyohara,
I don't know if there are any existing plugins that do what you're after. The cloudinary plugin (https://github.com/bridgetownrb/bridgetown-cloudinary) helps you integrate Cloudinary with Bridgetown which can do transformations and the like, I'm not sure, I've never used it.
To your actual question, I'd firstly recommend against storing your images in the Git repo itself. It can cause the repo size to really balloon. I have a photo gallery website powered by Bridgetown (https://fslash42.com). I do store the photos in the project, but they're in a gitignored folder. I have a rake task which syncs this folder to an S3 bucket which has a CloudFront distribution in front of i…