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

Include cache headers in 304 responses #2061

Merged
merged 2 commits into from
Nov 29, 2024
Merged

Include cache headers in 304 responses #2061

merged 2 commits into from
Nov 29, 2024

Conversation

alco
Copy link
Member

@alco alco commented Nov 28, 2024

Fixes #2059

@alco alco requested a review from KyleAMathews November 28, 2024 11:13
Copy link
Contributor

@KyleAMathews KyleAMathews left a comment

Choose a reason for hiding this comment

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

Nice! So it was just a bug from the ordering of the imports?

@@ -351,7 +351,7 @@ defmodule Electric.Plug.ServeShapePlug do
get_req_header(conn, "if-none-match")
|> Enum.flat_map(&String.split(&1, ","))
|> Enum.map(&String.trim/1)
|> Enum.map(&String.trim(&1, ~S|"|))
|> Enum.map(&String.trim(&1, <<?">>))
Copy link
Contributor

Choose a reason for hiding this comment

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

What's this do?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's doing the same thing as before but is easier to read to my Elixir eyes.

<<...>> is a container syntax for binaries in Elixir. By default, every element is treated as a byte:

<<80, 79, 80>> == "POP"

The ?... syntax is a parser feature the replaces itself with the ASCII code of the character, so:

?P == 80
?O == 79
?" == 34

The <<...>> syntax is used almost every time when you need to parse a string while doing different things for different classes of characters. See https://github.com/electric-sql/electric/blob/main/packages/sync-service/lib/electric/postgres/identifiers.ex for some examples.

I much prefer using it over ad-hoc delimiters with the ~S sigil because it's so common in Elixir code already. TO be fair, we also have some occurrences of "\"" in the code, it's yet another way to write the same string containing a single double-quote character.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah neat!

@alco
Copy link
Member Author

alco commented Nov 28, 2024

Nice! So it was just a bug from the ordering of the imports?

I've changed the order of plug applications, which is simply changing the order of function application as each plug is a function. Now, we first set the response headers on the conn struct, then perform the Etag check which may return early in case the Etag in the request header matches.

Copy link

netlify bot commented Nov 29, 2024

Deploy Preview for electric-next ready!

Name Link
🔨 Latest commit ed6fea8
🔍 Latest deploy log https://app.netlify.com/sites/electric-next/deploys/67498effce500e000815f9f9
😎 Deploy Preview https://deploy-preview-2061--electric-next.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@alco alco merged commit 704ac91 into main Nov 29, 2024
25 of 26 checks passed
@alco alco deleted the alco/304-cache-headers branch November 29, 2024 10:06
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.

Extend max-age in cache-control headers for 304 responses
3 participants