mirror of
https://github.com/benbusby/farside.git
synced 2025-06-06 01:16:37 +00:00
Preserve redirect with /_/
path prefix (#13)
This adds a straightforward way of preserving Farside's redirecting behavior in the user's browser history. That way if an instance becomes unavailable between the 5 min scans, the user can opt to navigate back one page and be taken to a new instance. This is accomplished using a single line of JS, and could potentially work as the default behavior of Farside (with the current default behavior requiring a path prefix instead). This should be revisited down the road when more people are using this service.
This commit is contained in:
parent
724a995fd9
commit
932f3bbcab
5 changed files with 34 additions and 0 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -11,3 +11,5 @@ erl_crash.dump
|
||||||
|
|
||||||
# Ignore results from update script
|
# Ignore results from update script
|
||||||
.update-result*
|
.update-result*
|
||||||
|
|
||||||
|
*.rdb
|
||||||
|
|
|
@ -55,6 +55,13 @@ Farside's routing is very minimal, with only the following routes:
|
||||||
URL>/r/popular`
|
URL>/r/popular`
|
||||||
- Note that a path is not required. `/libreddit` for example will still
|
- Note that a path is not required. `/libreddit` for example will still
|
||||||
redirect the user to a working libreddit instance
|
redirect the user to a working libreddit instance
|
||||||
|
- `/_/:service/*glob`
|
||||||
|
- Achieves the same redirect as the main `/:service/*glob` endpoint, but
|
||||||
|
preserves a short landing page in the browser's history to allow quickly
|
||||||
|
jumping between instances by navigating back.
|
||||||
|
- Ex: `/_/nitter` -> nitter instance A -> (navigate back one page) -> nitter
|
||||||
|
instance B -> ...
|
||||||
|
- *Note: Uses Javascript to preserve the page in history*
|
||||||
|
|
||||||
When a service is requested with the `/:service/...` endpoint, Farside requests
|
When a service is requested with the `/:service/...` endpoint, Farside requests
|
||||||
the list of working instances from Redis and returns a random one from the list
|
the list of working instances from Redis and returns a random one from the list
|
||||||
|
|
|
@ -9,6 +9,7 @@ config :farside,
|
||||||
previous_suffix: "-previous",
|
previous_suffix: "-previous",
|
||||||
services_json: "services.json",
|
services_json: "services.json",
|
||||||
index: "index.eex",
|
index: "index.eex",
|
||||||
|
route: "route.eex",
|
||||||
headers: [
|
headers: [
|
||||||
{"User-Agent", "Mozilla/5.0 (Linux x86_64; rv:94.0) Gecko/20100101 Firefox/94.0"},
|
{"User-Agent", "Mozilla/5.0 (Linux x86_64; rv:94.0) Gecko/20100101 Firefox/94.0"},
|
||||||
{"Accept", "text/html"},
|
{"Accept", "text/html"},
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
defmodule Farside.Router do
|
defmodule Farside.Router do
|
||||||
@index Application.fetch_env!(:farside, :index)
|
@index Application.fetch_env!(:farside, :index)
|
||||||
|
@route Application.fetch_env!(:farside, :route)
|
||||||
|
|
||||||
use Plug.Router
|
use Plug.Router
|
||||||
|
|
||||||
|
@ -24,6 +25,19 @@ defmodule Farside.Router do
|
||||||
send_resp(conn, 200, resp)
|
send_resp(conn, 200, resp)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
get "/_/:service/*glob" do
|
||||||
|
r_path = String.slice(conn.request_path, 2..-1)
|
||||||
|
|
||||||
|
resp =
|
||||||
|
EEx.eval_file(
|
||||||
|
@route,
|
||||||
|
service: service,
|
||||||
|
instance_url: r_path
|
||||||
|
)
|
||||||
|
|
||||||
|
send_resp(conn, 200, resp)
|
||||||
|
end
|
||||||
|
|
||||||
get "/:service/*glob" do
|
get "/:service/*glob" do
|
||||||
path = Enum.join(glob, "/")
|
path = Enum.join(glob, "/")
|
||||||
instance = Farside.pick_instance(service)
|
instance = Farside.pick_instance(service)
|
||||||
|
|
10
route.eex
Normal file
10
route.eex
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
<head>
|
||||||
|
<title>Farside Redirect - <%= service %></title>
|
||||||
|
<meta http-equiv="refresh" content="1; url=<%= instance_url %>">
|
||||||
|
<script>
|
||||||
|
history.pushState({page: 1}, "Farside Redirect");
|
||||||
|
</script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<span>Redirecting to <%= service %> instance...
|
||||||
|
</body>
|
Loading…
Add table
Add a link
Reference in a new issue