_hyperscript icon indicating copy to clipboard operation
_hyperscript copied to clipboard

PROPOSAL: Cookies

Open benpate opened this issue 4 years ago • 7 comments

Can we implement a way to add/remove cookies in hyperscript? This is similar to #127, but for stylesheets:

If we add expression cookie (similar to tag, input, or button from #121) that would identify a named cookie then we could do something like this:

set cookie "theme" to "dark" expiring NEVER

fetch /signin
set cookie "authorization" to it expiring 1 hour from now

remove cookie "theme"

Again, this is a hot take on giving hyperscript the ability to manage stylesheets dynamically, and I'm happy to keep looking for better options.

benpate avatar Apr 17 '21 20:04 benpate

+1 on this. Would very much love to quickly drop in some lightweight preferences mgmt and it couldn't be easier to read than above ⬆️

rtgoodwin avatar Oct 11 '21 12:10 rtgoodwin

I like it!

1cg avatar Oct 11 '21 21:10 1cg

I looked at this a little bit tonight. It seems like the changes here should go into the runtime.resolveSymbol() and runtime.setSymbol() functions.

However, if we do that, it might be hard to include features like ... expiring tomorrow but perhaps that could be included/overridden in a configuration function named something like htmx.config.setCookie().

@1cg, do you agree? If so, I think I can make this update. Or, give me your guidance if you have other thoughts, and I'll happily adjust this plan.

benpate avatar Oct 12 '21 04:10 benpate

I think this would be a new expression type, with the start cookie and then a custom syntax after that for declaring expiration, etc.

The expression itself would return the value of the cookie, and then you'd need to update set and put to handle setting values in the cookies (ideally by deferring to the cookie expression itself)

1cg avatar Oct 12 '21 15:10 1cg

I'm rubbish at JS so I can't offer much here but excitement and thanks :) (and if you need some testing, I'm trying to launch a couple pages this weekend and could include a build; the basic feature is just remembering some filter preferences (probably an array or two) so if it failed gracefully it's no biggie.

rtgoodwin avatar Oct 12 '21 16:10 rtgoodwin

@1cg -- I think I follow. I was heading down the road of making it a namespace. I'll aim at expressions and see where I get. Not totally sure I follow what the changes would be to the set statement, but let me knock it around for a bit first before I cry for help.

@rtgoodwin - cool, thanks. I'm not sure when I'll have something testable, but I'll reference this issue in the PR when I have something worth sharing. More eyes are always better!

benpate avatar Oct 12 '21 22:10 benpate

Some cookie support in hyperscript would be great as it is one of the only reasons why I sometimes use Alpine.js + js-cookie instead of HTMX + hyperscript.

medihack avatar Jul 15 '22 08:07 medihack

Hey @1cg do you think this is something that will eventually make it into Hyperscript?

fatherofinvention avatar Jun 07 '23 15:06 fatherofinvention

how do people feel about this? image

1cg avatar Jun 07 '23 15:06 1cg

@1cg I think that's perfect.

fatherofinvention avatar Jun 07 '23 17:06 fatherofinvention