Session expired
When a request finds the session gone, the server answers with a redirect header and the browser loads the sign-in page in full, with a link back to where the user was.
The page
A page stays open for an hour, and when the user finally clicks something the session is gone. A sign-in form swapped into the middle of the page would be confusing, so the page needs nothing special. Every request on it is covered:
<script src="https://cdn.jsdelivr.net/gh/ypy-develop/[email protected]/dist/ypy.min.js"></script>
<button ypy-get="/reports/42" ypy-target="#report" ypy-swap="innerHTML">Load report</button>
<div id="report"></div>What the server answers
For every protected route, a request with no valid session gets an answer with no body and one header:
GET /reports/42
YPY-Request: true
YPY-Current-URL: https://app.example.com/reports?month=2026-10
HTTP/1.1 204 No Content
YPY-Redirect: /login?next=%2Freports%3Fmonth%3D2026-10
Vary: YPY-Request| Request | Answer |
|---|---|
| YPY request, session valid | the fragment, 200 |
| YPY request, session gone | 204 with YPY-Redirect: /login?next=<current page> and no body |
| Any other request, session gone | 303 See Other with Location: /login?next=<requested page> |
-
Where to come back to is the page the user is on, not the fragment address. A YPY request sends it in the
YPY-Current-URLheader, sonextcarries that one. - A body would be swapped in before the navigation, so there is none.
- The sign-in page accepts only a local path in
next(it starts with/and not//) before it redirects there.
How it works
-
The client reads
YPY-Redirectbefore it looks at the status code, so the header also works on a401. The browser navigates with a full page load, to the same origin only. A different origin firesypy:security:blockedunlessconfig.modeiscors. YPY-Current-URLis the address of the page that made the request.- The
Vary: YPY-Requestheader keeps a cache from serving the redirect to a plain request, or the reverse.
Variation: keep the page and say so
Sometimes a redirect loses work, for instance a long form. Answer 401 with a short fragment and let the page put it
in a fixed place. Put the rule on a container, once:
<body ypy-status:401:inherited="target:#session-notice swap:innerHTML">
<div id="session-notice" role="alert"></div>
...
</body>The server answers:
HTTP/1.1 401 Unauthorized
Content-Type: text/html; charset=utf-8
Vary: YPY-Request<p>Your session ended. <a href="/login?next=%2Freports%3Fmonth%3D2026-10">Sign in again</a>.</p>ypy-status:<code> accepts swap:, target:, select:, push:, replace: and transition:. A range works too:
ypy-status:4xx.