Skip to content

Commit 407bc81

Browse files
committed
improve the documentation for the redirect component
1 parent b9c383f commit 407bc81

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

examples/official-site/sqlpage/migrations/09_redirect.sql

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,38 @@ INSERT INTO component (name, description, icon, introduced_in_version)
22
VALUES (
33
'redirect',
44
'Redirects the user to another page.
5-
This component is useful for implementing redirects after a form submission,
6-
or to redirect users to a login page if they are not logged in.
7-
8-
Contrary to the http_header component, this component completely stops the execution of the page after it is called,
9-
so it is suitable to use to hide sensitive information from users that are not logged in, for example.
105
11-
Since it uses an HTTP header to redirect the user, it is not possible to use this component after the page has started being sent to the browser.',
6+
This component helps you:
7+
1. Send users to a different page
8+
1. Stop execution of the current page
9+
10+
### Conditional logic
11+
12+
There is no `IF` statement in SQL. Even when you use a [`CASE` expression](https://modern-sql.com/caniuse/case_(simple)), all branches are always evaluated (and only one is returned).
13+
14+
To conditionally execute a component or a [SQLPage function](/functions.sql), you can use the `redirect` component.
15+
A common use case is error handling. You may want to process with the rest of a page only when certain pre-conditions are met.
16+
17+
```sql
18+
SELECT
19+
''redirect'' AS component,
20+
''error_page.sql'' AS link
21+
WHERE NOT your_condition;
22+
23+
-- The rest of the page is only executed if the condition is true
24+
```
25+
### Technical limitation
26+
27+
You must use this component **at the beginning of your SQL file**, before any other components that might send content to the browser.
28+
Since the component needs to tell the browser to go to a different page by sending an *HTTP header*,
29+
it will fail if the HTTP headers have already been sent by the time it is executed.
30+
31+
> **Important difference from [http_header](?component=http_header)**
32+
>
33+
> This component completely stops the page from running after it''s called.
34+
> This makes it a good choice for protecting sensitive information from unauthorized users.
35+
36+
',
1237
'arrow-right',
1338
'0.7.2'
1439
);

0 commit comments

Comments
 (0)