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

Sprockets to propshaft #1410

Open
wants to merge 16 commits into
base: master
Choose a base branch
from

Conversation

rahul1990gupta
Copy link
Contributor

@rahul1990gupta rahul1990gupta commented Dec 26, 2024

Overview

This PR uses propshaft in place of sprockets for asset management and delivery. This is in line with rails 8's recommendation and as outlines in this issue #1366

This PR

  • Removes sprocket and uglifier. Adds importmap-rails and propshaft gems for javascript and css delivery.
  • Changes application css from erb template to vanilla css file.

Testing

lobsters_autosize.mp4
lobsters_tomselect.mp4

@pushcx
Copy link
Member

pushcx commented Dec 26, 2024

I don’t want to split up the css into multiple files. I don’t see any benefit for a site this small.

Also, the LIGHT and DARK variables are there so users can override their browser’s default, see the references to prefers_color_scheme. I only glanced at the diff here but I think your change might break that. I’d love to reduce the duplication of having to print the dark colors twice and saw a mention on Bluesky that an expert thought it was possible, but I can’t find it again in the limited time I have this morning. I’ll have to keep looking.

@rahul1990gupta
Copy link
Contributor Author

The reason I suggested breaking up application.css to make the css more modular and with http 2, it is supported without any performance penalty. But i get it that benefits are mostly cosmetic and css file is not that too large.

A side question, what do you think about incorporating tailwindcss into the project ? It would be a larger rewrite of the css, but may make the UI more "modern".. maybe ? :D

@pushcx
Copy link
Member

pushcx commented Dec 31, 2024

Tailwind would reintroduce the nodejs dep I want to remove (or other build complexity). It would add a new, big dependency to maintain and for potential contributors to learn. It's not really a goal to keep the site looking modern, I want to keep css small so we can run it on cheap hosting and pages load in a snap. And because we're a programming community, our users are unusually broad in choice of browsers so it's nice to keep things simple and reliable.

If you want a big css project, #1279 has one. But let's keep it separate from this PR!

@pushcx
Copy link
Member

pushcx commented Dec 31, 2024

I can't find the original article on reducing duplication in the dark and light versions of the CSS but this article on light-dark has a design pattern that looks great. Probably also worth putting in a different PR than this build update!

@rahul1990gupta rahul1990gupta marked this pull request as ready for review January 1, 2025 10:38
@rahul1990gupta
Copy link
Contributor Author

I was looking at the autosize.js javascript library used in the project and was wondering if is possible to replace these legacy libraries with something simple in modern html and js features.

Will something like this capture all autosize.js functionalies.

@pushcx
Copy link
Member

pushcx commented Jan 1, 2025

So, I don’t think I’ve looked at that autosize js for years, I’m glad you took a fresh look at it. Reviewing, I think you’re right, we could drop that dependency and have just a little custom code for it.

It looks like you’ll need to add word-wrap: break-word; to the css (see autosize.js line 209). Otherwise a lot of the existing js works around layout issues we don’t have or events/lifecycle hooks we don’t need. Two things I don’t fully understand: 1. is there special behavior when the page resizes up or down? does the current library change autosize when the textarea is taller than the visible window and that resizes? 2. In resize(), what’s the purpose of the scrollTop code? Is it making sure it doesn’t feel like the textarea’s scroll position changes as the textarea changes size?

If you want to continue removing the library, please open a separate PR for removing it. I really look forward to it!

@@ -93,11 +95,11 @@ class _LobstersFunction {
this.curUser = null;

this.storyFlagReasons = ({
<%= Vote::STORY_REASONS.map{|k,v| "'#{k}': '#{v}'" }.join(", ") %>
'O': 'Off-topic', 'A': 'Already Posted', 'B': 'Broken Link', 'S': 'Spam', '': 'Cancel'
Copy link
Member

Choose a reason for hiding this comment

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

This (and commentFlagReasons) exist to avoid duplicating flags between the Vote model and here. Can you find another way to remove this duplication? Is there any negative to doing this with ERB?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we use ERB template for css and javascript, we will need to have sprocket. propshaft and importmap doesn't support erb templating. They don''t process files in any way. simply serves to browser after appending a checksum hash. This simplifies our asset pipeline, but we lose some features. That's why I had to remove templating from css and javascript files.

To avoid duplication, we could put this map in a view helper module and render that in the html directly and read it in javascript from html, if need be. Does that sound like a good idea ?

Copy link
Member

Choose a reason for hiding this comment

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

Ah, thanks for explaining.

It was already a project to remove inline scripts from the site (#696 #697 #779) as a long-term security improvement, so I don't want to add one back.

One option would be to have the js be the authoritative list of flag reasons and have Rails parse it out at startup. Maybe it's because I'm mostly a backend developer, but that feels backwards and I don't want to do that.

I guess we only add and remove flags very infrequently. Please add a comment to this js explaining the reasons are duplicated in the Vote model, and add the reverse comment to Vote model.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added the comments.

Copy link
Member

Choose a reason for hiding this comment

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

I talked this through on stream and someone pointed out that I misunderstood your suggestion. An easy way to remove this duplication would be to have an invisible html element with the flags in json and have the js load them. Now that I understand that, I think that's the best idea. Sorry I had you write comments.

We don't need to make it a standalone helper please add a meta tag in the head.

So I think that'll be <meta name="storyFlags" content="<%= Vote::STORY_REASONS.to_json "> and something very similar for commentFlags.

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 spent close to 2 hours making test work, but rspec is new to me. Will spend some more time later.

vendor/javascript/.keep Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants